JColorChooser设置窗体颜色

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JColorChooser;

public class JButtonColor {
/**
* @param args
*/
public static void main(String[] args) {
      // TODO Auto-generated method stub
      JFrame frame = new JFrame("JButtonColor");

       MyPane2 panel = new MyPane2();
       frame.getContentPane().add(panel);

       frame.pack();
       frame.setLocation(300, 200);
       frame.setVisible(true);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}
 class MyPane2 extends JPanel implements ActionListener {

       private static final long serialVersionUID = 1L;
       private JButton button;
       private Color color = new Color(255, 51, 150);
       public MyPane2() {

            button = new JButton("Get Color");
            button.addActionListener(this);

            setPreferredSize(new Dimension(550, 250));
            setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
            setBackground(color);
            add(button);
}

public void actionPerformed(ActionEvent e) {
        color = JColorChooser.showDialog(this, "Choose Color", color);
         if (color != null) {
              setBackground(color);
        button.setText("Get again");
}
}
}

posted on 2014-10-22 22:44  Iitb  阅读(248)  评论(0)    收藏  举报

导航