所以我有两个类,一个用于创建JFrame,另一个用于加载JFrame上的菜单栏,代码如下:
import java.awt.Toolkit;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.ImageIcon;
public class Main extends JFrame{
static JFrame F = new Main();
static UI ui = new UI();
public Main()
{
ImageIcon img = new ImageIcon("icon.png");
setTitle("JFrame title");
setJMenuBar(ui);
setUndecorated(true);
//maximize window
setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
setVisible(true);
setIconImage(img.getImage());
}
public static void main(String[] args)
{
}
}然后是UI代码:
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class UI extends JMenuBar{
JMenu menu = new JMenu("File");
JMenuItem i1,i2;
public UI() {
i1 = new JMenuItem("Save");
i2 = new JMenuItem("Load");
menu.add(i1);
menu.add(i2);
add(menu);
}
public static void main (String[] args) {
}
}
当我编译时,没有出现任何错误,我运行了代码,我只有JFrame,没有错误我做过吗
UI在setJMenuBar
中调用时仍未初始化。
public class Main extends JFrame {
static JFrame F = new Main();
public Main() {
UI ui = new UI();
ImageIcon img = new ImageIcon("icon.png");
setTitle("JFrame title");
setJMenuBar(ui);
setUndecorated(true);
//maximize window
setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
setVisible(true);
setIconImage(img.getImage());
}
public static void main(String[] args) {
}
}