java里简单事件与颜色示例

来源:互联网 发布:iphone淘宝指纹支付 编辑:程序博客网 时间:2024/04/28 07:11

package cs1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class myJFrame extends JFrame implements ActionListener
{
  private JColorChooser jcc;
  private JDialog jdd;
  private JButton jbb;
 
  public static void main(String args[])
  {
    myJFrame test=new myJFrame();
    test.setTitle("JFrame");
    test.setSize(300,200);
    test.setVisible(true);
  }
  public myJFrame()
  {
    jcc=new JColorChooser();

    jdd=JColorChooser.createDialog(this,"选择颜色",true,jcc,this,null);
    jbb=new JButton("click");
    jbb.addActionListener(this);
    getContentPane().add(jbb,BorderLayout.NORTH);
  }
 
  public void actionPerformed(ActionEvent e)
  {
    String text=((JButton)e.getSource()).getText();
    if(text=="click")
      jdd.show();
    else
      jbb.setBackground(jcc.getColor());
  }
}