Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的按钮不显示?_Java_User Interface - Fatal编程技术网

Java 为什么我的按钮不显示?

Java 为什么我的按钮不显示?,java,user-interface,Java,User Interface,我想构建一个bingo,获得以下源代码,它应该创建一个JFrame,其中25个按钮放置在5x5矩阵中。但是我的按钮没有一个被画在窗户上 我已经创建了一个Jpanel,按钮都放在上面,位置等都不具体,稍后会进行微调,第一件事就是把它们画在窗口上 Bingo Buttons是一个扩展JFrame的类,它只添加了两个方法,一个用于将其状态从true切换到false,另一个用于检查按钮当前是true还是false的方法(isSet) bingoField是一个字符串数组,它只保存按钮应该获取的数据 我不

我想构建一个bingo,获得以下源代码,它应该创建一个JFrame,其中25个按钮放置在5x5矩阵中。但是我的按钮没有一个被画在窗户上

我已经创建了一个Jpanel,按钮都放在上面,位置等都不具体,稍后会进行微调,第一件事就是把它们画在窗口上

Bingo Buttons是一个扩展JFrame的类,它只添加了两个方法,一个用于将其状态从true切换到false,另一个用于检查按钮当前是true还是false的方法(isSet)

bingoField是一个字符串数组,它只保存按钮应该获取的数据

我不明白为什么它什么都不做,请帮帮我。非常感谢您的任何帮助

public class BingoFrame extends JFrame {

public static final int BINGOSIZE=25;
public static final int BUTTON_X=50;
public static final int BUTTON_Y=50;

public BingoFrame() {
    setResizable(false);
    String[] bingoField = null;
    BingoButton[] buttons=new BingoButton[25];

try {
  bingoField = Utils.getRandomBingoField("Test");
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

this.setTitle("BS Bingo");
this.setResizable(false);
this.setLocation(50, 50);
this.setSize(600, 800);
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);

JPanel buttonPanel = new JPanel();
buttonPanel.setBounds(0, 0, 594, 772);
getContentPane().add(buttonPanel);
buttonPanel.setLayout(null);

for(int i=0;i<BINGOSIZE;i++) {
  buttons[i] = new BingoButton("Text");
}

//decorate buttons and add an action listener
for(int i=0;i<BINGOSIZE;i++) {
  final BingoButton temp = buttons[i];
  temp.setText(bingoField[i]);
  temp.setBackground(Color.white);
  temp.setForeground(Color.blue);
  temp.setPreferredSize(new Dimension(BUTTON_X,BUTTON_Y));

  temp.addActionListener(new ActionListener() {
    boolean toggle = false;
    @Override
    public void actionPerformed(ActionEvent e) {
      if (!temp.isSet()) {
        temp.setBackground(Color.blue);
        temp.setForeground(Color.white);
      } else {
        temp.setBackground(Color.white);
        temp.setForeground(Color.blue);
      }
      temp.toggle();
    }
  });

  buttons[i]=temp;
} 

//set Location for the buttons
for(int i=0;i<5;i++) {
    buttons[i].setLocation(100,(50*i)+10*(i+1));
}    
for(int i=5;i<10;i++) {
    buttons[i].setLocation(160,(50*i)+10*(i+1));
}
for(int i=10;i<15;i++) {
    buttons[i].setLocation(220,(50*i)+10*(i+1));
}
for(int i=15;i<20;i++) {
    buttons[i].setLocation(280,(50*i)+10*(i+1));
}
for(int i=20;i<25;i++) {
    buttons[i].setLocation(340,(50*i)+10*(i+1));
}

//add buttons to the panel
for(int i=0;i<BINGOSIZE;i++) {
  buttonPanel.add(buttons[i]);
}    

this.setVisible(true);   
公共类BingofName扩展了JFrame{
公共静态最终int BINGOSIZE=25;
公共静态最终整数按钮_X=50;
公共静态最终整数按钮_Y=50;
公共BingofName(){
可设置大小(假);
字符串[]bingoField=null;
BingoButton[]按钮=新BingoButton[25];
试一试{
bingoField=Utils.getRandomBingoField(“测试”);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
这是一个标题(“BS宾果”);
此参数为.setresizeable(false);
此设置位置(50,50);
这个。设置大小(600800);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT\u ON\u CLOSE);
getContentPane().setLayout(null);
JPanel buttonPanel=新的JPanel();
按钮面板立根(0,0594772);
getContentPane().add(按钮面板);
buttonPanel.setLayout(空);
对于(inti=0;i我得到了答案

我已将面板的布局更改为网格布局。这将使按钮在5x5矩阵中的位置对齐,并在按钮之间留出所需的间隙。这也使定位代码完全过时


通过简单地将布局更改为GridLayout,我的所有问题都消失了。

请更改窗口的标题,使其不那么亵渎。这是完整的代码吗?您缺少了类和主方法……它实际上正是对框架有用的。主方法(在另一个类中)简单地说就是对象的创建。我已经为frameclass添加了其余的代码。因此,您的框架显示了,但您的按钮没有显示?完全正确。我以前使用flowlayout进行过此操作,如果我想使用flow layout进行此操作,我会像应该的那样工作,但我想作为绝对布局进行此操作,但只要我编写。setLayout(null)只显示窗口,没有任何按钮。(1-)如果你想要一个网格,那么就使用一个
GridLayout
,而不是FlowLayout。也要去掉框架的空布局。Swing是为布局管理器设计的。没有理由使用空布局来处理类似的事情。你说得对,我只是没有想过。我的第一个想法是,如果面板像它需要的那样大因为换行符,按钮应该在哪里就在哪里。它在第一次尝试时起作用,所以我没有留下第二个想法。但是你是对的,网格布局更有意义。我将编辑anwser。