【问题标题】:Can't add checkbox group using java AWT example from book无法使用书中的 java AWT 示例添加复选框组
【发布时间】:2019-08-18 00:25:40
【问题描述】:

我正在尝试学习一些基本的 AWT 用法,以使用复选框组创建一个非常简单的 UI。我一直在使用《Java The Complete Reference - Tenth edition》一书”我使用的示例是直接从书中拿出来的。框架显示,文本字符串也显示,但复选框组没有显示(我正在使用eclipse on windows 7 64 bit. Java 版本为 12.0.1)

我已经在 eclipse 和命令行中尝试过,结果相同。

这里是示例的源代码:

// Demonstrate AWT Checkbox Group
import java.awt.*;
import java.awt.event.*;

public class CBGroup extends Frame implements ItemListener {
    String msg = "";
    Checkbox windows, android, solaris, mac;
    CheckboxGroup cbg;

    public CBGroup()    {
        // Use a flow layout
        setLayout (new FlowLayout());

        // Create a checkbox group
        cbg = new CheckboxGroup();

        // Create the checkboxes and include them in the group
        windows = new Checkbox("windows", cbg, true);
        android = new Checkbox("android", cbg, false);
        solaris = new Checkbox("solaris", cbg, false);
        mac = new Checkbox("mac", cbg, false);

        // Add item listeners
        windows.addItemListener(this);
        android.addItemListener(this);
        solaris.addItemListener(this);
        mac.addItemListener(this);

        addWindowListener(new WindowAdapter () {
            public void windowClosing (WindowEvent we) {
                System.exit(0); 
            }
        });
    }

    public void itemStateChanged (ItemEvent ie) {
        repaint();
    }

    // Display current state of the check boxes
    public void paint (Graphics g)  {
        msg = "Current selection: ";
        msg += cbg.getSelectedCheckbox().getLabel();
        g.drawString(msg, 20, 120);
    }

    public static void main(String[] args) {
        CBGroup appwin = new CBGroup();

        appwin.setSize(new Dimension (240, 180));
        appwin.setTitle("CBGroup");
        appwin.setVisible(true);
    }
}

我希望显示一个窗口框架,其中包含一个复选框组,显示窗口、solaris、mac 和 android 有选择,并且窗口已被选为默认值。下面应该是一个文本字符串,上面写着“当前选择:窗口”。文本字符串显示,窗口框架看起来不错并且工作正常,但复选框组没有显示。同样,这段代码直接来自我提到的书。我猜这可能与流程布局部分有关,但对此没有太多控制。

【问题讨论】:

    标签: java awt flowlayout


    【解决方案1】:

    (在您继续您的项目之前,请查看what is the difference between Swing and AWT我建议您转移到 Swing。

    您无法看到复选框,因为您没有将它们添加到框架中。使用Frame.add(Component c) 方法来实现这一点。

    现在关于自定义绘画,我不喜欢在这里,因为它只是一个文本。您可以添加标签或其他东西,而不是使用自定义绘画。此外,当您覆盖 paint 方法时,请始终从调用 super.paint(Graphics g) 开始(同样的“规则”适用于 Swing - paintComponent 方法)。

    最后,所有 AWT(和 Swing)应用程序都必须在它们自己的线程上运行。对 AWT 使用 EventQueue#invokeLater 方法,对 Swing 使用 SwingUtilities#invokeLater。 (它们真的不同吗?

    您的代码包含我提到的所有实现:

    public class CBGroup extends Frame implements ItemListener {
        String msg = "";
        Checkbox windows, android, solaris, mac;
        CheckboxGroup cbg;
    
        public CBGroup() {
            super("");
            // Use a flow layout
            setLayout(new FlowLayout());
    
            // Create a checkbox group
            cbg = new CheckboxGroup();
    
            // Create the checkboxes and include them in the group
            windows = new Checkbox("windows", cbg, true);
            android = new Checkbox("android", cbg, false);
            solaris = new Checkbox("solaris", cbg, false);
            mac = new Checkbox("mac", cbg, false);
    
            add(windows);
            add(android);
            add(solaris);
            add(mac);
    
            // Add item listeners
            windows.addItemListener(this);
            android.addItemListener(this);
            solaris.addItemListener(this);
            mac.addItemListener(this);
    
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent we) {
                    System.exit(0);
                }
            });
        }
    
        @Override
        public void itemStateChanged(ItemEvent ie) {
            repaint();
        }
    
    //  // Display current state of the check boxes
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            msg = "Current selection: ";
            msg += cbg.getSelectedCheckbox().getLabel();
            g.drawString(msg, 20, 120);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(() -> {
                CBGroup appwin = new CBGroup();
    
                appwin.setSize(new Dimension(240, 180));
                appwin.setTitle("CBGroup");
                appwin.setVisible(true);
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-25
      • 2013-02-20
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      相关资源
      最近更新 更多