java - JTextField 的 setText(String t) 无法更改文本字段的内容

标签 java swing jtextfield gridbaglayout settext

The Picture show the problem setting dialog. The dialog's first JTextfield works fine with setText(String t) method when change the first JComboBox selected Item, but the second JTextfield's setText(String t) method doesn't change the content of itself's when change the second JComboBox seleted item.

下面附有源代码。两个 JTextField 的构造方式相同。但我不知道为什么第二个的 setText(String) 方法不起作用。

package ztio.gui.updateGUI;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;



public class SimpleSettingDialog extends JDialog {
    private final int MAX_THRES_NUM = 10;
    private final int MAX_VALUE_THRES_NUM = 10;
    private JPanel dialogPane;
    private JComboBox testCaseCombo;
    private JComboBox testCaseItemCombo;
    private JComboBox[] judgeOperationComboArray = new JComboBox[MAX_THRES_NUM];
    private JComboBox[] thresTypeComboArray = new JComboBox[MAX_THRES_NUM];
    private String[] judgeOperationStrings;
    private String[] thresTypeStrings;
    private List<JButton> testBtnList;
    private JTextField showTargetChoice;
    private JTextField showTestCase;


    public SimpleSettingDialog(JFrame parent, boolean isModal) {
        // TODO Auto-generated constructor stub
        super(parent, isModal);
        String[] judges = {">", "<", "="};
        String[] types = {"int", "float", "string"};
        judgeOperationStrings = judges;
        thresTypeStrings = types;


        dialogPane = (JPanel) getContentPane();
        dialogPane.setLayout(new GridBagLayout());
        TitledBorder titled = BorderFactory.createTitledBorder("Config Thresholds");
        dialogPane.setBorder(titled);

        JPanel testCasePanel = createTestCasePanel();
         GridBagConstraints c = new GridBagConstraints();
         c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.weighty = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        dialogPane.add(testCasePanel, c);
        c.fill = GridBagConstraints.BOTH;
        c.ipady = 0;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;

        JPanel testItemPanel = createTestCaseItemPanel();

        JPanel debug = new JPanel();
        dialogPane.add(debug, c);
    }

    private JPanel createThresListPanel() {
        JPanel thresListPanel = new JPanel();
        thresListPanel.setLayout(new BoxLayout(thresListPanel, BoxLayout.Y_AXIS));
        for(int i = 0; i < 2; ++i){
            JPanel thresPanel = createThresPanel(i);
            thresListPanel.add(thresPanel);
        }
        return thresListPanel;
    }

    private JPanel createThresPanel(int i) {
        // TODO Auto-generated method stub
        JPanel thresPanel = new JPanel(new GridBagLayout());
        TitledBorder titled = BorderFactory.createTitledBorder("The " + (((i+1)==1)?"1st":((i+1)==2?"2nd":((i+1==3?"3rd":((i+1)+"th"))))) + " Threshold" );
        thresPanel.setBorder(titled);
        JComboBox thresTypeCombo = new JComboBox(thresTypeStrings);
        thresTypeComboArray[i] = thresTypeCombo;
        thresTypeCombo.setSelectedItem("string");
        JComboBox judgeOperationCombo = new JComboBox(judgeOperationStrings);
        judgeOperationComboArray[i] = judgeOperationCombo;
        judgeOperationCombo.setSelectedItem("=");
        JLabel label1 = new JLabel("Threshold's type:");
        JLabel label2 = new JLabel("Judge Operation:");

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        thresPanel.add(label1, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 0;
        thresPanel.add(thresTypeCombo, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;
        thresPanel.add(label2, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 1;
        thresPanel.add(judgeOperationCombo, c);

        return thresPanel;
    }

    private JPanel createTestCaseItemPanel() {
        // TODO Auto-generated method stub
        String[] testItemNameArray = {"volume", "brightness"};
        testCaseItemCombo = new JComboBox(testItemNameArray);

        testCaseItemCombo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JComboBox cb = (JComboBox)e.getSource();
                String caseItemName = (String)cb.getSelectedItem();
                String caseName = (String)testCaseCombo.getSelectedItem();
                String targetString = caseName + "->" + caseItemName;
                System.out.println(targetString);
                System.out.println("target text field: " + showTargetChoice.getText());
                showTargetChoice.setText(targetString);
            }
        });

        TitledBorder titled;

        titled = BorderFactory.createTitledBorder("Test Case Item");
        JPanel comp = new JPanel(new GridBagLayout());
        comp.setBorder(titled);

        JButton btnAddTestCaseItem = new JButton("ADD");
        btnAddTestCaseItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });

        JButton btnDelTestCaseItem = new JButton("DEL");
        btnDelTestCaseItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });



         GridBagConstraints c = new GridBagConstraints();
         c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        comp.add(testCaseItemCombo, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 0;
        comp.add(btnAddTestCaseItem, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 2;
        c.gridy = 0;
        comp.add(btnDelTestCaseItem, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.gridwidth = 1;
        c.gridx = 3;
        c.gridy = 0;
        comp.add(new JPanel(), c);

        //Row 2
        JLabel labelRegexp = new JLabel("target string:");
        showTargetChoice = new JTextField();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;
        comp.add(labelRegexp, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 3;
        c.gridx = 1;
        c.gridy = 1;
        comp.add(showTargetChoice, c);

        JPanel thresListPanel = createThresListPanel();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 4;
        c.gridx = 0;
        c.gridy = 2;
        comp.add(thresListPanel, c);

        return comp;
    }

    private JPanel createTestCasePanel() {
        // TODO Auto-generated method stub
        String[] testCaseArray = {"Light", "Voice"};
        testCaseCombo = new JComboBox(testCaseArray);
        testCaseCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JComboBox cb = (JComboBox)e.getSource();
                String caseName = (String)cb.getSelectedItem();
                System.out.println(caseName);
                showTestCase.setText(caseName);
            }
        });
        TitledBorder titled;

        titled = BorderFactory.createTitledBorder("Test Case");
        JPanel comp = new JPanel(new GridBagLayout());
        comp.setBorder(titled);

        JButton btnAddTestCase = new JButton("ADD");
        btnAddTestCase.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
            }
        });

        JButton btnDelTestCase = new JButton("DEL");
        btnDelTestCase.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });

         GridBagConstraints c = new GridBagConstraints();
         c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        comp.add(testCaseCombo, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 0;
        comp.add(btnAddTestCase, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 2;
        c.gridy = 0;
        comp.add(btnDelTestCase, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.gridwidth = 1;
        c.gridx = 3;
        c.gridy = 0;
        comp.add(new JPanel(), c);

        JLabel label = new JLabel("selected test case:");
        showTestCase = new JTextField();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;
        comp.add(label, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.gridwidth = 3;
        c.gridx = 1;
        c.gridy = 1;
        comp.add(showTestCase, c);

        JPanel testCaseItemPanel = createTestCaseItemPanel();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 4;
        c.gridx = 0;
        c.gridy = 2;
        comp.add(testCaseItemPanel, c);



        return comp;
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("SimpleSettingDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton btn = new JButton("open setting dialog");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                SimpleSettingDialog simpleSettingDialog = new SimpleSettingDialog(frame, true);
                simpleSettingDialog.pack();
                simpleSettingDialog.setVisible(true);
            }
        });
        frame.getContentPane().add(btn);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });

    }
}

最佳答案

createTestCaseItemPanel() 执行两次。一旦从 createTestCasePanel() 执行。第二次在构造函数中的这一行:

JPanel testItemPanel = createTestCaseItemPanel();

第二次调用创建了一个未使用的局部变量testItemPanel。然而,这会覆盖一些成员,其中之一是 showTargetChoice,它现在指向一个不可见的字段。

要解决此问题,只需删除第二个调用即可。

关于java - JTextField 的 setText(String t) 无法更改文本字段的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38835084/

相关文章:

java - 为什么日期在不同时区以相同的毫秒数变化?

java - 将 Button 更改为 JButton 无法修复它(((

java - JButton 无法识别 JRadioButton 是否被选择

java - 如何使 JTextField 中的光标跨越 FlowLayout 中的多行?

java - 增加 JTextField 组件的高度

java - 如何获取 Tomcat 的管理员用户和密码以访问 Servlets 列表

java - Android:清除 Activity 堆栈

java - Android:seekbar.setProgress() 给出 Nullpointer 异常

java - 从 JEditorPane 打印

java - 无法在 JTextfield 中键入或删除文本