JFrame-在屏幕上添加复选框 [英] JFrame - adding checkboxes to the screen

查看:137
本文介绍了JFrame-在屏幕上添加复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个显示5个不同复选框的JFrame.应该可以选择多个复选框.此代码仅读取ExchangingCard1行,并忽略所有其他复选框.运行它时,只有一个复选框,其中以"A"为字符.

I want to have a JFrame that displays 5 different check boxes. Multiple checkboxes should be able to be selected. This code only reads the ExchangingCard1 line and ignores all other checkboxes. When you run it you will have only one checkbox with "A" as the character.

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.

Put the check boxes in a JPanel, then put the JPanel to the JFrame.

这是一个可运行的示例.

Here's a runnable example.

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

}

这篇关于JFrame-在屏幕上添加复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆