在Java中从另一个窗口检查和更改JLabel状态

在Java中从另一个窗口检查和更改JLabel状态,java,swing,jframe,Java,Swing,Jframe,所以我有两个窗口,利用两个独立的类。我的主窗口有分层的jlabel,其中包含图像,我希望能够使用setVisible命令使用第二个窗口中的复选框来回切换这些图像。我使用windowbuilder创建窗口和可视元素,因此代码让我有点困惑。我尝试过创建setter和getter,但eclipse一直告诉我“标记“boolean”上的语法错误,@expected”,这并没有什么帮助。也许我已经站得太久了,但我不知道我是否把得手和二传手放在了正确的位置。这是我的主窗口的一些代码 public class

所以我有两个窗口,利用两个独立的类。我的主窗口有分层的jlabel,其中包含图像,我希望能够使用setVisible命令使用第二个窗口中的复选框来回切换这些图像。我使用windowbuilder创建窗口和可视元素,因此代码让我有点困惑。我尝试过创建setter和getter,但eclipse一直告诉我“标记“boolean”上的语法错误,@expected”,这并没有什么帮助。也许我已经站得太久了,但我不知道我是否把得手和二传手放在了正确的位置。这是我的主窗口的一些代码

public class ChristmasTree {

private JFrame frame;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ChristmasTree window = new ChristmasTree();
                window.frame.setVisible(true);
                ControlWindow.createWindow();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });



}



/**
 * Create the application.
 */
public ChristmasTree() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(200, 50, 500, 625);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLayeredPane layeredPane = new JLayeredPane();
    GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
    groupLayout.setHorizontalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addComponent(layeredPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
    );
    groupLayout.setVerticalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(groupLayout.createSequentialGroup()
                .addGap(1)
                .addComponent(layeredPane, GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE))
    );

    JLayeredPane backGround = new JLayeredPane();
    backGround.setBounds(0, 0, 484, 586);
    layeredPane.add(backGround);

    JLayeredPane treePane = new JLayeredPane();
    layeredPane.setLayer(treePane, 0);
    treePane.setBounds(0, 0, 484, 586);
    layeredPane.add(treePane);

    JLabel treeLabel = new JLabel("");
    treePane.setLayer(treeLabel, 0);
    treeLabel.setIcon(new ImageIcon(ChristmasTree.class.getResource("/resources/tree.png")));
    treeLabel.setBounds(0, 0, 484, 586);
    treePane.add(treeLabel);



    JLayeredPane decorationsPane = new JLayeredPane();
    layeredPane.setLayer(decorationsPane, 2);
    decorationsPane.setBounds(0, 0, 484, 586);
    layeredPane.add(decorationsPane);
    decorationsPane.setVisible(true);


    JLabel starLabel = new JLabel("");
    starLabel.setIcon(new ImageIcon(ChristmasTree.class.getResource("/resources/star.png")));
    starLabel.setBounds(0, 0, 484, 586);
    decorationsPane.add(starLabel);
    starLabel.setVisible(false);
例如,这里的starLabel-我想使用一个复选框将其从visible==true/false来回切换。我不知道这是否有帮助,但这是第二个窗口的部分代码

public class ControlWindow extends ChristmasTree {

private JFrame frame;
private boolean isSnowSelected = false;


/**
 * Launch the application.
 */
public static void createWindow() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ControlWindow window = new ControlWindow();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public ControlWindow() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(700, 100, 455, 231);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ChristmasTree myChristmasTree = new ChristmasTree();

    JLabel chooseLabel = new JLabel("");
    chooseLabel.setIcon(new ImageIcon(ControlWindow.class.getResource("/resources/message.png")));

    final JCheckBox chckbxSnow = new JCheckBox("Snow");
    chckbxSnow.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            if (isSnowSelected == false)
            {   

            }
            else if (isSnowSelected == true)
            {

            }

            if (isSnowSelected == true)
            {
                isSnowSelected = false;
            }
            else
            {
                isSnowSelected = true;
            }
        }
        });

整个isSnowSelected boolean我正计划做其他事情,但是如果我能得到一个返回的boolean,我就可以写得容易得多。有什么想法吗?顺便说一句,我真的很感谢大家不断阅读像我这样的问题,很高兴知道有人真正关心我。

两个窗口的代码中都没有链接。在启动
actionPerformed()
方法期间,您需要在
子窗口中有主窗口的
引用
,以设置主窗口的
复选框
属性。最好的方法是在主窗口和子窗口之间创建一个
Delegate
类,该类将执行
复选框的启用/禁用操作。除此之外,在
级别中只有
框架
引用,所有其他组件都在
方法
级别。要更改窗口中组件的属性,需要在
级别声明它们。请为窗口的所有组件创建
setter
getter
。那真的很有帮助。希望这有帮助

为什么不选择一个静态变量作为复选框的状态(显示或隐藏) 这将是按类名访问的,它将显示标签可见性的当前状态

public class ABC {

public JFrame frame;
private static boolean isSnowSelected = false;
private JLayeredPane layeredPane;
JLayeredPane backGround;
JLayeredPane treePane;
 JLayeredPane decorationsPane;
 JLabel starLabel;
JLabel treeLabel;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ABC window = new ABC();
                window.frame.setVisible(true);
                ABC window1 = new ABC();
                window1.frame.setVisible(true);
                ControlWindow.createWindow();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });



}

/**
 * Create the application.
 */
public ABC() {
    initialize();
   runth();
}
public void runth(){
    Thread th = new Thread() {
                    public void run() {
                        while (true) {
                            if (isSnowSelected) {
                                treeLabel.setVisible(true);
                                starLabel.setVisible(false);
                            } else {
                                treeLabel.setVisible(false);
                                starLabel.setVisible(true);
                            }
                        }

                    }
                };
                th.start();
}
public static boolean getSelected() {
    return isSnowSelected;
}

public static void setSelected(boolean value) {
    isSnowSelected = value;
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    try {
        frame = new JFrame();
        frame.setBounds(200, 50, 500, 625);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        layeredPane = new JLayeredPane();
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                .addComponent(layeredPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE));
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                .addGap(1)
                .addComponent(layeredPane, GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)));

        backGround = new JLayeredPane();
        backGround.setBounds(0, 0, 484, 586);
        layeredPane.add(backGround);

        treePane = new JLayeredPane();
        layeredPane.setLayer(treePane, 0);
        treePane.setBounds(0, 0, 484, 586);
        layeredPane.add(treePane);

         treeLabel = new JLabel("");
        treePane.setLayer(treeLabel, 0);
        treeLabel.setIcon(new ImageIcon(ABC.class.getResource("/resources/tree.png")));
        treeLabel.setBounds(0, 0, 484, 586);
        treePane.add(treeLabel);



        decorationsPane = new JLayeredPane();
        layeredPane.setLayer(decorationsPane, 2);
        decorationsPane.setBounds(0, 0, 484, 586);
        layeredPane.add(decorationsPane);
        decorationsPane.setVisible(true);


         starLabel = new JLabel("");
        starLabel.setIcon(new ImageIcon(ABC.class.getResource("/resources/star.png")));
        starLabel.setBounds(0, 0, 484, 586);
        decorationsPane.add(starLabel);
        starLabel.setVisible(true);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
}


帮助您解决问题的示例代码。在此示例中,如果选择了
大型机的
JCheckBox
,则它也会将
其他帧的
JCheckBox
设置为选中

public class MainFrame extends JFrame {

        private OtherFrame c = null;
        JCheckBox checkbox = new JCheckBox();

        public MainFrame() {
            addActionListener();
        }

        public void setC(OtherFrame c) {
            this.c = c;
        }

        private void addActionListener() {
            checkbox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //performs operation
                    if (checkbox.isSelected()) {
                        c.checkbox.setSelected(true);
                    }

                }
            });
        }
    }

    public class OtherFrame extends JFrame {

        JCheckBox checkbox = new JCheckBox();

        public OtherFrame() {
            addActionListener();
        }

        private void addActionListener() {
            checkbox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //performs operation
                }
            });

        }
    }

考虑使用A来切换。基本上,您需要对子窗口中的主窗口进行引用,然后需要子窗口将它的愿望传递给子窗口。我可以考虑的是使用某种类型的模型,这两个窗口都有参考。主窗口将侦听对模型的更改,而子窗口将告诉它需要进行哪些更改。这将使您的UI解耦,并防止子窗口使用主窗口执行不应该执行的操作
public class MainFrame extends JFrame {

        private OtherFrame c = null;
        JCheckBox checkbox = new JCheckBox();

        public MainFrame() {
            addActionListener();
        }

        public void setC(OtherFrame c) {
            this.c = c;
        }

        private void addActionListener() {
            checkbox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //performs operation
                    if (checkbox.isSelected()) {
                        c.checkbox.setSelected(true);
                    }

                }
            });
        }
    }

    public class OtherFrame extends JFrame {

        JCheckBox checkbox = new JCheckBox();

        public OtherFrame() {
            addActionListener();
        }

        private void addActionListener() {
            checkbox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //performs operation
                }
            });

        }
    }