我的 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,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 永远不会有什么坏处。

您会找到如下信息:

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

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

content.add(button,BorderLayout.PAGE_END);

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

content.add(red);
        
...

content.add(blue);

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

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

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

,

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

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

您创建了一个局部变量,它是 hiding 类成员。因此 static JCheckBox red; 保持为 null,因此下面的 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 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());
    }
}

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

screen capture of running app