當aboutWindow
框架打開時,只有標籤(-contents)name
可見,菜單欄丟失。當標籤被註釋掉時,會出現該條,但會填充整個框架。我錯過了什麼?JLabel覆蓋JMenuBar
import javax.swing.*;
import java.net.*;
import java.awt.event.*;
public class Main
{
public static JDialog aboutWindow;
public static void about() {
//JDialog aboutWindow = new JDialog();
Main.aboutWindow = new JDialog();
Main.aboutWindow.setTitle("About kingfisher a0.0.3");
Main.aboutWindow.setSize(300,600);
Main.aboutWindow.setModal(true);
JMenuBar menubar = new JMenuBar();
JMenu control = new JMenu("Control");
JMenuItem quit = new JMenuItem("quit");
quit.addActionListener(new ActionListene121r() {
public void actionPerformed(ActionEvent event) {
Main.aboutWindow.setVisible(false);
}
}
);
control.add(quit);
menubar.add(control);
Main.aboutWindow.add(menubar);
JLabel name = new JLabel("kingfisher");
Main.aboutWindow.add(name);
Main.aboutWindow.setVisible(false);
}
public static void main (String args[]){
//Defined window dimensions
JFrame controlpanel = new JFrame();
controlpanel.setTitle("kingfisher Control Panel");
controlpanel.setSize(500,400);
controlpanel.add(new JLabel("The angels have the blue box"));
JMenuBar menubar = new JMenuBar();
//defining menu groups
JMenu windows = new JMenu("Windows");
JCheckBoxMenuItem chat = new JCheckBoxMenuItem("Chat");
JCheckBoxMenuItem filetransfer = new JCheckBoxMenuItem("Filetransfer");
JCheckBoxMenuItem settings = new JCheckBoxMenuItem("Settings");
windows.add(chat);
windows.add(filetransfer);
windows.add(settings);
JMenu control = new JMenu("Control");
JMenuItem quit = new JMenuItem("Quit");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
);
control.add(quit);
JMenu help = new JMenu("Help");
JMenuItem support = new JMenuItem("Support");
JMenuItem about = new JMenuItem("About");
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Main.aboutWindow.setVisible(true);
}
}
);
help.add(support);
help.add(about);
menubar.add(control);
menubar.add(windows);
menubar.add(help);
controlpanel.setJMenuBar(menubar);
controlpanel.setVisible(true);
};
};
運行該代碼(將'new ActionListene121r'更改爲'new ActionListener'後)在這裏產生'NullPointerException' .. –
哎呀,這些錯別字是不是打算。但'NullPointerExeption',我看不到。哪些LOC會引用錯誤? – sam4ritan
這是一種有趣的格式化風格,至少可以說...你的標籤都在商店 – Drgabble