我有一个循环,可以制作3x3网格按钮。我按下按钮时会尝试这样做,会打开一个窗口供用户添加有关按钮的数据。窗口打开,一切都在第一次工作。保存此窗口并单击一个新按钮后,会出现一个覆盖所有内容的白色方块(可能是面板)。如果我使窗口变大,我可以看到它后面的文本字段,但白色方块保持相同的大小(非扩展窗口的大小)。在ex;和窗口之后,我认为我为最后一个按钮添加的数据仍然存在于' new'窗口,让我知道我可能正在打开同一个窗口而新的窗口没有被创建。有没有办法每次创建一个全新的窗口?
这是创建按钮的循环
for (int i = 0; i < addButtons3.length; i++) {
addButtons3[i] = new JButton(" Add Bed "); // make text big
addButtons3[i].addActionListener(new RoomListener());
addButtons3[i].setActionCommand("" + i);
gbc.fill = GridBagConstraints.BOTH;
room3Panel.add(addButtons3[i]);
}
这是每个按钮的actionListener
class RoomListener implements ActionListener{
public void actionPerformed(ActionEvent event){
AbstractButton btn = (AbstractButton) event.getSource();
source = event.getActionCommand();
System.out.println(source);
x = Integer.parseInt(source);
y = Integer.parseInt(roomSource);
btn.setText("Adding bed..");
intFrame8.setMaximizable(true); //Add maximize
intFrame8.setIconifiable(true); //Make it configurable
intFrame8.setResizable(true); //Make it resizable
intFrame8.setClosable(true); //How will it react on close?
intFrame8.setDefaultCloseOperation(DISPOSE_ON_CLOSE); //Like this
intFrame8.setSize(320,240);
desktopPane.add(intFrame8);
JPanel addRoomPanel = new JPanel();
addRoomPanel.setLayout(new GridLayout(7,1,5,5)); //row/col/rspace/cspace
addRoomPanel.setBackground(Color.white);
intFrame8.getContentPane().add(addRoomPanel);
intFrame8.toFront();
//Name text field
nameLabel.setText("Name: ");
name.addActionListener(new nameListener());
addRoomPanel.add(nameLabel);
addRoomPanel.add(name);
//DoB text field
dobLabel.setText("Date of Birth: ");
dob.addActionListener(new dobListener());
addRoomPanel.add(dobLabel);
addRoomPanel.add(dob);
//Passport Number text field
passNum.addActionListener(new passNumListener());
addRoomPanel.add(passNumLabel);
addRoomPanel.add(passNum);
//Start Date text field
startDate.addActionListener(new startDateListener());
addRoomPanel.add(startDateLabel);
addRoomPanel.add(startDate);
//End Date text field
endDate.addActionListener(new endDateListener());
addRoomPanel.add(endDateLabel);
addRoomPanel.add(endDate);
//Comments section text field
comments.addActionListener(new commentsListener());
addRoomPanel.add(commentsLabel);
addRoomPanel.add(comments);
addBedSave.addActionListener(new bedSaveListener());
addRoomPanel.add(addBedSave);
intFrame8.setVisible(true);
}
}
以下是&#39; save&#39;的actionListener。关闭窗口的按钮
class bedSaveListener implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println(nameTemp);
nameList[x][y] = nameTemp;
dobList[x][y] = dobTemp;
startDateList[x][y] = startDateTemp;
endDateList[x][y] = endDateTemp;
passNumList[x][y] = passNumTemp;
commentsList[x][y] = commentsTemp;
intFrame8.dispose();
switch(y){
case 0:
intFrame4.toFront();
addButtons1[x].setText(nameTemp);
break;
case 1:
intFrame5.toFront();
addButtons2[x].setText(nameTemp);
break;
case 2:
intFrame6.toFront();
addButtons3[x].setText(nameTemp);
break;
case 3:
intFrame7.toFront();
addButtons4[x].setText(nameTemp);
break;
}
}
}
答案 0 :(得分:0)
建议:对ActionListeners使用if(e.getSource == some_JButton){//code}
。