Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTextField的setText(字符串t)无法更改textfield的内容_Java_Swing_Jtextfield_Gridbaglayout_Settext - Fatal编程技术网

Java JTextField的setText(字符串t)无法更改textfield的内容

Java JTextField的setText(字符串t)无法更改textfield的内容,java,swing,jtextfield,gridbaglayout,settext,Java,Swing,Jtextfield,Gridbaglayout,Settext,源代码附在下面。两个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.ActionL

源代码附在下面。两个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();
            }
        });

    }
}
包ztio.gui.updateGUI;
导入java.awt.Container;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
导入javax.swing.BorderFactory;
导入javax.swing.Box;
导入javax.swing.BoxLayout;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JDialog;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入javax.swing.border.TitledBorder;
公共类SimpleSettingDialog扩展JDialog{
私人最终整数最大值为10;
私有最终整数最大值等于10;
专用JPanel对话框窗格;
私有jComboxTestCaseCombo;
私有jComboxTestCaseItemCombo;
私有JComboBox[]判断操作组合数组=新JComboBox[MAX_THRES_NUM];
私有JComboBox[]thresTypeComboArray=新JComboBox[MAX_THRES_NUM];
私有字符串[]判断操作字符串;
私有字符串[]thresTypeStrings;
私有列表testBtnList;
私人JTextField showTargetChoice;
私有JTextField展示测试用例;
公共SimpleSettingDialog(JFrame父对象,布尔isModal){
//TODO自动生成的构造函数存根
超级(父级,isModal);

字符串[]判断={“>”,“
createTestCaseItemPanel()
执行两次。从
createTestCasePanel()
执行一次。第二次在构造函数的这一行:

JPanel testItemPanel = createTestCaseItemPanel();
第二个调用创建了一个未使用的局部变量
testTempanel
。但是,这会覆盖一些成员,其中一个成员是
showTargetChoice
,它现在指向一个不可见的字段


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

createTestCaseItemPanel()
将执行两次。一旦从
createTestCasePanel()
执行该调用,则在构造函数中的这一行执行第二次:

JPanel testItemPanel = createTestCaseItemPanel();
第二个调用创建了一个未使用的局部变量
testTempanel
。但是,这会覆盖一些成员,其中一个成员是
showTargetChoice
,它现在指向一个不可见的字段


要解决此问题,只需去掉第二个调用。

考虑创建一个-其中有许多代码与问题无关,但需要花费大量时间考虑创建一个-其中有许多代码与问题无关,但需要花费大量时间。我感谢您帮助我解决此问题。谢谢t、 我很感谢你帮我弄明白。非常感谢。