gpt4 book ai didi

java - JFrame - 将复选框添加到屏幕

转载 作者:行者123 更新时间:2023-12-01 22:28:11 25 4
gpt4 key购买 nike

我想要一个显示 5 个不同复选框的 JFrame。应该能够选择多个复选框。此代码仅读取 ExchangeCard1 行并忽略所有其他复选框。当你运行它时,你将只有一个以“A”为字符的复选框。

JCheckBox ExchangingCard1 = new JCheckBox("A");
JCheckBox ExchangingCard2 = new JCheckBox("B");
JCheckBox ExchangingCard3 = new JCheckBox("C");
JCheckBox ExchangingCard4 = new JCheckBox("D");
JCheckBox ExchangingCard5 = new JCheckBox("E");

JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setTitle("Exchange.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(ExchangingCard1);
frame.setVisible(true);
frame.add(ExchangingCard2);
frame.setVisible(true);
frame.add(ExchangingCard3);
frame.setVisible(true);
frame.add(ExchangingCard4);
frame.setVisible(true);
frame.add(ExchangingCard5);
frame.setVisible(true);

最佳答案

将复选框放入 JPanel 中,然后将 JPanel 放入 JFrame 中。

这是一个可运行的示例。

package com.ggl.testing;

import java.awt.BorderLayout;

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

public class CheckBoxTest2 implements Runnable {

private JFrame frame;

@Override
public void run() {
frame = new JFrame("Check Box Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());

JPanel checkBoxPanel = new JPanel();

JCheckBox exchangingCard1 = new JCheckBox("A");
checkBoxPanel.add(exchangingCard1);
JCheckBox exchangingCard2 = new JCheckBox("B");
checkBoxPanel.add(exchangingCard2);
JCheckBox exchangingCard3 = new JCheckBox("C");
checkBoxPanel.add(exchangingCard3);
JCheckBox exchangingCard4 = new JCheckBox("D");
checkBoxPanel.add(exchangingCard4);
JCheckBox exchangingCard5 = new JCheckBox("E");
checkBoxPanel.add(exchangingCard5);

mainPanel.add(checkBoxPanel);

frame.add(mainPanel);

frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

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

}

关于java - JFrame - 将复选框添加到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28326585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com