public class JavaExam {
public static void main(String[] args) {
new P07_ComboBox();
}
}
class P07_ComboBox extends JFrame{
public P07_ComboBox() {
run();
}
private void run() {
JPanel panel = new JPanel();
String[] items = new String[] {"빨강","주황","노랑","초록","파랑","남색","보라"};
JComboBox<String> combo = new JComboBox<>(items);
combo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox<String> cb = (JComboBox<String>)e.getSource();
int index = cb.getSelectedIndex();
JOptionPane.showMessageDialog(null, ""+items[index]+":"+index);
}
});
panel.add(combo);
add(panel);
setLayout(new FlowLayout());
setTitle("P07 ComboBox");
setBounds(700, 300, 300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class P06_List extends JFrame implements ListSelectionListener {
public P06_List() {
run();
}
private void run() {
JPanel jPanel = new JPanel();
String[] hobbies = new String[] {"잠자기","먹기","유튜브","넷플릭스","게임"};
JList<String> list = new JList<String>(hobbies);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(this);
jPanel.add(list);
add(jPanel);
setBounds(700, 300, 300, 300);
setTitle("P06 List");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void valueChanged(ListSelectionEvent e) {
JList list = (JList)e.getSource();
if(!e.getValueIsAdjusting())
System.out.println(list.getSelectedValue());
}
}
public class JavaExam {
public static void main(String[] args) {
new P06_List();
}
}
class P06_List extends JFrame{
public P06_List() {
run();
}
private void run() {
JPanel jPanel = new JPanel();
String[] hobbies = new String[] {"잠자기","먹기","유튜브","넷플릭스","게임"};
JList<String> list = new JList<String>(hobbies);
jPanel.add(list);
add(jPanel);
setBounds(700, 300, 300, 300);
setTitle("P06 List");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class JavaExam {
public static void main(String[] args) {
new P05_Radio();
}
}
class P05_Radio extends JFrame{
JRadioButton radio1,radio2,radio3;
public P05_Radio() {
run();
}
private void run() {
JPanel jPanel = new JPanel();
//라디오 버튼 생성(컴포넌트) 생성
radio1 = new JRadioButton("게임",true);
radio1.addItemListener(new MyItemListener());
radio2 = new JRadioButton("넷플릭스");
radio2.addItemListener(new MyItemListener());
radio3 = new JRadioButton("유튜브");
radio3.addItemListener(new MyItemListener());
//라디오 버튼은 버튼 그룹에 묶여 있어야 함
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radio1);
buttonGroup.add(radio2);
buttonGroup.add(radio3);
// 라디오 버튼을 패널에 붙임
jPanel.add(radio1);
jPanel.add(radio2);
jPanel.add(radio3);
add(jPanel);
setTitle("P05 Radio");
setBounds(700,300,300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyItemListener implements ItemListener{
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.DESELECTED) {
return;
}
JRadioButton rbtn = (JRadioButton)e.getSource();
if(rbtn.getText().equals("게임"))
JOptionPane.showMessageDialog(null, ""+rbtn.getText());
if(rbtn.getText().equals("넷플릭스"))
JOptionPane.showMessageDialog(null, ""+rbtn.getText());
if(rbtn.getText().equals("유튜브"))
JOptionPane.showMessageDialog(null, ""+rbtn.getText());
}
}
public class JavaExam {
public static void main(String[] args) {
new P05_Radio();
}
}
class P05_Radio extends JFrame{
JRadioButton radio1,radio2,radio3;
public P05_Radio() {
run();
}
private void run() {
JPanel jPanel = new JPanel();
//라디오 버튼 생성(컴포넌트) 생성
radio1 = new JRadioButton("게임",true);
radio2 = new JRadioButton("넷플릭스");
radio3 = new JRadioButton("유튜브");
//라디오 버튼은 버튼 그룹에 묶여 있어야 함
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radio1);
buttonGroup.add(radio2);
buttonGroup.add(radio3);
// 라디오 버튼을 패널에 붙임
jPanel.add(radio1);
jPanel.add(radio2);
jPanel.add(radio3);
add(jPanel);
setTitle("P05 Radio");
setBounds(700,300,300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class JavaExam {
public static void main(String[] args) {
new P04_CheckBox2();
}
}
class P04_CheckBox2 extends JFrame{
JLabel label;
JCheckBox check1, check2, check3;
JButton button;
public P04_CheckBox2() {
run();
}
private void run() {
label = new JLabel("<html><body text='red'><h1>주문할 상품을 선택하세요</h1></html>");
label.setBounds(50, 50, 300, 30);
check1 = new JCheckBox("버거킹 와퍼 Set 7900원");
check1.setBounds(100,100,220,20);
check2 = new JCheckBox("맥도날드 빅맥 Set 5500원");
check2.setBounds(100,150,220,20);
check3 = new JCheckBox("맘스터치 싸이버거 Set 6700원");
check3.setBounds(100,200,220,20);
button = new JButton("주문");
button.setBounds(100, 300, 200, 30);
button.addActionListener(new ActionListener() {
private String whatselected(String str) {
if(check1.isSelected()) str += check1.getText()+"\n";
if(check2.isSelected()) str += check2.getText()+"\n";
if(check3.isSelected()) str += check3.getText()+"\n";
return str;
}
private int whatselected() {
int total_price = 0;
if(check1.isSelected()) total_price +=7900;
if(check2.isSelected()) total_price +=5500;
if(check3.isSelected()) total_price +=6700;
return total_price;
}
public void actionPerformed(ActionEvent e) {
String str="";
str = whatselected(str);
int price = whatselected();
str += "----------------------------------\n";
JOptionPane.showMessageDialog(null, str+"합계: "+price+"원");
}
});
add(label);
add(check1);add(check2); add(check3);
add(button);
setTitle("햄버거 키오스크");
setBounds(700,300,400,400);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class JavaExam {
public static void main(String[] args) {
new P03_CheckBox();
}
}
class P03_CheckBox extends JFrame{
public P03_CheckBox() {
run();
}
private void run() {
// 패널(컨테이너) 생성
JPanel jpanel = new JPanel();
// 체크박스 생성
JCheckBox checkBox1 = new JCheckBox("등산"); //체크된 상태로
JCheckBox checkBox2 = new JCheckBox("수영");
JCheckBox checkBox3 = new JCheckBox("낚시");
JCheckBox checkBox4 = new JCheckBox("공연관람");
JCheckBox checkBox5 = new JCheckBox("영화보기");
JButton button = new JButton("확인");
button.addActionListener(new ActionListener() {
private String whatselected(String str) {
if(checkBox1.isSelected())str += checkBox1.getText()+" ";
if(checkBox2.isSelected())str += checkBox2.getText()+" ";
if(checkBox3.isSelected())str += checkBox3.getText()+" ";
if(checkBox4.isSelected())str += checkBox4.getText()+" ";
if(checkBox5.isSelected())str += checkBox5.getText()+" ";
return str;
}
public void actionPerformed(ActionEvent e) {
String str = "";
str = whatselected(str);
JOptionPane.showMessageDialog(null, str);
}
});
// 체크박스를 패널(컨테이너)에 붙이기
jpanel.add(checkBox1);
jpanel.add(checkBox2);
jpanel.add(checkBox3);
jpanel.add(checkBox4);
jpanel.add(checkBox5);
jpanel.add(button);
add(jpanel);
setTitle("예제3-체크박스");
setBounds(100, 100, 350, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class JavaExam {
public static void main(String[] args) {
new P03_CheckBox();
}
}
class P03_CheckBox extends JFrame{
public P03_CheckBox() {
run();
}
private void run() {
// 패널(컨테이너) 생성
JPanel jpanel = new JPanel();
// 체크박스 생성
JCheckBox checkBox1 = new JCheckBox("등산"); //체크된 상태로
checkBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (checkBox1.isSelected()) {
System.out.println("등산선택");
JOptionPane.showMessageDialog(null,"등산 선택됨");
}
else {
System.out.println("등산해제");
JOptionPane.showMessageDialog(null,"등산 해제");
}
}
});
JCheckBox checkBox2 = new JCheckBox("수영");
JCheckBox checkBox3 = new JCheckBox("낚시");
JCheckBox checkBox4 = new JCheckBox("공연관람");
JCheckBox checkBox5 = new JCheckBox("영화보기");
JButton button = new JButton("확인");
// 체크박스를 패널(컨테이너)에 붙이기
jpanel.add(checkBox1);
jpanel.add(checkBox2);
jpanel.add(checkBox3);
jpanel.add(checkBox4);
jpanel.add(checkBox5);
jpanel.add(button);
add(jpanel);
setTitle("예제3-체크박스");
setBounds(100, 100, 350, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class JavaExam {
public static void main(String[] args) {
new P02_Button();
}
}
class P02_Button{ // JFrame을 상속받지 않은 예제
public P02_Button() {
run();
}
private void run() {
JFrame jFrame = new JFrame();
JPanel panel = new JPanel();
// 버튼 컴포넌트를 생성
JButton button1 = new JButton("버튼 1");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "버튼 1클릭");
}
});
JButton button2 = new JButton("버튼 2");
button2.addActionListener(e -> {
JOptionPane.showMessageDialog(null, "버튼 2클릭");
});
JButton button3 = new JButton("버튼 3");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "버튼 3클릭");
}
});
// 버튼 컴포넌트를 패널(컨테이너)에 붙이기
panel.add(button1);
panel.add(button2);
panel.add(button3);
// 패널을 프레임에 붙이기
jFrame.add(panel);
jFrame.setTitle("예제2-버튼");
jFrame.setBounds(100, 100, 400, 300);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JavaExam {
public static void main(String[] args) {
new P02_Button();
}
}
class P02_Button{ // JFrame을 상속받지 않은 예제
public P02_Button() {
run();
}
private void run() {
JFrame jFrame = new JFrame();
JPanel panel = new JPanel();
// 버튼 컴포넌트를 생성
JButton button1 = new JButton("버튼 1");
JButton button2 = new JButton("버튼 2");
JButton button3 = new JButton("버튼 3");
// 버튼 컴포넌트를 패널(컨테이너)에 붙이기
panel.add(button1);
panel.add(button2);
panel.add(button3);
// 패널을 프레임에 붙이기
jFrame.add(panel);
jFrame.setTitle("예제2-버튼");
jFrame.setBounds(100, 100, 400, 300);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.JFrame;
public class JavaExam {
public static void main(String[] args) {
new P01_Frame();
}
}
class P01_Frame extends JFrame{
public P01_Frame() {
run();
}
private void run() {
setTitle("예제1");
// setLocation(100, 100);
// setSize(300, 200);
setBounds(100, 100, 400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
'PROGRAM > JAVA' 카테고리의 다른 글
GUI #2 (0) | 2022.08.30 |
---|---|
CSV파일 처리 (0) | 2022.08.10 |
Intellij 시작하기 (0) | 2020.11.09 |
JDK 1.8.0_271 설치하기 (0) | 2020.10.21 |
Thread TimerEx (0) | 2020.06.20 |