2025 04 13
//import javax.swing.*;
//import javax.swing.event.*;
//import java.awt.event.*;
//import java.awt.*;
//
//public class Main extends JFrame {
// private JRadioButton[] radio = new JRadioButton [2];
// private String[] text = {"LEFT", "RIGHT"};
// private ImageIcon[] image = {
// new ImageIcon("ai-generated-8612487_640.jpg"),
// new ImageIcon("sea-7315960_640.jpg"),
// new ImageIcon("sunset-7760143_640.jpg")};
// private JLabel imageLabel = new JLabel();
//
// int imagenum = 0;
// public Main() {
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Container c = getContentPane();
// c.setLayout(new BorderLayout());
// JPanel radioPanel = new JPanel();
// JPanel imagePanel = new JPanel();
// c.add(radioPanel, BorderLayout.NORTH);
// c.add(imagePanel, BorderLayout.CENTER);
//
// ButtonGroup g = new ButtonGroup();
// for(int i=0;i<radio.length;i++) {
// radio[i] = new JRadioButton(text[i]);
// g.add(radio[i]);
// radioPanel.add(radio[i]);
// radio[i].addItemListener(new MyItemListener());
// }
// imageLabel.setIcon(image[0]);
// imagePanel.add(imageLabel);
// setSize(350,300);
// setVisible(true);
// }
// class MyItemListener implements ItemListener{
//
// public void itemStateChanged(ItemEvent e) {
// if (e.getStateChange()==ItemEvent.SELECTED) {
//
// if (radio[0].isSelected()) {
// if (imagenum==0) imagenum=2;
// else imagenum--;
// }
// if (radio[1].isSelected()) {
// if (imagenum==2) imagenum=0;
// else imagenum++;
// }
// imageLabel.setIcon(image[imagenum]);
// }
// }
//
// }
//
// public static void main(String[] args) {
// new Main();
// }
//}
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class Main extends JFrame {
private JLabel colorLabel;
private JSlider [] sl = new JSlider [3];
private JLabel [] la = new JLabel[3];
public Main() {
setTitle("슬라이더와 ChangeEvent 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
colorLabel = new JLabel(" SLIDER EXAMPLE ");
for(int i=0;i<sl.length;i++) {
sl[i] = new JSlider(JSlider.HORIZONTAL,0,255,128);
sl[i].setPaintLabels(true);
sl[i].setPaintTicks(true);
sl[i].setPaintTrack(true);
sl[i].setMajorTickSpacing(50);
sl[i].setMinorTickSpacing(10);
sl[i].addChangeListener(new MyChangeListener());
la[i] = new JLabel("128");
c.add(sl[i]);
c.add(la[i]);
}
sl[0].setForeground(Color.RED);
sl[1].setForeground(Color.GREEN);
sl[2].setForeground(Color.BLUE);
int r = sl[0].getValue();
int g = sl[1].getValue();
int b = sl[2].getValue();
colorLabel.setOpaque(true);
colorLabel.setBackground(new Color(r,g,b));
c.add(colorLabel);
setSize(300,230);
setVisible(true);
}
class MyChangeListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
int r = sl[0].getValue();
int g = sl[1].getValue();
int b = sl[2].getValue();
colorLabel.setBackground(new Color(r,g,b));
la[0].setText(Integer.toString(r));
la[1].setText(Integer.toString(g));
la[2].setText(Integer.toString(b));
///////////////////////// 폰트크기조절///////////////
colorLabel.setFont(new Font("Arial",Font.PLAIN,r));
}
}
public static void main(String[] args) {
new Main();
}
}




