package com.company; import javax.swing.*; import java.awt.*; public class Main extends JFrame {//让main成为一个窗体 public Main(){ setVisible(true);//设置窗体可见 setTitle("窗体标题"); /* *窗体关闭规则 *EXIT_ON_CLOSE:隐藏窗体,并停止程序 * DO_NOTHING_ON_CLOSE:无任何操作 * HIDE_ON_CLOSE:隐藏窗体,但不停止程序 * DISPOSE_ON_CLOSE:释放窗体资源 */ // f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // f.setSize(300,200);//设置窗体大小,单位:像素 // f.setLocation(200,200);//设置窗体坐标,单位:像素 setBounds(200,200,300,400);//设置窗体坐标和大小,单位:像素 Container c=getContentPane(); c.setBackground(Color.black); JLabel l=new JLabel("this is window"); c.add(l);//添加组件 c.remove(l);//删除组件 c.validate();//验证容器中的组件 setContentPane(c);//重新载入组件 setResizable(false);//设置窗体是否可变大小 System.out.println("x="+getX()+" y="+getY()); } public static void main(String[] args) { new Main(); } }