Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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更改现有组件_Java_Swing_Components - Fatal编程技术网

Java更改现有组件

Java更改现有组件,java,swing,components,Java,Swing,Components,当添加另一个选项时,我试图将组件从JLabel更改为JComboBox,但由于某些原因,面板未更新 SSCCE: public class SwitchComponent { public static void main(String[] args) { JPanel panel = new JPanel(); JComponent component = new JLabel("This is a test"); panel.add

当添加另一个选项时,我试图将组件从
JLabel
更改为
JComboBox
,但由于某些原因,面板未更新

SSCCE:

public class SwitchComponent {
    public static void main(String[] args) { 
        JPanel panel = new JPanel();
        JComponent component = new JLabel("This is a test");

        panel.add(component);

        JComboBox<String> comboBox = new JComboBox<String>();
        comboBox.addItem("Testing..");
        comboBox.addItem("1.. 2.. 3..");

        component = comboBox;

        // I have tried with only one of the below lines and without any also...
        // Doesn't seem to have an effect.
        // I've also tried invoking the below methods on the panel instead.
        component.revalidate();
        component.repaint();

        JOptionPane.showConfirmDialog(null, panel, "Test",
                                      JOptionPane.OK_OPTION,
                                      JOptionPane.PLAIN_MESSAGE);
    }
}
公共类开关组件{
公共静态void main(字符串[]args){
JPanel面板=新的JPanel();
JComponent组件=新的JLabel(“这是一个测试”);
面板。添加(组件);
JComboBox comboBox=新的JComboBox();
comboBox.addItem(“测试…”);
comboBox.addItem(“1..2..3..”);
组件=组合框;
//我只试过下面的一行,没有任何一行。。。
//似乎没有效果。
//我还尝试在面板上调用以下方法。
component.revalidate();
repaint();
showConfirmDialog(空,面板,“测试”,
JOptionPane.OK\u选项,
JOptionPane.普通消息);
}
}
为什么会这样?
面板
不应该引用
组件
,这样对
组件
的任何更改都可以通过
面板
反映出来吗


当组件发生变化时,我真的必须完全重新组装面板吗?

当单击JOptionPane上的“是/否”按钮时,JOptionPane将关闭

我们需要再次将JComboBox添加到面板中,并使用JOptionPane在代码中再次显示面板

试试这个:

public class SwitchComponent {
public static void main(String[] args) { 
    JPanel panel = new JPanel();
    JComponent component = new JLabel("This is a test");

    panel.add(component);

    JOptionPane.showConfirmDialog(null, panel, "Test",
                                  JOptionPane.OK_OPTION,
                                  JOptionPane.PLAIN_MESSAGE);

    panel.remove(component);

    JComboBox<String> comboBox = new JComboBox<String>();
    comboBox.addItem("Testing..");
    comboBox.addItem("1.. 2.. 3..");
    panel.add(comboBox);

    // I have tried with only one of the below lines and without any also...
    // Doesn't seem to have an effect.
    // I've also tried invoking the below methods on the panel instead.
    panel.revalidate();
    panel.repaint();

    JOptionPane.showConfirmDialog(null, panel, "Test",
            JOptionPane.OK_OPTION,
            JOptionPane.PLAIN_MESSAGE);

}
}
公共类开关组件{
公共静态void main(字符串[]args){
JPanel面板=新的JPanel();
JComponent组件=新的JLabel(“这是一个测试”);
面板。添加(组件);
showConfirmDialog(空,面板,“测试”,
JOptionPane.OK\u选项,
JOptionPane.普通消息);
面板。移除(组件);
JComboBox comboBox=新的JComboBox();
comboBox.addItem(“测试…”);
comboBox.addItem(“1..2..3..”);
panel.add(组合框);
//我只试过下面的一行,没有任何一行。。。
//似乎没有效果。
//我还尝试在面板上调用以下方法。
panel.revalidate();
panel.repaint();
showConfirmDialog(空,面板,“测试”,
JOptionPane.OK\u选项,
JOptionPane.普通消息);
}
}
也就是说,标签可以是三个系列中的第二个,当组件更改为组合框时,我希望组合框保持第二个。因此,我试图更改引用


使用一个。它将在同一位置更换一个部件。

在这种情况下,如果面板中有多个部件,则会导致部件订购问题。也就是说,标签可以是三个系列中的第二个,当组件更改为组合框时,我希望组合框保持第二个。因此,我试图更改引用。