第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

無(wú)法使用書中的 java AWT 示例添加復(fù)選框組

無(wú)法使用書中的 java AWT 示例添加復(fù)選框組

胡子哥哥 2023-06-08 19:19:05
我正在嘗試學(xué)習(xí)一些基本的 AWT 用法來(lái)使用復(fù)選框組創(chuàng)建一個(gè)非常簡(jiǎn)單的 UI。我一直在使用 Java The Complete Reference - Tenth edition 這本書” 我使用的示例直接來(lái)自書中??蚣茱@示,文本字符串也顯示,但未顯示復(fù)選框組(我正在使用Windows 7 64 位上的 eclipse。Java 版本是 12.0.1)我已經(jīng)在 eclipse 和命令行中嘗試過,結(jié)果相同。下面是示例的源代碼:// 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);    }}我希望顯示一個(gè)窗口框架,其中包含一個(gè)復(fù)選框組,顯示窗口、solaris、mac 和 android 有選擇,并且窗口已經(jīng)被選為默認(rèn)窗口。在它下面應(yīng)該是一個(gè)文本字符串,上面寫著“當(dāng)前選擇:windows”。文本字符串出現(xiàn),窗口框架看起來(lái)不錯(cuò)并且工作正常,但復(fù)選框組沒有出現(xiàn)。同樣,這段代碼直接來(lái)自我提到的那本書。我猜它可能與流程布局部分有關(guān),但對(duì)此控制不多。
查看完整描述

1 回答

?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

(在你繼續(xù)你的項(xiàng)目之前,先看看Swing 和 AWT 有什么區(qū)別。我建議你轉(zhuǎn)向 Swing。

您看不到復(fù)選框,因?yàn)槟鷽]有將它們添加到框架中。使用Frame.add(Component c)方法來(lái)實(shí)現(xiàn)這一點(diǎn)。

現(xiàn)在關(guān)于自定義繪畫,我不喜歡這里,因?yàn)樗皇且粋€(gè)文本。您可以添加標(biāo)簽或其他東西,而不是使用自定義繪畫。此外,當(dāng)您重寫paint方法時(shí),始終從調(diào)用開始super.paint(Graphics g)(相同的“規(guī)則”適用于 Swing -paintComponent方法)。

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

您的代碼以及我提到的所有實(shí)現(xiàn):

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

? ? ? ? });

? ? }

}


查看完整回答
反對(duì) 回復(fù) 2023-06-08
  • 1 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)