简体   繁体   English

数组中的JButton不会更新文本

[英]JButton in array wont update text

I've created an array of buttons, added them to my frame and (inefficiently) created action listeners for each of them which open a JOptionPane to take an input and add it to a String array, yet the text on the array button does not update after the popup is closed, while a button not part of an array updates its text fine. 我创建了一个按钮数组,将它们添加到我的框架中,并(低效地)为它们中的每一个创建了动作监听器,这些监听器打开了一个JOptionPane以接受输入并将其添加到String数组中,但是数组按钮上的文本却没有弹出窗口关闭后更新,而不是数组一部分的按钮可以更新其文本。

The string array grabs the data from the JOptionPane fine, it just wont update the button's caption. 字符串数组从JOptionPane处获取数据,它只是不会更新按钮的标题。

In my full program I'm writing ar_str_vals to a .xml file, and it can properly save and load the array, and surprisingly the array buttons properly set their text but only at the beginning of my program. 在我的完整程序中,我正在将ar_str_vals写入.xml文件,它可以正确保存和加载数组,令人惊讶的是,数组按钮仅在程序开始时就可以正确设置其文本。

package wtf;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;

    public class Wtf extends JFrame{
        JButton[] ar_btn_vals = new JButton[2];
        String[] ar_str_vals = new String[2];
        public Wtf(){
        super("Title");
        setLayout(null);
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        constructor();
        actionlisteners();
        }

public void constructor(){
    for (int x = 0;x<=1;x++){
        ar_btn_vals[x] = new JButton();
        ar_btn_vals[x].setText(ar_str_vals[x]);
        ar_btn_vals[x].setBounds(5,(100 * x)+20, 100,40);
        ar_btn_vals[x].setVisible(true);
        add(ar_btn_vals[x]);
        System.out.println(ar_str_vals[x]);
    }

}

public void actionlisteners(){
    for (int x=0;x<=1;x++){
        switch (x){
        case 0:
                ar_btn_vals[0].addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        ar_btn_vals0ActionPerformed(evt);
                    }
                });
            break;
            case 1:
                ar_btn_vals[1].addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        ar_btn_vals1ActionPerformed(evt);
                    }
                });
            break;
        }
    }
}

private void ar_btn_vals0ActionPerformed(java.awt.event.ActionEvent evt) {
    JFrame frm_val0change = new JFrame();
    String newval = JOptionPane.showInputDialog(frm_val0change, "Enter new Button 1 Value");
    ar_str_vals[0] = newval;  
    constructor();
}

private void ar_btn_vals1ActionPerformed(java.awt.event.ActionEvent evt) {
    JFrame frm_val1change = new JFrame();
    String newval = JOptionPane.showInputDialog(frm_val1change, "Enter new Button 2 Value");
    ar_str_vals[1] = newval;  
    constructor();
}

public static void main(String[] args) {
    Wtf frame = new Wtf();
}
    }

I'm aware that this isn't as efficient as it could be, but I've got limited time to finish this and I have absolutely no idea why this isn't working properly. 我知道这样做效率不高,但是完成此工作的时间有限,我完全不知道为什么它无法正常工作。

This is also my first time asking a question, so please have mercy if I've formatted anything wrong. 这也是我第一次问一个问题,因此,如果我格式化错误,请留意。

正如MadProgrammer所说,我要做的就是更新动作侦听器中的按钮文本,而不是再次调用构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM