日期:2014-05-20 浏览次数:20897 次
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class ComponentEvents implements ActionListener { JFrame jf = new JFrame(); JButton b1=new JButton("改变框架的主题."); JButton b2=new JButton("还原框架的主题"); JButton btnColor=new JButton("改变按钮的颜色"); JButton btnRestore=new JButton("还原按钮颜色"); //Graphics gra=new Graphics(); Color color=btnColor.getBackground(); public ComponentEvents () { jf.setTitle("ComponentEvents"); jf.setSize(600,300); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); b1.addActionListener(this); b2.addActionListener(this); btnColor.addActionListener(this); btnRestore.addActionListener(this); b1.setBounds(10,20,150,20); b2.setBounds(160,20,150,20); btnColor.setBounds(10,60,150,20); btnRestore.setBounds(160,60,150,20); JPanel pane=new JPanel(); pane.setLayout(null); pane.add(b1); pane.add(b2); pane.add(btnColor); pane.add(btnRestore); jf.add(pane); jf.setVisible(true); } public void actionPerformed(ActionEvent evt) { Object source=evt.getSource(); if(source==b1) jf.setTitle("dkf"); else if(source==b2) jf.setTitle("ComponentEvents"); else if (source==btnColor) { btnColor.setBackground(Color.orange); } else if (source==btnRestore) { btnColor.setBackground(color); } } // public void paint(Graphics g) // { // b1.setBounds(10,20,150,20); // b2.setBounds(160,20,150,20); // btnColor.setBounds(10,60,150,20); // btnRestore.setBounds(160,60,150,20); // g.drawLine(10,170,500,170); // //repaint(); // } public static void main(String[] args) { ComponentEvents comevt= new ComponentEvents(); } }