티스토리 뷰
반응형
✏️ 11장 실습문제 - 1번
package ex;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame{
public SolveProblem(){
setTitle("CheckBox Practice");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c =getContentPane();
c.setLayout(new FlowLayout());
JCheckBox checkBox1 = new JCheckBox("버튼 비활성화");
JCheckBox checkBox2 = new JCheckBox("버튼 감추기");
JButton button = new JButton("test button");
c.add(checkBox1);
c.add(checkBox2);
c.add(button);
checkBox1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
JCheckBox checkBox = (JCheckBox)e.getSource();
if(checkBox.isSelected()==true)
button.setEnabled(false);
else
button.setEnabled(true);
}
});
checkBox2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
JCheckBox checkBox = (JCheckBox)e.getSource();
if(checkBox.isSelected()==true)
button.setVisible(false);
else
button.setVisible(true);
}
});
setVisible(true);
setSize(300,150);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 11장 실습문제 - 2번
package ex;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame{
public SolveProblem(){
setTitle("JTextFiled and Jcomponent");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c =getContentPane();
c.setLayout(new FlowLayout());
JTextField jTextField = new JTextField(20);
JComboBox<String> jComboBox = new JComboBox<>();
c.add(jTextField);
c.add(jComboBox);
jTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JTextField text = (JTextField)e.getSource();
String string = text.getText();
jComboBox.addItem(string);
}
});
setVisible(true);
setSize(300,150);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 11장 실습문제 - 3번
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
private JTextField priceTextField = new JTextField(12);
private String[] texts = { "오만원", "만원", "천원", "500원", "100원", "50원", "10원", "1원" };
private int[] money = { 50000, 10000, 1000, 500, 100, 50, 10, 1 };
private JTextField[] outputs = new JTextField[8];
public SolveProblem() {
setTitle("Money Charger");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
c.setBackground(Color.PINK);
JLabel priceLabel = new JLabel("금액");
priceLabel.setSize(65, 50);
priceLabel.setHorizontalAlignment(SwingConstants.RIGHT);
c.add(priceLabel);
priceTextField.setBounds(90, 15, 100, 20);
c.add(priceTextField);
JButton btn = new JButton("계산");
btn.setBounds(200, 15, 70, 20);
c.add(btn);
for (int i = 0; i < texts.length; i++) {
JLabel price = new JLabel(texts[i]);
price.setHorizontalAlignment(SwingConstants.RIGHT);
price.setBounds(50, 45 + i * 20, 40, 20);
c.add(price);
outputs[i] = new JTextField(25);
outputs[i].setBounds(110, 45 + i * 20, 70, 20);
outputs[i].setHorizontalAlignment(SwingConstants.CENTER);
c.add(outputs[i]);
}
btn.addActionListener(new MyActionListener());
setSize(300, 300);
setVisible(true);
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int total = Integer.valueOf(priceTextField.getText());
for (int i = 0; i < money.length; i++) {
outputs[i].setText(Integer.toString(total / money[i]));
total = total % money[i];
}
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 11장 실습문제 - 4번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class SolveProblem extends JFrame {
private String [] strings = {"오만원","만원","천원","500원","100원","50원","10원","1원"};
private int [] moneys= {50000,10000,1000,500,100,50,10,1};
private JTextField [] moneyText = new JTextField[8];
private JCheckBox [] checkBoxes = new JCheckBox[8];
private JLabel [] moneyLabel = new JLabel[8];
private int sum;
public SolveProblem(){
setTitle("Money Changer With Checkbox");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c =getContentPane();
c.setLayout(new BorderLayout());
c.setBackground(Color.PINK);
JPanel paenl1=new JPanel();
JLabel label = new JLabel("금액");
JTextField textField = new JTextField(15);
JButton jButton = new JButton("계산");
paenl1.add(label);
paenl1.add(textField);
paenl1.add(jButton);
paenl1.setBackground(Color.PINK);
c.add(paenl1,BorderLayout.NORTH);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Integer money = Integer.parseInt(textField.getText());
for(int i=0;i<7;i++){
if(checkBoxes[i].isSelected()){
Integer q = money/moneys[i];
moneyText[i].setText(Integer.toString(q));
money %= moneys[i];
}
else{
moneyText[i].setText("0");
}
}
moneyText[7].setText(Integer.toString(money));
}
});
// 중앙 부분 추가하기
JPanel panel2 = new JPanel();
panel2.setBackground(Color.PINK);
panel2.setLayout(new GridLayout(8,3));
for(int i=0;i<strings.length;i++){
moneyLabel[i] = new JLabel(strings[i]);
moneyText[i] = new JTextField();
panel2.add(moneyLabel[i]);
panel2.add(moneyText[i]);
if(i<7){
checkBoxes[i] = new JCheckBox();
checkBoxes[i].setBackground(Color.PINK);
panel2.add(checkBoxes[i]);
}
}
c.add(panel2,BorderLayout.CENTER);
JPanel west=new JPanel();
west.setBackground(Color.pink);
c.add(west,BorderLayout.WEST);
JPanel east=new JPanel();
east.setBackground(Color.pink);
c.add(west,BorderLayout.EAST);
JPanel south=new JPanel();
south.setBackground(Color.pink);
c.add(west,BorderLayout.SOUTH);
setVisible(true);
setSize(400,200);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 11장 실습문제 - 5번
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
private JLabel label;
public SolveProblem() {
setTitle("JSlider Practice Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JSlider slider = new JSlider(100,200,130);
slider.setPaintTrack(true);
slider.setPaintLabels(true);
slider.setMajorTickSpacing(20);
slider.setOrientation(JSlider.HORIZONTAL);
c.add(slider);
label = new JLabel(Integer.toString(slider.getValue()));
label.setOpaque(true);
label.setBackground(Color.GREEN);
c.add(label);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
label.setText(Integer.toString(slider.getValue()));
}
});
setVisible(true);
setSize(400,200);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 11장 실습문제 - 6번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
private JTextArea textArea;
private JSlider slider;
public SolveProblem() {
setTitle("TextArea Practice Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
textArea = new JTextArea(4,24);
c.add(textArea);
slider = new JSlider(JSlider.HORIZONTAL,0,100,0);
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(20);
slider.setPaintTicks(true);
slider.setPaintTrack(true);
slider.setPaintLabels(true);
c.add(slider);
textArea.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
JTextArea text = (JTextArea)e.getSource();
String s = text.getText();
if(s.length()<=100){
int len = s.length();
slider.setValue(len);
}
else{
s = s.substring(0,99);
text.setText(s);
}
}
});
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider jSlider = (JSlider)e.getSource();
String currentString = textArea.getText();
int len1 = currentString.length();
int len2 = jSlider.getValue();
if(len1>=len2){
currentString = currentString.substring(0,len2);
textArea.setText(currentString);
}
else{
jSlider.setValue(len1);
}
}
});
setVisible(true);
setSize(400,200);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 11장 실습문제 - 7번
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
public SolveProblem() {
setTitle("JSlider Practice Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout());
JSlider slider = new JSlider(JSlider.HORIZONTAL,1,100,50);
slider.setPaintLabels(true);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
slider.setPaintTrack(true);
slider.setPaintTicks(true);
c.add(slider,BorderLayout.NORTH);
JLabel label = new JLabel("I Love Java");
label.setFont(new Font("Arial",Font.ITALIC,slider.getValue()));
label.setHorizontalAlignment(SwingConstants.CENTER);
c.add(label,BorderLayout.CENTER);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider slider1 = (JSlider)e.getSource();
int size = slider1.getValue();
label.setFont(new Font("Arial",Font.ITALIC,size));
}
});
setVisible(true);
setSize(400,200);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 11장 실습문제 - 8번
package ex;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Vector;
public class Exercise extends JFrame {
private JRadioButton left = new JRadioButton("left");
private JRadioButton right = new JRadioButton("right");
private JLabel imageLabel;
private Vector<ImageIcon> imagesVector = new Vector<ImageIcon>();
public Exercise() {
setTitle("Image Gallery Practice Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout());
File fp = new File("images"); //images 폴더를 가리키는 파일 포인터 fp
File[] imageFiles = fp.listFiles(); //지정한 폴더에 있는 파일들을 File 배열에 저장
for (File f : imageFiles) {
if (f.isFile()) {
ImageIcon icon = new ImageIcon(f.getPath()); //getPath는 파일 경로를 리턴
imagesVector.add(icon); //벡터에 이미지를 추가한다
}
}
c.add(new MenuPanel(), BorderLayout.NORTH);
imageLabel=new JLabel(imagesVector.get(0)); //초기에는 인덱스 0의 이미지를 보이도록
c.add(imageLabel, BorderLayout.CENTER);
imageLabel.addMouseListener(new MyMouseListener(0)); //이미지 레이블에 Mouse 리스너 추가
setSize(500, 300);
setVisible(true);
}
class MenuPanel extends JPanel {
public MenuPanel() {
setLayout(new FlowLayout());
ButtonGroup g = new ButtonGroup();
g.add(left);
g.add(right); //버튼 그룹에 라디오 버튼 추가
add(left);
add(right);
left.setSelected(true); //기본으로 left 버튼을 선택 상태로 함
}
}
class MyMouseListener extends MouseAdapter {
private int index;
public MyMouseListener(int startIndex) {
index = startIndex;
//마우스 리스너 객체가 처음 생성될 때 지정한 시작 인덱스의 이미지를 초기 화면에 보인다
}
public void mouseReleased(MouseEvent e) { //버튼을 눌렀다 뗐을 때
if (left.isSelected()) { //왼쪽 버튼이 선택된 상태이면
index++; //인덱스 증가
if (index >= imagesVector.size())
index = 0;
//벡터 크기보다 크면 인덱스를 0으로 변경
} else { //오른쪽 버튼이 선택된 상태이면
index--; //인덱스 감소
if (index == -1)
index = imagesVector.size() - 1;
//인덱스가 -1이 되면 인덱스 값을 최대로 변경
}
imageLabel.setIcon(imagesVector.get(index));
//변경된 인덱스의 이미지를 이미지 라벨에 보이도록 설정
}
}
public static void main(String[] args) {
new Exercise();
}
}
✏️ 11장 실습문제 - 9번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class SolveProblem extends JFrame {
private ImageIcon[] imageIcons = { new ImageIcon("images/가위.jpg"),new ImageIcon("images/바위.jpg"),new ImageIcon("images/보.jpg")};
private JButton [] buttons = new JButton[3];
private JLabel me=new JLabel("me");
private JLabel com=new JLabel("com");
private JLabel result=new JLabel();
public SolveProblem() {
setTitle("가위 바위 보 게임");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout());
JPanel jPanel = new JPanel();
jPanel.setLayout(new FlowLayout());
jPanel.setBackground(Color.GRAY);
JPanel jPanel1 = new JPanel();
jPanel1.setLayout(new FlowLayout());
jPanel1.add(me);
jPanel1.add(com);
jPanel1.add(result);
jPanel1.setBackground(Color.YELLOW);
c.add(jPanel,BorderLayout.NORTH);
c.add(jPanel1,BorderLayout.CENTER);
for(int i=0;i<3;i++){
buttons[i] = new JButton(imageIcons[i]);
buttons[i].addActionListener(new MyAction(i));
jPanel.add(buttons[i]);
}
setVisible(true);
setSize(400,200);
}
class MyAction implements ActionListener{
private int index;
public MyAction(int i){
index=i;
}
@Override
public void actionPerformed(ActionEvent e) {
int i = (int)(Math.random()*3);
if(index==i) {
result.setText("Same !!!");
result.setForeground(Color.RED);
me.setIcon(imageIcons[i]);
com.setIcon(imageIcons[i]);
}
else if(index-i==1||index-i==-2){
result.setText("Me !!!");
result.setForeground(Color.RED);
me.setIcon(imageIcons[i]);
com.setIcon(imageIcons[i]);
}
else{
result.setText("Computer !!!");
result.setForeground(Color.RED);
me.setIcon(imageIcons[i]);
com.setIcon(imageIcons[i]);
}
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 11장 실습문제 - 10번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class SolveProblem extends JFrame {
private JLabel [] jLabel = new JLabel[10];
private int index = 0;
public SolveProblem() {
setTitle("Ten 레이블 클릭");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
for(int i=0;i<10;i++){
jLabel[i] = new JLabel(Integer.toString(i));
int x= (int)(Math.random()*300);
int y= (int)(Math.random()*300);
jLabel[i].setLocation(x,y);
jLabel[i].setSize(10,10);
c.add(jLabel[i]);
}
for(int i=0;i<10;i++) {
jLabel[i].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JLabel label = (JLabel) e.getSource();
int num = Integer.parseInt(label.getText());
if (num == index) {
label.setVisible(false);
index++;
} else
return;
}
});
}
setVisible(true);
setSize(400,200);
}
public static void main(String[] args) {
new SolveProblem();
}
}
반응형
'PL > JAVA' 카테고리의 다른 글
명품 자바 프로그래밍 14장 실습 문제 (0) | 2024.06.26 |
---|---|
명품 자바 프로그래밍 12장 실습 문제 (0) | 2024.06.25 |
명품 자바 프로그래밍 10장 실습 문제 (0) | 2024.06.21 |
명품 자바 프로그래밍 9장 실습 문제 (0) | 2024.06.21 |
명품 자바 프로그래밍 8장 실습 문제 (0) | 2024.06.21 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- html
- 반복문
- 스프링 부트 crud 게시판 구현
- 자바
- 투 포인터
- CSS
- 백준 풀이
- C++ Stack
- C++
- BFS
- 알고리즘
- HTML5
- Do it!
- 이분 매칭
- 자료구조
- 백준
- java
- 카운팅 정렬
- 스택
- DFS
- 에라토스테네스의 체
- 세그먼트 트리
- 유클리드 호제법
- 우선순위 큐
- DP
- c++ string
- 유니온 파인드
- js
- 자바스크립트
- 알고리즘 공부
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함