I didn't come out at once, so make a note.
MainWindow.java
public class MainPanel extends JPanel{
public MainPanel(){
JButton button1 = new JButton("button");
add(button1);
}
public static void main(String[] args){
JFrame frame = new JFrame();
MainPanel mainPanel = new MainPanel();
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
If nothing is done, MainWindow
will appear in the menu bar, so do as follows.
Confirmed on macOS 10.12.6, JRE 9.0.1.
MainWindow.java
public class MainPanel extends JPanel{
public MainPanel(){
JButton button1 = new JButton("button");
add(button1);
}
}
//Make another class. Any name
class Loader{
public static void main(String[] args){
//Use the system menu bar
System.setProperty("apple.laf.useScreenMenuBar", "true");
//Specifying the application name
System.setProperty("apple.awt.application.name", "test");
JFrame frame = new JFrame();
MainPanel mainPanel = new MainPanel();
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Specify properties for the JRE with System.setProperty
. At this time, the point is to specify ** before Swing is loaded.
If you write JPanel in an extension class, Swing will be loaded first. Therefore, it is necessary to create a separate class that is not related to Swing and write it at the beginning of the main method there.
Recommended Posts