簡體   English   中英

如何將菜單集成到布局中(Java GUI)?

[英]How do I integrate a menu into a layout (Java GUI)?

我正在嘗試向該程序添加一個菜單,但我遇到了麻煩。 我認為菜單本身的代碼還可以,但我不知道如何將它實際插入到我的 GUI 中。 在這個項目中,我有 4 個課程。 一個用於一些 JRadioButtons,一個用於添加大文本字段,一個用於菜單,最后一個用於將它們放在一起。 當我在沒有菜單 class 的情況下運行程序時,這是 output。 Output 程序無菜單 class

這里也是一些代碼。

JRadioButton encrypt1 = new JRadioButton("Reverse Word" );
JRadioButton encrypt2 = new JRadioButton("+1");
ButtonGroup group1 = new ButtonGroup();

JRadioButton decrypt1 = new JRadioButton("Reverse Word");
JRadioButton decrypt2 = new JRadioButton("+1");
ButtonGroup group2 = new ButtonGroup();

public chooseEncryption(){
    setLayout(new GridLayout(3, 4));

    panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel();
    panel4 = new JPanel(); panel5 = new JPanel(); panel6 = new JPanel();
    panel7 = new JPanel(); panel8 = new JPanel(); panel9 = new JPanel();
    panel10 = new JPanel(); panel11 = new JPanel(); panel12 = new JPanel();

    encryptionLabel = new JLabel("Encryption");
    decryptionLabel = new JLabel("Decryption");
    group1.add(encrypt1);
    group1.add(encrypt2);
    group2.add(decrypt1);
    group2.add(decrypt2);

    panel2.add(encryptionLabel);
    panel4.add(decryptionLabel);
    panel6.add(encrypt1);
    panel8.add(decrypt1);
    panel10.add(encrypt2);
    panel12.add(decrypt2);

    add(panel1); add(panel2); add(panel3); add(panel4); add(panel5);
    add(panel6); add(panel7); add(panel8); add(panel9); add(panel10);
    add(panel11); add(panel12);
}

那是單選按鈕 class,然后我使用這個 putTogether class 集成它。

public putTogether(){ //Constructor of putTogether class
    setTitle("Encryption Program");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());

    panel = new chooseEncryption(); //object of radio button class
    panel2 = new textField(); //object of text field class


    add(panel, BorderLayout.NORTH);
    add(panel2, BorderLayout.CENTER);
    pack();
    setVisible(true);
}

這是菜單 class 的一部分。

public class menu extends JFrame{

    public void buildMenuBar(){
        menuBar = new JMenuBar();

        buildFileMenu();
        buildEditMenu();
        buildHelpMenu();

        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);

        setJMenuBar(menuBar);
    }

我將如何設置菜單使其成為 GUI 的一部分。 我知道這與為它創建構造函數有關,但我無法取得任何進展。 我很感激這方面的幫助!

使用卡片布局。 如何使用卡片布局 - https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

希望。 它有助於。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM