I'd like to do two things:
1st: add a label, or anything, that allows me to put a "text" below the colors. Example in the illustration:
2nd:Makethepointer/pointerofslider
,gotocoloraccordingtoacertainnumberenteredinthefield(inthemethodbelowisclearertounderstand)p>
packagepacote01;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Graphics;importjava.awt.KeyEventDispatcher;importjava.awt.KeyboardFocusManager;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.image.BufferedImage;importjava.util.Dictionary;importjava.util.Hashtable;importjavax.swing.ImageIcon;importjavax.swing.JComponent;importjavax.swing.JFrame;importstaticjavax.swing.JFrame.EXIT_ON_CLOSE;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JSlider;importjavax.swing.JTextField;importjavax.swing.SwingUtilities;publicclassSliderextendsJFrame{publicstaticfinalColor[]COLORS={Color.red,Color.orange,Color.yellow,Color.green,Color.blue};privatestaticfinalintCOMPRIMENTO=30;privatestaticfinalintALTURA=10;privateJSliderslider=newJSlider(0,100,0);publicJTextFieldcampo=newJTextField();privatedoublevalor;publicSlider(){setSize(525,300);add(montaSlider());setLocationRelativeTo(null);setVisible(true);setDefaultCloseOperation(EXIT_ON_CLOSE);}privateJComponentmontaSlider(){JPaneljpSlider=newJPanel();jpSlider.add(campo);campo.setPreferredSize(newDimension(100,20));campo.addActionListener(newActionListener()//Caixaconsultaéocampoqueédigitadoocódigo.{@OverridepublicvoidactionPerformed(ActionEvente){KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(newKeyEventDispatcher(){@OverridepublicbooleandispatchKeyEvent(KeyEvente){if(e.getID()==KeyEvent.KEY_RELEASED&&e.getKeyCode()==KeyEvent.VK_ENTER){definePonteiro();}else{}returnfalse;}});}});intmajorSpacing=slider.getMaximum()/(COLORS.length-1);Dictionary<Integer,JLabel>dictionary=newHashtable<>();slider.setMajorTickSpacing(majorSpacing);slider.setPaintLabels(true);slider.setPaintTicks(true);slider.setSnapToTicks(true);for(inti=0;i<COLORS.length;i++){ImageIconicon=createColorIcon(COLORS[i]);JLabellabel=newJLabel(icon);intkey=i*majorSpacing;dictionary.put(key,label);}slider.setLabelTable(dictionary);jpSlider.add(slider,BorderLayout.CENTER);returnjpSlider;}privateImageIconcreateColorIcon(Colorcolor){BufferedImageimg=newBufferedImage(COMPRIMENTO,ALTURA,BufferedImage.TYPE_INT_RGB);Graphicsg=img.getGraphics();g.setColor(color);g.fillRect(0,0,COMPRIMENTO,ALTURA);g.dispose();returnnewImageIcon(img);}publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(newRunnable(){@Overridepublicvoidrun(){Sliders=newSlider();}});}publicvoiddefinePonteiro(){valor=Double.valueOf(campo.getText());if(valor<=11.5){//setaoponteirona1ªcorSystem.out.println("1ª");
} else if (valor <= 20) {
//seta o ponteiro na 2ª cor
System.out.println("2ª");
} else if (valor <= 30) {
//seta o ponteiro na 3 cor
System.out.println("3ª");
} else if (valor <= 40) {
//seta o ponteiro na 4ª cor
System.out.println("4ª");
} else if (valor <= 50) {
//seta o ponteiro na 5ª cor
System.out.println("5ª");
}
}
}