我的JCheckBox程序只显示一个框。这是为什么?

我正在尝试向该程序添加另一个复选框,但由于某种原因,当我运行该程序时它不会显示。仅显示蓝色药丸的复选框。我试图添加一些东西或改变程序的结构方式,但到目前为止我所做的一切都没有帮助。

代码如下:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class CMIS242WK4DonnersonAReply {
    
    static JCheckBox red;
    static JCheckBox blue;
    static JButton button;
    
 
    public CMIS242WK4DonnersonAReply() {
        
        
        button = new JButton("submit"); // Creates submit button 
    widget
        ButtonHandler listener = new ButtonHandler(); // Creates the handler for the button.
        button.addActionListener((ActionListener) listener); // adds the handler to the button widget
        
        
        JPanel content = new JPanel(); // "container"
        content.setLayout(new BorderLayout());
        content.add(button, BorderLayout.PAGE_END);// places submit button at the bottom of panel.
        
        
        JLabel label = new JLabel("At last. Welcome, Neo. As you no doubt have guessed, I am Morpheus.  This is your last chance. After this there is no turning back."); // Label in frame.
        content.add(label, BorderLayout.NORTH);// places label at the top of the screen.

        //Creating Check Boxes
        JCheckBox red = new JCheckBox("You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.");
        red.setBounds(100,100, 50,50);  
        content.add(red);
        
        JCheckBox blue = new JCheckBox("You take the blue pill, the story ends, you wake up in your bed and believe whatever you want to believe. ");
        blue.setBounds(100,100, 50,50);  
        content.add(blue);
        
        //Adding Frame
        JFrame window = new JFrame("Matrix Monologue"); // JFrame = Window
        window.setContentPane(content);
        window.setSize(750,200); // Length, Height
        window.setLocation(200,200); // X/Y "OF THE ENTIRE FRAME" Not the contents
        window.setVisible(true); // makes window visible
       

    }
    
    // Method handles what happens when button is pressed.
    private static class ButtonHandler implements ActionListener{
        public void actionPerformed1(ActionEvent e) {
            
            
            // Checks if which pill was selected and responds to user depending on their action.
            if (red.isSelected() == true) {
                System.out.println("Follow me");
                System.out.println();
            }
            if (blue.isSelected() == true) {
                System.out.println("Very Well, You may go back to your world");
                System.out.println();
            }
            else
                System.out.println("You must make a choice for what pill you will take");
            System.exit(0); //closes program
            
            
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            
        }
    }
    
    
    // Main/driver method that runs everything.
    public static void main(String[] args) {

        CMIS242WK4DonnersonAReply matrixMonologue= new CMIS242WK4DonnersonAReply();
        
    }
}

任何指针?

回答

当您遇到问题时,返回并查阅文档永远不会有什么坏处。

你会找到这样的信息:

边框布局布局一个容器,排列和调整其组件的大小以适应五个区域:北、南、东、西和中心。每个区域可能包含不超过一个组件,并由相应的常量标识:NORTH、SOUTH、EAST、WEST 和 CENTER。将组件添加到具有边框布局的容器时,请使用以下五个常量之一...

添加按钮时,请执行以下操作:

content.add(button, BorderLayout.PAGE_END);

但是,当需要添加复选框时,您可以这样做:

content.add(red);
        
...

content.add(blue);

你看到缺少什么了吗?我敢打赌,您只会看到蓝色复选框,因为您将它添加到(或只是置换)红色复选框之上。请记住,文档说“每个区域可能包含不超过一个组件......”

尝试指定您希望看到每个复选框的 BorderLayout 区域。

如果您希望它们出现在同一区域,请将它们放在自己的 JPanel 中,并将它们布置在 NORTH 和 SOUTH 或 EAST 和 WEST,然后将该复选框面板添加到您content希望它们出现的区域的面板中。


以上是我的JCheckBox程序只显示一个框。这是为什么?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>