我在哪里把这个语句'setJMenuBar(menuBar);'?

时间:2013-11-29 06:39:16

标签: java swing jframe jpanel jmenu

主类:

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,这是我的菜单项初始化的地方,但是我在这个语句'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类。

2 个答案:

答案 0 :(得分:3)

查看How to Use Menus上Swing教程中的部分。 MenuDemo示例将向您展示构建程序的一种方法。

它还向您展示了在Event Dispatch Thread上创建GUI的正确方法。

答案 1 :(得分:1)

setJMenuBar(menubar)不是你真正想要的。如果要将菜单栏设置为框架,只需使用方法add(menuBar)将菜单栏添加到框架中。