Java的弹出发送多个动作

问题描述 投票:0回答:1

第一弹出只会派一个动作,第二个会派二,三等奖将有三个如此等等。我可以将它缩小到它是按钮发送动作的多个时间。

起初,我是用的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();
    }
}
java swing actionlistener jdialog
1个回答
0
投票
static JLabel bname = new JLabel();
JTextArea details = new JTextArea();
static JButton addsche = new JButton("ADD TO SCHEDULE");

不要使用static关键字。这意味着该变量是由类的所有实例共享。

所以每次你创建你执行下面的代码类的一个新实例:

addsche.addActionListener(floorPlan);

这增加了另一个的ActionListener到按钮。

static关键字是通常只应使用当您创建类常量变量,它不应该被用于需要要为每个类唯一的组件。

© www.soinside.com 2019 - 2024. All rights reserved.