亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用書中的 java AWT 示例添加復選框組

無法使用書中的 java AWT 示例添加復選框組

胡子哥哥 2023-06-08 19:19:05
我正在嘗試學習一些基本的 AWT 用法來使用復選框組創建一個非常簡單的 UI。我一直在使用 Java The Complete Reference - Tenth edition 這本書” 我使用的示例直接來自書中??蚣茱@示,文本字符串也顯示,但未顯示復選框組(我正在使用Windows 7 64 位上的 eclipse。Java 版本是 12.0.1)我已經在 eclipse 和命令行中嘗試過,結果相同。下面是示例的源代碼:// Demonstrate AWT Checkbox Groupimport 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 有選擇,并且窗口已經被選為默認窗口。在它下面應該是一個文本字符串,上面寫著“當前選擇:windows”。文本字符串出現,窗口框架看起來不錯并且工作正常,但復選框組沒有出現。同樣,這段代碼直接來自我提到的那本書。我猜它可能與流程布局部分有關,但對此控制不多。
查看完整描述

1 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

(在你繼續你的項目之前,先看看Swing 和 AWT 有什么區別。我建議你轉向 Swing。

您看不到復選框,因為您沒有將它們添加到框架中。使用Frame.add(Component c)方法來實現這一點。

現在關于自定義繪畫,我不喜歡這里,因為它只是一個文本。您可以添加標簽或其他東西,而不是使用自定義繪畫。此外,當您重寫paint方法時,始終從調用開始super.paint(Graphics g)(相同的“規則”適用于 Swing -paintComponent方法)。

最后,所有 AWT(和 Swing)應用程序都必須在它們自己的線程上運行。將EventQueue#invokeLater方法用于 AWT 和SwingUtilities#invokeLaterSwing。(他們真的不同嗎?

您的代碼以及我提到的所有實現:

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

? ? ? ? });

? ? }

}


查看完整回答
反對 回復 2023-06-08
  • 1 回答
  • 0 關注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號