Implementation of the java-RGB color palette (anonymous internal class of event listeners)

Keywords: Java jvm

Title requirements:

 **Require a case where three JSliders are used to select values of R, G, B primary colors. Users can dynamically synthesize a color using the active JSlider slider. The synthesized color is displayed on the interface.**

Code Ideas Analysis:
1. Required Components

(1)JFrame window
 (2) RGB tag text component of JLabel
 (3) Number of color changes in JTextField
 (4) Slider component for JSlider color exchange
 (5)JTextArea color display area

2. Instantiate all JFrame, JSlider, JTextField, JTextArea components in the construction method
3. Write another way to add all the components you created to the appropriate panels. The layout is based on BorderLayout (east, west, north, south) mode of the JPanel component panel in the swing package, with the middle and middle panels among them and the others not written
4. Most importantly, I used the method of anonymous internal classes to write a time listener added by a method independently. See the code in detail
5. Write a way to size the outermost panel, close the window, and most importantly, close the window setVisible (true), which is explained as follows:

setVisible(true); the method means that the data model has been constructed to allow the JVM to execute the paint method based on the data model to start drawing and display it on the screen, instead of displaying the graphics, it is ready to run and start drawing.This method is a bit different from the java multithreaded start method, which allows the run method to run. The start method is similar to the setVisible method.
6. Write all the methods to the end of the construction method, keeping in mind that the order is most important, as follows:
Add the component to the panel to invoke the time listener method, or you will see the consequences yourself.
8. The last step is to instantiate the creation of objects by putting all the methods in the main method into the construction method.
The code is as follows:

package create;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ZuoYe {
    private JFrame jf;
    private JLabel label1, label2, label3;
    private JSlider slider1, slider2, slider3;
    private JTextField field1, field2, field3;
    private JTextArea jta;

    public ZuoYe() {
        jf = new JFrame();
        label1 = new JLabel("R:");
        label2 = new JLabel("\n G:");
        label3 = new JLabel("B:");

        slider1 = new JSlider(0, 255, 127);
        slider2 = new JSlider(0, 255, 127);
        slider3 = new JSlider(0, 255, 127);

        field1 = new JTextField("127");
        field2 = new JTextField("127");
        field3 = new JTextField("127");

        field1.setEditable(false);
        field2.setEditable(false);
        field3.setEditable(false);

        jta = new JTextArea(5, 10);
        jta.setEditable(false);
        jta.setBackground(new Color(127, 127, 127));
        init();
        showMe();
        addEventHandler();
    }

    public void init() {
        JPanel jp1 = new JPanel();
        jp1.add(label1);
        jp1.add(slider1);
        jp1.add(field1);

        jp1.add(label2);
        jp1.add(slider2);
        jp1.add(field2);

        jp1.add(label3);
        jp1.add(slider3);
        jp1.add(field3);

        JPanel jp2 = new JPanel();
        jp2.add(jta);

        jf.add(jp1, BorderLayout.CENTER);
        jf.add(jp2, BorderLayout.EAST);
        jf.setVisible(true);
    }

    public void showMe() {
        jf.setSize(400, 200);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }

    public void addEventHandler() {
        ChangeListener lis = new ChangeListener() {

            public void stateChanged(ChangeEvent arg0) {
                int r = slider1.getValue();
                int g = slider2.getValue();
                int b = slider3.getValue();
                field1.setText(r + "");
                field2.setText(g + "");
                field3.setText(b + "");
                Color c = new Color(r, g, b);
                jta.setBackground(c);
            }
        };
        slider1.addChangeListener(lis);
        slider2.addChangeListener(lis);
        slider3.addChangeListener(lis);
    }

    public static void main(String[] args) {
        //The construct method writes the calling object, calls the method without the construct object, and other methods are written at the end of the construct method
        ZuoYe ts = new ZuoYe();
//        ts.showMe();
//        ts.init();
    }
}

Posted by mcatalf0221 on Mon, 11 Nov 2019 19:32:01 -0800