从ActionListener变量创建Jbuttons

时间:2015-04-21 14:25:18

标签: java jbutton actionlistener

这里的任务是从ActionListener中的String创建JButton,但是我们需要一种刷新GUI面板的方法,因此它知道GUI中的按钮创建器现在有变量。我感觉按钮创建者必须在ActionListener中,并且在ActionListener中有一个像repaint()或removeAll这样的命令。

public JButton[] turneringer = null;
JButton AntallTurneringer = new JButton("number of buttons");

JMenuBar meny = new JMenuBar();
JMenu fil = new JMenu("somthing");
JMenuItem angre = new JMenuItem("deleate on button");
JMenuItem angre2 = new JMenuItem("deleate all buttons");

int d;
int i;

public GUI(){
    this.setTitle("somthing");
    this.setSize(500,500);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());

    this.setJMenuBar(meny);
    meny.add(fil);
    fil.add(angre2);
    fil.add(angre);
    angre2.addActionListener(this);
    angre.addActionListener(this);

    AntallTurneringer.addActionListener(this);
    this.add(AntallTurneringer);
    AntallTurneringer.setVisible(true);

    if(d > 0){
    turneringer = new JButton[d];
    for(i = 0; i < d; i++){
        turneringer[d] = new JButton();
        turneringer[d].addActionListener(this);
        turneringer[d].setText("Turnering "+(i+1));
        turneringer[d].setVisible(true);
        this.add(turneringer[d]);
    }}
    this.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent arg0) {
    if(arg0.getSource().equals(AntallTurneringer)){
        String tu = JOptionPane.showInputDialog(null, "number of buttons");
        d = Integer.parseInt(tu);

    }
}

2 个答案:

答案 0 :(得分:1)

您可以为按钮使用单独的面板。会简化整个事情。

private JPanel buttonPnl;

public void actionPerformed(ActionEvent e){
    buttonPnl.invalidate();

    buttonPnl.clear();

    //create the new buttons

    buttonPnl.validate();
    buttonPnl.repaint();
}    

答案 1 :(得分:0)

我已经解决了,没有必要重新粉刷或清除任何东西。代码中的第一个问题是我在数组中使用了[d],而不是[i]。第二个问题是for循环的放置。这是。

下的工作代码
public JButton[] turneringer = null;
JButton AntallTurneringer = new JButton("Velg antall turneringer");

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();

int d;

public GUI(){
    this.setTitle("Squash Turnering");
    this.setLayout(new GridLayout());
    this.setSize(500,500);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    panel1.setBackground(Color.BLACK);
    panel2.setBackground(Color.RED);

    AntallTurneringer.addActionListener(this);
    AntallTurneringer.setVisible(true);

    panel1.add(AntallTurneringer);
    add(panel1);

    add(panel2);
    panel2.setVisible(false);

    this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
    if(arg0.getSource().equals(AntallTurneringer)){
        String tu = JOptionPane.showInputDialog(null, "number of buttons you want to add");
        d = Integer.parseInt(tu);

        turneringer = new JButton[d];
        for(int i = 0; i < d; i++){
            turneringer[i] = new JButton();
            turneringer[i].addActionListener(this);
            turneringer[i].setText("Turnering "+(i+1));
            turneringer[i].setVisible(true);
            turneringer[i].setSize(100, 100);
            panel2.add(turneringer[i]);
        }
        panel1.setVisible(false);
        panel2.setVisible(true);
    }
}}