1 package ch11; 2 3 import javax.swing.JFrame; 4 5 public class MyfirstJframe { 6 7 public static void main(String[] args) { 8 JFrame f=new JFrame("我的窗口"); 9 f.setSize(300, 400); 10 f.setLocation(500, 500); 11 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 f.setVisible(true); 13 } 14 15 }
1 package ch11; 2 3 import java.awt.HeadlessException; 4 5 import javax.swing.JFrame; 6 7 public class MyfirstJframe_2 extends JFrame { 8 9 public MyfirstJframe_2() throws HeadlessException { 10 super(); 11 setTitle("我的窗口"); 12 setSize(500, 500); 13 setLocation(400, 400); 14 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15 } 16 17 public static void main(String[] args) { 18 MyfirstJframe_2 f=new MyfirstJframe_2(); 19 f.setVisible(true); 20 } 21 22 23 24 }
1 package ch11; 2 import java.awt.*; 3 import javax.swing.*; 4 public class MyFirstJFrame_3 extends JFrame{ 5 6 private JLabel la; 7 private JButton lb; 8 9 public MyFirstJFrame_3() { 10 super(); 11 setTitle("我的窗口"); 12 setBounds(400, 400, 400, 400); 13 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 14 setLayout(new FlowLayout()); 15 la=new JLabel("这是我设计的第一个窗口!"); 16 Font ft=new Font("黑体",Font.ITALIC,25); 17 la.setFont(ft); 18 la.setForeground(Color.RED); 19 add(la); 20 lb=new JButton("点击该按钮,标签中显示字体将变小");//此时没反应 21 add(lb); 22 23 } 24 public static void main(String[] args) { 25 MyFirstJFrame_3 f=new MyFirstJFrame_3(); 26 f.setVisible(true); 27 } 28 29 }
1 package ch11; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 public class MyFirstJFrame_4 extends JFrame implements ActionListener{ 8 9 private JLabel la; 10 private JButton lb; 11 12 public MyFirstJFrame_4() throws HeadlessException { 13 super(); 14 setTitle("我的窗口"); 15 setSize(400, 400); 16 setLocation(400, 400); 17 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 setLayout(new FlowLayout()); 19 la= new JLabel("这是我设计的第一个窗口"); 20 Font ft=new Font("宋体", Font.BOLD, 25); 21 la.setFont(ft); 22 la.setForeground(Color.blue); 23 add(la); 24 lb=new JButton("单点击该按钮,标签中显示字体变小"); 25 add(lb); 26 lb.addActionListener(this); 27 } 28 29 public static void main(String[] args) { 30 MyFirstJFrame_4 f=new MyFirstJFrame_4(); 31 f.setVisible(true); 32 33 } 34 35 @Override 36 public void actionPerformed(ActionEvent e) {//重写单点击事件响应处理的方法 37 Font ft=new Font("宋体", Font.BOLD, 11); 38 la.setFont(ft); 39 lb.setEnabled(false); 40 } 41 42 }