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

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

在兩個(gè)不同的 JFrame 中打開的相同菜單僅適用于最后一個(gè)

在兩個(gè)不同的 JFrame 中打開的相同菜單僅適用于最后一個(gè)

我正在嘗試編寫一個(gè)簡(jiǎn)單的 Paint 應(yīng)用程序,在該應(yīng)用程序中,用戶將能夠JFrames使用繪圖面板打開新的(在新線程中),并且這些框架中的每一個(gè)都會(huì)JMenuBar在頂部有一個(gè),但是,只有最后一個(gè)打開的框架具有功能菜單欄,所有剩余的(打開的)框架都顯示菜單,但菜單不起作用(單擊時(shí)沒有任何反應(yīng))。有誰(shuí)知道如何解決這一問題?我已經(jīng)簡(jiǎn)化了代碼,只留下關(guān)于JMenuBar.代碼由以下類組成:主.javapackage sample;public class Main {    Main() {        MainFrameThread.getMainFrameThread().run();    }//end of Main()    public static void main(String[] args) {        new Main();    }}//end of Main classTopMenu.javapackage sample;import javax.swing.*;public class TopMenu extends JMenuBar {    private JMenu menu_File;    private static JMenuItem menu_New;    public static JMenuItem getMenu_New() {        return menu_New;    }    public TopMenu() {        menu_File = new JMenu("File");        menu_New = new JMenuItem("New");        this.add(menu_File);        menu_File.add(menu_New);    }//end of TopMenu()}//end of TopMenu extends JMenuBarMainFrameThread.javapackage sample;public class MainFrameThread extends Thread {    private static MainFrameThread mainFrameThread = new MainFrameThread();    public static MainFrameThread getMainFrameThread() {        return mainFrameThread;    }    public MainFrameThread() {}    @Override    public void run() {        MainFrame mainFrame = new MainFrame();    }//end of public void run()}//end of public class FrameSizeDialogThread動(dòng)作控制器.javapackage sample;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class ActionController {    private static ActionController actionController = new ActionController();    private ListenForMenu listenForMenu = new ListenForMenu();    public static ActionController getActionController() {        return actionController;    }我被卡住了,不管我在哪里初始化 MainFrame.java,什么都沒有。有人看錯(cuò)了嗎???
查看完整描述

1 回答

?
UYOU

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

然而,只有最后一個(gè)打開的框架有一個(gè)功能菜單欄


Swing 組件不能共享。Swing 組件只能有一個(gè)父組件。因此,對(duì)于每個(gè)子窗口,您都需要?jiǎng)?chuàng)建一個(gè)新的JMenuBarandJMenu和JMenuItem。


但是,Action使用的JMenuItem可以共享。


private static JMenuItem menu_New;


public static JMenuItem getMenu_New() {

    return menu_New;

}

與菜單相關(guān)的變量或方法都不應(yīng)該是靜態(tài)的。同樣,您需要為每個(gè)創(chuàng)建一個(gè)唯一的實(shí)例。


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

添加回答

舉報(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)