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

My JCheckBox program only displays one box. Why is that?

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

代码如下:

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

有什么指点吗?

当您遇到问题时,返回并参考 documentation

您会找到这样的信息:

A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component, and is identified by a corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER. When adding a component to a container with a border layout, use one of these five constants...

当你添加你的按钮时,你这样做:

content.add(button, BorderLayout.PAGE_END);

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

content.add(red);
        
...

content.add(blue);

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

尝试指定要在 BorderLayout 中看到每个复选框的区域。

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

我觉得您的 Swing 编程需要一些指导。我已经重写了你的 CMIS242WK4DonnersonAReply class。代码如下。但首先要对您问题中的代码发表一些评论。

JCheckBox red = new JCheckBox("You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.");

您已经创建了一个局部变量,它是 hiding class 成员。因此 static JCheckBox red; 保持为空,因此下面的 if 语句将抛出 NullPointerException.

if (red.isSelected() == true) {

顺便说一下,== true 不是必需的。以下就够了。

if (red.isSelected()) {

现在还有一点。

red.setBounds(100,100, 50,50);

由于您使用的是 layout manager,即 BorderLayout,方法 setBounds 将被忽略。布局管理器决定组件在屏幕上的位置。

window.setContentPane(content);

默认情况下,JFrame 的内容窗格是 JPanelBorderLayout,因此无需替换默认内容窗格。

private static class ButtonHandler implements ActionListener

无需创建嵌套 class。只需让 class CMIS242WK4DonnersonAReply 实现 ActionListener 接口。

System.out.println("Follow me");

我认为将控制台包含在 GUI 应用程序中不是一个好主意。我会使用 JOptionPane 向用户显示消息。

static JCheckBox blue;

我认为 JRadioButtonJCheckBox 更适合你的情况。

这是我的代码。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class CMIS242WK4DonnersonAReply implements Runnable, ActionListener {
    private JButton  button;
    private JRadioButton  blue;
    private JRadioButton  red;
    private JFrame  window;

    @Override
    public void actionPerformed(ActionEvent event) {
        if (red.isSelected()) {
            JOptionPane.showMessageDialog(window, "Follow me.");
        }
        else if (blue.isSelected()) {
            JOptionPane.showMessageDialog(window, "Very Well, You may go back to your world");
        }
        else {
            JOptionPane.showMessageDialog(window, "You must make a choice for what pill you will take");
        }
    }

    @Override
    public void run() {
        createAndShowGui();
    }

    private void createAndShowGui() {
        window = new JFrame("Matrix Monologue");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        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.
        window.add(label, BorderLayout.PAGE_START);
        window.add(createCheckBoxes(), BorderLayout.CENTER);
        JPanel buttonPanel = new JPanel();
        button = new JButton("submit");
        button.addActionListener(this);
        buttonPanel.add(button);
        window.add(buttonPanel, BorderLayout.PAGE_END);
        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
    }

    private JPanel createCheckBoxes() {
        JPanel panel = new JPanel();
        BoxLayout layout = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
        panel.setLayout(layout);
        red = new JRadioButton("You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.");
        blue = new JRadioButton("You take the blue pill, the story ends, you wake up in your bed and believe whatever you want to believe.");
        ButtonGroup grp = new ButtonGroup();
        grp.add(red);
        grp.add(blue);
        panel.add(red);
        panel.add(blue);
        return panel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new CMIS242WK4DonnersonAReply());
    }
}

这是我 运行 应用程序的外观。