⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selector.java~91~

📁 The lenguaje in java
💻 JAVA~91~
字号:
package g17.color;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class Selector
    extends JFrame implements ActionListener, ChangeListener {

  public static int r = 0, g = 0, b = 0;
  public static Color color;

  private JLabel jlRojo = new JLabel("Rojo");
  private JLabel jlVerde = new JLabel("Verde");
  private JLabel jlAzul = new JLabel("Azul");

  private JSlider jslRojo = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
  private JSlider jslVerde = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
  private JSlider jslAzul = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);

  private JSpinner jspRojo = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));
  private JSpinner jspVerde = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));
  private JSpinner jspAzul = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));

  private JPanel pMuestra = new JPanel(new BorderLayout());
  private Canvas muestra = new Canvas();

  private JTextField jtfCodificacion = new JTextField("0,0,0");
  private JTextField jtfInstancia = new JTextField("new Color(0,0,0);");

  private JButton jbCopiarCod = new JButton("Copiar");
  private JButton jbCopiarInst = new JButton("Copiar");

  private JCheckBox jchkbxAlawaysOntTop = new JCheckBox("Siempre al frente", true);

  public Selector() {
    super("Selector de Color [G-17]");
    setAlwaysOnTop(jchkbxAlawaysOntTop.isSelected());
    setSize(700, 400);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation( (int) (d.getWidth() - getWidth()) / 2,
                (int) (d.getHeight() - getHeight()) / 2);

    setContentPane(createContentPane());

    jchkbxAlawaysOntTop.addActionListener(this);
    jbCopiarCod.addActionListener(this);
    jbCopiarInst.addActionListener(this);

    jslRojo.addChangeListener(this);
    jslVerde.addChangeListener(this);
    jslAzul.addChangeListener(this);

    jspRojo.addChangeListener(this);
    jspVerde.addChangeListener(this);
    jspAzul.addChangeListener(this);


    setVisible(true);
  }

  private JPanel createContentPane() {
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints g = new GridBagConstraints();

    jlRojo.setDisplayedMnemonic(KeyEvent.VK_R);
    jlVerde.setDisplayedMnemonic(KeyEvent.VK_V);
    jlAzul.setDisplayedMnemonic(KeyEvent.VK_A);
    jbCopiarCod.setMnemonic(KeyEvent.VK_O);
    jbCopiarInst.setMnemonic(KeyEvent.VK_C);
    jchkbxAlawaysOntTop.setMnemonic(KeyEvent.VK_S);

    jbCopiarCod.setToolTipText("Copiar codificaci髇 RGB al portapapeles");
    jbCopiarInst.setToolTipText(
        "Copiar Instancia JAVA del color al portapapeles");
    jchkbxAlawaysOntTop.setToolTipText(
        "Mantener esta ventana al frente de todas las otras ventanas");
    pMuestra.setToolTipText("Resultado de la codificaci髇 seleccionada");

    jtfCodificacion.setEditable(false);
    jtfInstancia.setEditable(false);

    jlRojo.setLabelFor(jslRojo);
    jlVerde.setLabelFor(jslVerde);
    jlAzul.setLabelFor(jslAzul);

    jslRojo.setMajorTickSpacing(15);
    jslRojo.setMinorTickSpacing(5);
    jslRojo.setPaintLabels(true);
    jslRojo.setPaintTicks(true);
    jslRojo.setBorder(BorderFactory.createLineBorder(Color.red));

    jslVerde.setMajorTickSpacing(15);
    jslVerde.setMinorTickSpacing(5);
    jslVerde.setPaintLabels(true);
    jslVerde.setPaintTicks(true);
    jslVerde.setBorder(BorderFactory.createLineBorder(Color.green));

    jslAzul.setMajorTickSpacing(15);
    jslAzul.setMinorTickSpacing(5);
    jslAzul.setPaintLabels(true);
    jslAzul.setPaintTicks(true);
    jslAzul.setBorder(BorderFactory.createLineBorder(Color.blue));

    muestra.setBackground(Color.black);
    pMuestra.setBorder(BorderFactory.createEtchedBorder());
    pMuestra.add(muestra, BorderLayout.CENTER);
    pMuestra.setPreferredSize(new Dimension(150, 100));

    panel.add(new JLabel("Busque el color deseado desplazando los sliders y luego identifique su codificaci髇."),
              gbc(0, 0, 7, 1, 0.0, 0.0, g.WEST, g.HORIZONTAL, 10, 2));
    panel.add(new JSeparator(),
              gbc(0, 1, 7, 1, 1.0, 0.0, g.WEST, g.HORIZONTAL, 10, 2));
    panel.add(jlRojo, gbc(0, 2, 1, 1, 1.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jslRojo, gbc(0, 3, 4, 1, 1.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jspRojo, gbc(4, 3, 1, 1, 0.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jlVerde, gbc(0, 4, 1, 1, 1.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jslVerde, gbc(0, 5, 4, 1, 1.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jspVerde, gbc(4, 5, 1, 1, 0.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jlAzul, gbc(0, 6, 1, 1, 1.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jslAzul, gbc(0, 7, 4, 1, 1.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jspAzul, gbc(4, 7, 1, 1, 0.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(pMuestra, gbc(5, 3, 2, 5, 0.0, 0.0, g.CENTER, g.BOTH, 10, 30));
    panel.add(new JLabel("Codificaci髇 RGB:"),
              gbc(0, 8, 1, 1, 0.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jtfCodificacion,
              gbc(0, 9, 1, 1, 1.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jbCopiarCod, gbc(1, 9, 1, 1, 0.0, 0.0, g.CENTER, g.NONE, 1, 2));
    panel.add(new JLabel("Instancia JAVA:"),
              gbc(2, 8, 1, 1, 0.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jtfInstancia,
              gbc(2, 9, 2, 1, 1.0, 0.0, g.WEST, g.HORIZONTAL, 1, 2));
    panel.add(jbCopiarInst, gbc(4, 9, 1, 1, 0.0, 0.0, g.CENTER, g.NONE, 1, 2));
    panel.add(jchkbxAlawaysOntTop,
              gbc(5, 9, 2, 1, 0.0, 0.0, g.CENTER, g.HORIZONTAL, 1, 2));

    return panel;
  }

  public GridBagConstraints gbc(int x, int y, int w, int h, double wx,
                                double wy, int pos, int fill, int sepx,
                                int sepy) {
    return new GridBagConstraints(x, y, w, h, wx, wy, pos, fill,
                                  new Insets(sepy, sepx, sepy, sepx), 2, 2);
  }

  private void actualizeUndecorated() {
    setAlwaysOnTop(jchkbxAlawaysOntTop.isSelected());
  }

  private void createColorForTextFields(){
    String code=r+", "+g+", "+b;
    jtfCodificacion.setText(code);
    jtfInstancia.setText("new Color("+code+");");
  }

  public void stateChanged(ChangeEvent e) {
  }

  public void actionPerformed(ActionEvent e) {
    if(e.getSource()==jbCopiarCod){
      jtfCodificacion.selectAll();
      jtfCodificacion.copy();
    }else if(e.getSource()==jbCopiarInst){
      jtfInstancia.selectAll();
      jtfInstancia.copy();
    }else if(e.getSource()==jchkbxAlawaysOntTop){
      actualizeUndecorated();
    }
  }


  public static void main(String args[]) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (UnsupportedLookAndFeelException ex) {
    }
    catch (IllegalAccessException ex) {
    }
    catch (InstantiationException ex) {
    }
    catch (ClassNotFoundException ex) {
    }

    Selector selector = new Selector();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -