- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JTextField;
- import javax.swing.border.LineBorder;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- import java.awt.Color;
- import javax.swing.JSlider;
- import javax.swing.JLabel;
- import java.awt.Font;
- import javax.swing.SwingConstants;
- public class Main extends JFrame {
- private JPanel contentPane;
- private JTextField textField;
- private JSlider slider1;
- private JSlider slider2;
- private JSlider slider3;
- private JLabel lb_r;
- private JLabel lb_g;
- private JLabel lb_b;
- private JPanel panel;
- private int r,g,b;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Main frame = new Main();
- frame.setVisible(true);
- frame.setTitle("調色工具");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Main() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 435, 403);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
-
- textField = new JTextField();
- textField.setHorizontalAlignment(SwingConstants.CENTER);
- textField.setText("0,0,0");
- textField.setBounds(164, 10, 96, 21);
- contentPane.add(textField);
- textField.setColumns(10);
-
- panel = new JPanel();
- panel.setBackground(new Color(0, 0, 0));
- panel.setBorder(new LineBorder(new Color(0, 0, 0)));
- panel.setBounds(149, 41, 126, 128);
- contentPane.add(panel);
-
- slider1 = new JSlider();
- slider1.setValue(0);
- slider1.setMaximum(255);
- slider1.setBounds(10, 193, 276, 23);
- slider1.addChangeListener(new ChangeListener(){
- public void stateChanged(ChangeEvent e)
- {
- lb_r.setText(slider1.getValue()+"");
- r=slider1.getValue();
- panel.setBackground(new Color(r, g, b));
- textField.setText(r+","+g+","+b);
- }
- });
- contentPane.add(slider1);
-
- slider2 = new JSlider();
- slider2.setValue(0);
- slider2.setMaximum(255);
- slider2.setBounds(10, 254, 276, 23);
- slider2.addChangeListener(new ChangeListener(){
- public void stateChanged(ChangeEvent e)
- {
- lb_g.setText(slider2.getValue()+"");
- g=slider2.getValue();
- panel.setBackground(new Color(r, g, b));
- textField.setText(r+","+g+","+b);
- }
- });
- contentPane.add(slider2);
-
- slider3 = new JSlider();
- slider3.setValue(0);
- slider3.setMaximum(255);
- slider3.setBounds(10, 312, 276, 23);
- slider3.addChangeListener(new ChangeListener(){
- public void stateChanged(ChangeEvent e)
- {
- lb_b.setText(slider3.getValue()+"");
- b=slider3.getValue();
- panel.setBackground(new Color(r, g, b));
- textField.setText(r+","+g+","+b);
- }
- });
- contentPane.add(slider3);
-
- lb_r = new JLabel("0");
- lb_r.setFont(new Font("新細明體", Font.BOLD, 16));
- lb_r.setForeground(Color.RED);
- lb_r.setBounds(318, 193, 57, 23);
- contentPane.add(lb_r);
-
- lb_g = new JLabel("0");
- lb_g.setForeground(new Color(0, 100, 0));
- lb_g.setFont(new Font("新細明體", Font.BOLD, 16));
- lb_g.setBounds(318, 254, 57, 23);
- contentPane.add(lb_g);
-
- lb_b = new JLabel("0");
- lb_b.setForeground(new Color(0, 0, 139));
- lb_b.setFont(new Font("新細明體", Font.BOLD, 16));
- lb_b.setBounds(318, 312, 57, 23);
- contentPane.add(lb_b);
- }
- }
複製代碼 |