簡體   English   中英

我在哪里把這個語句'setJMenuBar(menuBar);'?

[英]Where do I put this statement 'setJMenuBar(menuBar);'?

主類:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Main extends JFrame {

public static void main(String[] args) {
    JFrame frame  = new JFrame("Checking Account Actions");
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    CheckingAccountActions panel = new CheckingAccountActions();

    MyWindowAdapter winAdapter = new MyWindowAdapter(panel);
    frame.addWindowListener(winAdapter);

    frame.getContentPane().add(panel);
    frame.pack();

    frame.setVisible(true);
}

} // Main()

class MyWindowAdapter extends WindowAdapter {
private CheckingAccountActions saved;

public MyWindowAdapter(CheckingAccountActions saved) {
    this.saved = saved;
}

// in your window closing method
// check the state of checkActions first before doing anything
public void windowClosing(WindowEvent e) {
    // note -- don't check for saved in a static way
    // use a method on the instance.
    if(!saved.saved) {
        String  message = "The data in the application is not saved.\n"
                        + "Would you like to save it before exiting the
                                            +  application?";
        int confirm = JOptionPane.showConfirmDialog (null, message);

        if (confirm == JOptionPane.YES_OPTION)
            CheckingAccountActions.chooseFile(2);
    }

    JFrame frame = (JFrame)e.getSource();
    frame.setVisible(false);

    // Main.frame.setVisible(false);
    System.exit(0);
}

} // MyWindowAdapter()

正如你所看到的,這個類擴展了JPanel,這是我的Menu項初始化的地方,但是我在這個語句'setJMenuBar(menuBar);'中使用Main類,因為它在CheckingAccountActions中給出了一個錯誤,因為JFrame在MAIN中。

CheckingAccountActions類:

public class CheckingAccountActions extends JPanel {
// Panel
private JLabel         message;

// Menu
private JMenuBar           menuBar;

private JMenu          File;
private JMenuItem      Read, Write;

private JMenu          Account;
private JMenuItem      NewAccount, ListAccounts, ListChecks, ListDeposits, FindAccount;

private JMenu          Transactions;
private JMenuItem      AddTransactions;

// code...
//
//
// code...

public CheckingAccountActions() {


//************************** PANEL *****************************************
// JLabel
    message = new JLabel("Please choose one of the items: ");
    message.setFont(new Font("Helvetica", Font.BOLD, 15));

    CheckingAccountActionsListener listener = new CheckingAccountActionsListener();

//************************** MENU ******************************************
// Menu
    File  = new JMenu("File");
// MenuItem
    Read  = new JMenuItem("Read from file");
    Write = new JMenuItem("Write to file");
// ActionListener
    Read.addActionListener(listener);
    Write.addActionListener(listener);
// Add Buttons to Menu
    File.add(Read);
    File.add(Write);

// Menu
    Account = new JMenu("Account");
// MenuItem
    NewAccount   = new JMenuItem("Add new account");
    ListAccounts = new JMenuItem("List accounts transaction");
    ListChecks   = new JMenuItem("List all checks");
    ListDeposits = new JMenuItem("List all deposits");
    FindAccount  = new JMenuItem("Find an account");
// ActionListener
    NewAccount.addActionListener(listener);
    ListAccounts.addActionListener(listener);
    ListChecks.addActionListener(listener);
    ListDeposits.addActionListener(listener);
    FindAccount.addActionListener(listener);
// Add Buttons to Menu
    Account.add(NewAccount);
    Account.add(ListAccounts);
    Account.add(ListChecks);
    Account.add(ListDeposits);
    Account.add(FindAccount);


// Menu
    Transactions = new JMenu("Transactions");
// MenuItem
    AddTransactions = new JMenuItem("Add Transactions");
// ActionListener
    AddTransactions.addActionListener(listener);
// Add Buttons to Menu
    Transactions.add(AddTransactions);

// MenuBar
    menuBar = new JMenuBar();
    menuBar.add(File);
    menuBar.add(Account);
    menuBar.add(Transactions);

    setBackground(Color.white);
    setPreferredSize(new Dimension(240, 250));
    setJMenuBar(menuBar);
}

private class CheckingAccountActionsListener implements ActionListener {

// code...

}

編輯:我感到困惑的是,當Frame在另一個類時,如何將我的MenuBar添加到Frame?

最終編輯:我得到了它的工作。 我剛剛將所有JFrame組件移動到CheckingAccountActions類。

請查看Swing教程中有關如何使用菜單的部分 MenuDemo示例將向您展示構建程序的一種方法。

它還向您展示了在Event Dispatch Thread上創建GUI的正確方法。

setJMenuBar(menubar)不是你真正想要的。 如果要將菜單欄設置為框架,只需使用方法add(menuBar)將菜單欄添加到框架中。

暫無
暫無

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

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