Let's start by using JavaSwing to display the application window.
The Java development environment uses OpenJDK 11.0.4 installed on Ubuntu 18.04.
MyFrame.java
import javax.swing.JFrame;
public class MyFrame extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame("Java Swing test");
frame.setVisible(true);
// x(X)Quit the application with the button.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Window display position and size(Coordinates x,Coordinate y,width,height)
frame.setBounds(300, 80, 500, 600);
}
}
Running this code will display an empty window at the specified position (x = 300, y = 80) and at the specified size (500, 600).
In setBounds of the above code, the position and size of the window are specified, but by replacing setBounds with the following code, the window can be displayed in the center of the screen.
//Window size(width,height)
frame.setSize(500, 600);
//Display window in the center of the screen
frame.setLocationRelativeTo(null);
The window is. I want to display it in the center of the screen, so I will actually replace it.
MyFrame.java
import javax.swing.JFrame;
public class MyFrame extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame("Java Swing test");
frame.setVisible(true);
// x(X)Quit the application with the button.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Window size(width,height)
frame.setSize(500, 600);
//Display window in the center of the screen
frame.setLocationRelativeTo(null);
}
}
The execution result is here.
Try making a calculator app in Java
Recommended Posts