第一个弹出窗口只会发送一个动作,第二个弹出窗口将发送两个动作,第三个弹出窗口将发送三个动作,依此类推。我能够将其范围缩小为多次发送操作的按钮。
起初,我在所有窗口中都使用jframe,所以我尝试使用jdialog,问题仍然存在。尝试这样做,以便当用户单击按钮时,将窗口丢弃,仍然不要修复它。
public class BoothDetails extends JDialog implements ActionListener{
FloorPlanGUI floorPlan = new FloorPlanGUI();
static JLabel bname = new JLabel();
JTextArea details = new JTextArea();
static JButton addsche = new JButton("ADD TO SCHEDULE");
JPanel northPanel = new JPanel();
public BoothDetails(String name, String detail) {
setVisible(true);
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
bname.setText(name);
details.setText(detail);
setLayout(new BorderLayout());
northPanel.setLayout(new FlowLayout(0, 10, 10));
northPanel.add(bname);
northPanel.add(addsche);
addsche.addActionListener(floorPlan);
addsche.addActionListener(this);
add(northPanel, BorderLayout.NORTH);
add(details, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent a) {
dispose();
}
}
答案 0 :(得分:0)
static JLabel bname = new JLabel();
JTextArea details = new JTextArea();
static JButton addsche = new JButton("ADD TO SCHEDULE");
请勿使用static
关键字。这意味着该变量由该类的所有实例共享。
因此,每次创建该类的新实例时,都将执行以下代码:
addsche.addActionListener(floorPlan);
将另一个ActionListener添加到按钮。
通常仅当在类中创建常量变量时才使用static关键字,而不应将其用于每个类需要唯一的组件。