package ui;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo {
public static void main(String[] args) {
//定义一个窗体对象f,窗体命名为"美丽的窗口"
JFrame f=new JFrame("美丽的窗口");
//用户单击窗口的关闭按钮是程序执行操作
f.setDefaultCloseOperation(3);
//把MyPanel设置为f的内容面板
f.setContentPane(new MyPanel());
//离显示屏上边缘100像素,里显示屏左边缘200像素
f.setLocation(100,200);
//设置窗体的大小为300*200像素大小
f.setSize(300,200);
//设置窗体是否可以调整大小,参数为布尔值
f.setResizable(false);
//设置窗体可见
f.setVisible(true);
}
}
class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
//设置成红色
g.setColor(new Color(255,0,0));
//画一个矩形
g.fillRect(32,108,32,32);
//写字符串
g.drawString("萌萌哒 ^_^ " , 54, 64);
//字体的颜色和大小
//g.setFont(new Font("黑体",Font.BOLD,32));
}
}
setDefaultCloseOperation(int operation):设置用户在此窗体上发起 "close" 时默认执行的操作。
方法中的参数解释如下:
0或DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in thewindowClosing method of a registeredWindowListener object.1或HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registeredWindowListener objects.2或DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registeredWindowListener objects.3或EXIT_ON_CLOSE (defined in JFrame): Exit the application using theSystemexit method. Use this only in applications