有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JCheckBox未出现

我已经检查了everwhere以获得修复,但没有任何东西可以让我的复选框出现。我将其添加到面板,并将面板添加到窗口。按钮出现了,所以复选框一定有问题。这是我的代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainApplication {

    public static Toolkit tk = Toolkit.getDefaultToolkit();

    public static void main(String[] args) {
        MainApplication instance = new MainApplication();
        instance.start();
    }

    private JFrame window;
    private JPanel mainPanel;
    private JPanel contingencyPanel;

    private JButton applyButton = new JButton("Apply Changes");
    private JCheckBox autoRedLightBox = new JCheckBox("Red Light");
    private JCheckBox autoYellowLightBox = new JCheckBox("Yellow Light");
    private JCheckBox autoGreenLightBox = new JCheckBox("Green Light");
    private JCheckBox autoBlueLightBox = new JCheckBox("Blue Light");

    public void start() {
        window = new JFrame("Main Control Window");
        mainPanel = new JPanel();
        contingencyPanel = new JPanel();

        window.setSize(1280, 720);
        window.setResizable(false);
        window.setFocusable(true);
        window.setFocusTraversalKeysEnabled(true);
        int screenWidth = (int)tk.getScreenSize().getWidth();
        int screenHeight = (int)tk.getScreenSize().getHeight();
        window.setLocation((screenWidth/2)-(window.getWidth()/2), (screenHeight/2)-(window.getHeight()/2));
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainPanel.setLayout(null);
        contingencyPanel.setLayout(null);

        applyButton.setToolTipText("Changes will be applied to the arduino.");
        applyButton.setSize(new Dimension(120, 30));
        applyButton.setLocation(new Point((1280-120)-10, (720-56)-10));

        autoRedLightBox.setSelected(true);
        autoRedLightBox.setLocation(new Point(30, 30));
        autoRedLightBox.setMnemonic(KeyEvent.VK_R);

        mainPanel.add(applyButton);
        mainPanel.add(autoRedLightBox, BorderLayout.CENTER);

        window.add(mainPanel);
        window.setVisible(true);
    }
}

期望的结果:

DesiredOutcome


共 (2) 个答案

  1. # 1 楼答案

    这非常适合GridLayout

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    public class ButtonsAndChecks {
    
        private JComponent ui = null;
    
        ButtonsAndChecks() {
            initUI();
        }
    
        public void initUI() {
            if (ui!=null) return;
    
            // adjust last two numbers to need..
            ui = new JPanel(new GridLayout(0,5,20,20));
            ui.setBorder(new EmptyBorder(4,4,4,4));
    
            // adjust numbers to need..
            for (int i=1; i<26; i++) {
                ui.add(new JButton("Button " + i));
            }
            // adjust numbers to need..
            for (int i=1; i<26; i++) {
                ui.add(new JCheckBox("Check " + i, i%2==0));
            }
        }
    
        public JComponent getUI() {
            return ui;
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception useDefault) {
                    }
                    ButtonsAndChecks o = new ButtonsAndChecks();
    
                    JFrame f = new JFrame(o.getClass().getSimpleName());
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setLocationByPlatform(true);
    
                    f.setContentPane(o.getUI());
                    f.pack();
                    f.setMinimumSize(f.getSize());
    
                    f.setVisible(true);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }
    
  2. # 2 楼答案

    阅读一些关于Swing中的布局经理的信息。每个面板都需要一个布局来容纳组件,如果不为JPanel指定布局,它将使用默认布局(FlowLayout)。 问题出在这一行,当您为布局设置null时

        mainPanel.setLayout(null);
    

    只需对其进行注释,您就会在表单上看到按钮。 此外,要将表单放在屏幕中央,可以调用此方法

        window.setLocationRelativeTo(null);