简介
java核心编程示例代码
code
/*
* @Author: your name
* @Date: 2020-10-28 17:06:45
* @LastEditTime: 2020-10-28 19:09:28
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /java/SimpleFrameTest.java
*/
import java.awt.*;
import javax.swing.*;
public class SimpleFrameTest {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
SimpleFrame frame = new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class SimpleFrame extends JFrame {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
public SimpleFrame() {
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
}
code
获得屏幕大小设定程序为1/2大小显示
/*
* @Author: your name
* @Date: 2020-10-28 19:42:14
* @LastEditTime: 2020-10-28 19:46:43
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /java/SizedFrameTest.java
*/
import java.awt.*;
import javax.swing.*;
public class SizedFrameTest {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new SizedFrame();
frame.setTitle("SizedFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class SizedFrame extends JFrame {
public SizedFrame() {
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth / 2, screenHeight / 2);
setLocationByPlatform(true);
Image img = new ImageIcon("icon.gif").getImage();
setIconImage(img);
}
}