gpt4 book ai didi

java - 我应该把这条声明 'setJMenuBar(menuBar);' 放在哪里?

转载 作者:行者123 更新时间:2023-11-29 03:27:30 25 4
gpt4 key购买 nike

主类:

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);”,因为它在 CheckingAccountActions 中给出错误,因为 JFrame 在主要。

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...

}

编辑:我感到困惑的是当框架在另一个类中时如何将我的菜单栏添加到框架?

最终编辑:我让它工作了。我刚刚将所有 JFrame 组件移至 CheckingAccountActions 类。

最佳答案

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

它还向您展示了在事件调度线程上创建 GUI 的正确方法。

关于java - 我应该把这条声明 'setJMenuBar(menuBar);' 放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20279795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com