[img]https://i.stack.imgur .com/KlgME.png[/img]
Мой код
Класс панели:
Код: Выделить всё
import javax.swing.*;
import java.awt.*;
public class MainFramePanel extends JPanel {
public static final int WIDTH = 1700, HEIGHT = 900;
private final JMenuBar menuBar = new JMenuBar();
private final JMenu fileMenu = makeFileMenu();
private MainFramePanel() {
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
this.setLayout(new BorderLayout());
menuBar.add(fileMenu);
this.add(menuBar, BorderLayout.NORTH);
}
private JMenu makeFileMenu() {
JMenu menu = new JMenu();
JMenuItem newFile = new JMenuItem("New"),
openFile = new JMenuItem("Open"),
saveFile = new JMenuItem("Save"),
exportFile = new JMenuItem("Export As"),
exit = new JMenuItem("Exit");
menu.add(newFile); menu.add(openFile);
menu.addSeparator();
menu.add(saveFile); menu.add(exportFile);
menu.addSeparator();
menu.add(exit);
return menu;
}
private static final MainFramePanel INSTANCE = new MainFramePanel();
public static MainFramePanel getInstance() {
return INSTANCE;
}
}
Код: Выделить всё
import javax.swing.*;
public class MainFrame extends JFrame {
private MainFrame() {
super("My Program");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(MainFramePanel.getInstance());
this.pack();
this.setLocationRelativeTo(null);
}
private static final MainFrame INSTANCE = new MainFrame();
public static MainFrame getInstance() {
return INSTANCE;
}
}
Подробнее здесь: https://stackoverflow.com/questions/780 ... at-the-top