package sdmexpert.backup.gui; import java.awt.*; import javax.swing.event.*; import javax.swing.*; public class Zippo extends JFrame implements ChangeListener{ int i=0,j=0,k=0; private Color color=new Color(i,j,k); private JPanel panel,panelDown; private JLabel lbl[]=new JLabel[3]; private JSlider sld[]=new JSlider[3]; private JTextField tf[]=new JTextField[3]; Zippo(){ super("颜色选择"); setSize(350,300); setLocationRelativeTo(null); //设置 窗体最初出现的位置 setLayout(new GridLayout(2,1)); setResizable(false); panel=new JPanel(); panel.setBackground(Color.BLACK); panel.setBorder(BorderFactory.createTitledBorder(null,"面板")); add(panel); panelDown=new JPanel(); panelDown.setBorder(BorderFactory.createTitledBorder(null,"颜色控制")); lbl[0]=new JLabel("红色:"); lbl[1]=new JLabel("绿色:"); lbl[2]=new JLabel("蓝色:"); for(int index=0;index<3;index++){ sld[index]=new JSlider(0,255,0); tf[index]=new JTextField("0",3); tf[index].setEditable(false); // 设置为不可编辑 panelDown.add(lbl[index]); panelDown.add(sld[index]); panelDown.add(tf[index]); sld[index].addChangeListener(this); } add(panelDown); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void setColor(int index1){ tf[index1].setText(String.valueOf(sld[index1].getValue())); i=Integer.parseInt(tf[0].getText()); j=Integer.parseInt(tf[1].getText()); k=Integer.parseInt(tf[2].getText()); color=new Color(i,j,k); panel.setBackground(color); } public void stateChanged(ChangeEvent e){ if(e.getSource()==sld[0]){ setColor(0); }else if(e.getSource()==sld[1]){ setColor(1); }else if(e.getSource()==sld[2]){ setColor(2); } } public static void main(String args[]){ new Zippo(); } }