java中按钮显示不出文本,怎么回事?
packagemyborderlayout;importjava.awt.*;importjava.awt.event.WindowAdapter;importjava....
package myborderlayout;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
*
* @author Administrator
*/
public class MyBorderLayout {
Frame f;
Button east,south,west,north,center;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MyBorderLayout mb=new MyBorderLayout();
mb.go();
}
public void go()
{
f=new Frame("BorderLayout演示");
f=new Frame("FlowLayout演示");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
f.setVisible(false);
f.dispose();
System.exit(0);
}
});
f.setBounds(0,0,300,300);
f.setLayout(new BorderLayout());
north=new Button("北");
east=new Button("东");
south=new Button("南");
west=new Button("西");
center=new Button("中");
f.add(BorderLayout.NORTH,north);
f.add(BorderLayout.SOUTH,south);
f.add(BorderLayout.EAST,east);
f.add(BorderLayout.WEST,west);
f.add(BorderLayout.CENTER,center);
f.setVisible(true);
}
}
结果是下面这个样子的,没有显示东西南北中按钮: 展开
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
*
* @author Administrator
*/
public class MyBorderLayout {
Frame f;
Button east,south,west,north,center;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MyBorderLayout mb=new MyBorderLayout();
mb.go();
}
public void go()
{
f=new Frame("BorderLayout演示");
f=new Frame("FlowLayout演示");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
f.setVisible(false);
f.dispose();
System.exit(0);
}
});
f.setBounds(0,0,300,300);
f.setLayout(new BorderLayout());
north=new Button("北");
east=new Button("东");
south=new Button("南");
west=new Button("西");
center=new Button("中");
f.add(BorderLayout.NORTH,north);
f.add(BorderLayout.SOUTH,south);
f.add(BorderLayout.EAST,east);
f.add(BorderLayout.WEST,west);
f.add(BorderLayout.CENTER,center);
f.setVisible(true);
}
}
结果是下面这个样子的,没有显示东西南北中按钮: 展开
展开全部
Button不支持汉字排版,你的字符边距太大,按钮显示不下
把Button 全部替换成JButton
然后每一个按钮对象调用setMargin(new Insets(0,0,0,0));方法,把边距社为零,就可以显示了
改进代码如下:
package myborderlayout;
mport java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
/**
*
* @author Administrator
*/
public class MyBorderLayout {
Frame f;
JButton east,south,west,north,center;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MyBorderLayout mb=new MyBorderLayout();
mb.go();
}
public void go()
{
f=new Frame("BorderLayout演示");
f=new Frame("FlowLayout演示");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
f.setVisible(false);
f.dispose();
System.exit(0);
}
});
f.setBounds(0,0,300,300);
f.setLayout(new BorderLayout());
north=new JButton("北");
east=new JButton("东");
south=new JButton("南");
west=new JButton("西");
center=new JButton("中");
north.setMargin(new Insets(0,0,0,0));
east.setMargin(new Insets(0,0,0,0));
west.setMargin(new Insets(0,0,0,0));
center.setMargin(new Insets(0,0,0,0));
south.setMargin(new Insets(0,0,0,0));
f.add(BorderLayout.NORTH,north);
f.add(BorderLayout.SOUTH,south);
f.add(BorderLayout.EAST,east);
f.add(BorderLayout.WEST,west);
f.add(BorderLayout.CENTER,center);
f.setVisible(true);
}
}
可以正常显示,望采纳
把Button 全部替换成JButton
然后每一个按钮对象调用setMargin(new Insets(0,0,0,0));方法,把边距社为零,就可以显示了
改进代码如下:
package myborderlayout;
mport java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
/**
*
* @author Administrator
*/
public class MyBorderLayout {
Frame f;
JButton east,south,west,north,center;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MyBorderLayout mb=new MyBorderLayout();
mb.go();
}
public void go()
{
f=new Frame("BorderLayout演示");
f=new Frame("FlowLayout演示");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
f.setVisible(false);
f.dispose();
System.exit(0);
}
});
f.setBounds(0,0,300,300);
f.setLayout(new BorderLayout());
north=new JButton("北");
east=new JButton("东");
south=new JButton("南");
west=new JButton("西");
center=new JButton("中");
north.setMargin(new Insets(0,0,0,0));
east.setMargin(new Insets(0,0,0,0));
west.setMargin(new Insets(0,0,0,0));
center.setMargin(new Insets(0,0,0,0));
south.setMargin(new Insets(0,0,0,0));
f.add(BorderLayout.NORTH,north);
f.add(BorderLayout.SOUTH,south);
f.add(BorderLayout.EAST,east);
f.add(BorderLayout.WEST,west);
f.add(BorderLayout.CENTER,center);
f.setVisible(true);
}
}
可以正常显示,望采纳
更多追问追答
追问
恩,不错。蛮好的,我因为其实也是刚刚在学习java,所以对有些东西不理解。south.setMargin(new Insets(0,0,0,0));这句代码是什么意思能帮我解释一下么?其实,我把这几句代码去掉好像也没什么问题耶,这个需要在什么样的情况下实用呢?而且我发现运行后界面颜色变得还蛮好看的,不是一般白白的,这个是从哪一句代码体现的呢?
追答
1那句话的意思就是按钮中的文字 对上下左右四个方向的边距取零
2不用就不用呗,就是改样式的时候用。
3frame 有默认的背景色,你也可以修改的
JFrame j= new JFrame();
j. f.setBackground(Color.yellow);//输入你想要的颜色就行
展开全部
没有repaint()方法,没有重写paint方法。。
追问
这只是一个简单的边界布局的实例,为什么要用repaint()方法啊,又要如何paint方法,这两种方法要在什么样的情况下写呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询