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

时间:2013-12-03 05:20:14

标签: java swing jframe

所以我有两个窗口,使用两个单独的类。我的主要分层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布尔我正计划做其他事情,但如果我能得到一个返回的布尔值,我可以写得那么容易。有任何想法吗?顺便说一句,我真的很感激每个人不断阅读像我这样的问题,很高兴知道有人真正关心。

3 个答案:

答案 0 :(得分:0)

两个窗口都没有代码中的链接。您需要在reference窗口中拥有Child主窗口,以便在Checkbox方法触发期间设置主窗口的actionPerformed()属性。最好的方法是在主窗口和子窗口之间创建Delegate类,这将执行Checkbox启用/禁用操作。除此之外,您只有frame级别的Class引用,而所有其他组件都在method级别。要更改窗口中组件的属性,您需要在Class级别声明它们。请为窗口的所有组件创建settersgetters。那真的会有所帮助。希望这有帮助

答案 1 :(得分:0)

为什么不为复选框的状态(显示或隐藏)采用静态变量 将通过类名访问,它将显示标签可见性的当前状态。

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();
    }
}

}

public class ControlWindow extends ABC {



/**
 * 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() {
    try{
    frame = new JFrame();
    frame.setBounds(700, 100, 455, 231);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    final JCheckBox chckbxSnow = new JCheckBox("Snow");
     frame.add(chckbxSnow);
    chckbxSnow.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
           if(chckbxSnow.isSelected()){
               setSelected(true);
           }else{
               setSelected(false);
           }
        }
    });
}catch(Exception Ex){
      Ex.printStackTrace();
}
}
}

答案 2 :(得分:0)

帮助您解决问题的示例代码。在示例中,如果选择MainFrame's JCheckBox,则还会选择OtherFrame's 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
                }
            });

        }
    }