오늘의코드
UI관련 코드
package edu.java.gui01;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JLabel;
public class GuiMain01 {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain01 window = new GuiMain01();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GuiMain01() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
JLabel northlbl = new JLabel("North");
frame.getContentPane().add(northlbl, BorderLayout.NORTH);
JLabel westlbl = new JLabel("West");
frame.getContentPane().add(westlbl, BorderLayout.WEST);
JLabel centertlbl = new JLabel("Center");
frame.getContentPane().add(centertlbl, BorderLayout.CENTER);
JLabel eastlbl = new JLabel("East");
frame.getContentPane().add(eastlbl, BorderLayout.EAST);
JLabel southlbl = new JLabel("South");
frame.getContentPane().add(southlbl, BorderLayout.SOUTH);
} // end main()
} // end GuiMain01
package edu.java.gui02;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
public class GuiMain02 {
private JFrame frame;
private JTextField textInput;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain02 window = new GuiMain02();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain02() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblOutput = new JLabel("오늘은 월요일 입니다.");
lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
lblOutput.setFont(new Font("맑은 고딕", Font.BOLD, 18));
lblOutput.setForeground(Color.BLUE);
lblOutput.setBounds(12, 10, 410, 72);
frame.getContentPane().add(lblOutput);
// 이벤트 리스너에서 접근하는 변수들은
// 멤버 변수로 선언하는 것이 좋음
textInput = new JTextField();
textInput.setBounds(22, 72, 400, 62);
textInput.setFont(new Font("맑은 고딕", Font.PLAIN, 24));
frame.getContentPane().add(textInput);
textInput.setColumns(10);
JButton btnInput = new JButton("입력");
btnInput.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// 버튼을 클릭하면
// JTextField에 입력된 내용을 읽어서
// JLabel에 작성
String msg = textInput.getText();
System.out.println(msg);
lblOutput.setText(msg);
}
});
btnInput.setBackground(Color.LIGHT_GRAY);
btnInput.setForeground(Color.BLUE);
btnInput.setBounds(12, 145, 410, 81);
btnInput.setFont(new Font("맑은 고딕", Font.PLAIN, 18));
frame.getContentPane().add(btnInput);
System.out.println("initialize 메소드 호출 끝");
}
}
package edu.java.gui03;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class GuiMain03 {
private JFrame frame;
private JTextField txtName;
private JTextField txtPhone;
private JTextField txtEmail;
private JButton btnInsert;
private JTextField txtOutput;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain03 window = new GuiMain03();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain03() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 500, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); // Absolute layout
int lblWidth = 245; // 레이블 넓이
int lblHeight = 76; // 레이블 높이
Font lblFont = new Font("굴림", Font.BOLD, 48);
JLabel lblName = new JLabel("이름");
lblName.setOpaque(true);
lblName.setHorizontalAlignment(SwingConstants.CENTER);
lblName.setBackground(Color.PINK);
lblName.setBounds(12, 10, lblWidth, lblHeight);
lblName.setFont(lblFont);
frame.getContentPane().add(lblName);
JLabel lblPhone = new JLabel("전화번호");
lblPhone.setOpaque(true);
lblPhone.setHorizontalAlignment(SwingConstants.CENTER);
lblPhone.setFont(lblFont);
lblPhone.setBackground(Color.ORANGE);
lblPhone.setBounds(12, 96, lblWidth, lblHeight);
frame.getContentPane().add(lblPhone);
JLabel lblEmail = new JLabel("이메일");
lblEmail.setOpaque(true);
lblEmail.setHorizontalAlignment(SwingConstants.CENTER);
lblEmail.setFont(lblFont);
lblEmail.setBackground(new Color(30, 144, 255));
lblEmail.setBounds(12, 182, lblWidth, lblHeight);
frame.getContentPane().add(lblEmail);
int txtWidth = 203;
int txtHeight = 76;
Font txtFont = new Font("굴림", Font.PLAIN, 30);
txtName = new JTextField();
txtName.setBounds(269, 10, txtWidth, txtHeight);
txtName.setFont(txtFont);
frame.getContentPane().add(txtName);
txtName.setColumns(10);
txtPhone = new JTextField();
txtPhone.setFont(txtFont);
txtPhone.setColumns(10);
txtPhone.setBounds(269, 96, txtWidth, txtHeight);
frame.getContentPane().add(txtPhone);
txtEmail = new JTextField();
txtEmail.setFont(txtFont);
txtEmail.setColumns(10);
txtEmail.setBounds(269, 182, txtWidth, txtHeight);
frame.getContentPane().add(txtEmail);
btnInsert = new JButton("정보 출력");
btnInsert.setFont(txtFont);
btnInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = txtName.getText();
String phone = txtPhone.getText();
String email = txtEmail.getText();
String msg = "이름 : " + name + "\n"
+ "전화번호 : " + phone + "\n"
+ "이메일 : " + email + "\n";
txtOutput.setText(msg);
// textArea.setText(msg);
textArea.append(msg); // 문자열을 연결해서 출력
}
});
btnInsert.setBounds(12, 268, 460, 76);
frame.getContentPane().add(btnInsert);
txtOutput = new JTextField();
txtOutput.setFont(txtFont);
txtOutput.setBounds(12, 354, 460, 76);
frame.getContentPane().add(txtOutput);
txtOutput.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 440, 460, 161);
frame.getContentPane().add(scrollPane);
textArea = new JTextArea();
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
}
} // end GuiMain03
package edu.java.gui04;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GuiMain04 {
private JFrame frame;
private JTextField textNumber1;
private JTextField textNumber2;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain04 window = new GuiMain04();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GuiMain04() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 700, 550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Font frameFont = new Font("굴림", Font.BOLD | Font.ITALIC, 48);
JLabel lblNumber1 = new JLabel("Number 1");
lblNumber1.setBounds(12, 10, 280, 70);
lblNumber1.setFont(frameFont);
frame.getContentPane().add(lblNumber1);
JLabel lblNumber2 = new JLabel("Number 2");
lblNumber2.setFont(new Font("굴림", Font.BOLD | Font.ITALIC, 48));
lblNumber2.setBounds(12, 90, 280, 70);
frame.getContentPane().add(lblNumber2);
textNumber1 = new JTextField();
textNumber1.setBounds(304, 10, 368, 70);
textNumber1.setFont(frameFont);
frame.getContentPane().add(textNumber1);
textNumber1.setColumns(10);
textNumber2 = new JTextField();
textNumber2.setColumns(10);
textNumber2.setFont(frameFont);
textNumber2.setBounds(304, 90, 368, 70);
frame.getContentPane().add(textNumber2);
JButton btnPlus = new JButton("+");
btnPlus.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("btnPlus : mouseClicked()");
String num1 = textNumber1.getText();
String num2 = textNumber2.getText();
// 문자열을 숫자로 변환
double x = Double.parseDouble(num1);
double y = Double.parseDouble(num2);
String result = x + " + " + y + " = " + (x + y) + "\n";
textArea.append(result);
}
});
btnPlus.setBounds(12, 170, 100, 100);
btnPlus.setFont(frameFont);
frame.getContentPane().add(btnPlus);
JButton btnMinus = new JButton("-");
btnMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("btnMinus : actionPerformed()");
String num1 = textNumber1.getText();
String num2 = textNumber2.getText();
// 문자열을 숫자로 변환
double x = Double.parseDouble(num1);
double y = Double.parseDouble(num2);
String result = x + " - " + y + " = " + (x - y) + "\n";
textArea.append(result);
}
});
btnMinus.setFont(new Font("굴림", Font.BOLD | Font.ITALIC, 48));
btnMinus.setBounds(192, 170, 100, 100);
frame.getContentPane().add(btnMinus);
JButton btnMultiple = new JButton("*");
btnMultiple.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("btnMultiple : keyPressed() ");
System.out.println(e.getKeyChar());
System.out.println(e.getKeyCode());
System.out.println("엔터키 누름");
String num1 = textNumber1.getText();
String num2 = textNumber2.getText();
// 문자열을 숫자로 변환
double x = Double.parseDouble(num1);
double y = Double.parseDouble(num2);
String result = x + " * " + y + " = " + (x * y) + "\n";
textArea.append(result);
}
});
btnMultiple.setFont(new Font("굴림", Font.BOLD | Font.ITALIC, 48));
btnMultiple.setBounds(386, 170, 100, 100);
frame.getContentPane().add(btnMultiple);
JButton btnDivide = new JButton("/");
btnDivide.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("btnDivdie : mouesClicked()");
String num1 = textNumber1.getText();
String num2 = textNumber2.getText();
// 문자열을 숫자로 변환
double x = Double.parseDouble(num1);
double y = Double.parseDouble(num2);
String result = x + " / " + y + " = " + (x / y) + "\n";
textArea.append(result);
}
});
btnDivide.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
}
});
btnDivide.setFont(new Font("굴림", Font.BOLD | Font.ITALIC, 48));
btnDivide.setBounds(572, 170, 100, 100);
frame.getContentPane().add(btnDivide);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 280, 660, 221);
frame.getContentPane().add(scrollPane);
textArea = new JTextArea();
textArea.setFont(new Font("Monospaced", Font.PLAIN, 48));
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
}
}
package edu.java.gui05;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextArea;
public class GuiMain05 {
private JFrame frame;
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain05 window = new GuiMain05();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain05() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea textArea = new JTextArea();
JRadioButton rdbtnAgree = new JRadioButton("동의");
rdbtnAgree.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.setText(rdbtnAgree.getText());
}
});
buttonGroup.add(rdbtnAgree);
rdbtnAgree.setSelected(true);
rdbtnAgree.setBounds(8, 6, 121, 23);
frame.getContentPane().add(rdbtnAgree); // 판에다가 그려야하기에 frame이 없으면 안됨
JRadioButton rdbtnReject = new JRadioButton("거절");
rdbtnReject.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.setText(rdbtnReject.getText());
}
});
buttonGroup.add(rdbtnReject);
rdbtnReject.setBounds(133, 6, 121, 23);
frame.getContentPane().add(rdbtnReject);
textArea.setBounds(8, 46, 414, 205);
frame.getContentPane().add(textArea);
}
}
package edu.java.gui06;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JButton;
public class GuiMain06 {
private JFrame frame;
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain06 window = new GuiMain06();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiMain06() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(8, 63, 414, 188);
frame.getContentPane().add(textArea);
JRadioButton rdbtnMale = new JRadioButton("남자");
buttonGroup.add(rdbtnMale);
rdbtnMale.setSelected(true);
rdbtnMale.setBounds(8, 6, 121, 23);
frame.getContentPane().add(rdbtnMale);
JRadioButton rdbtnFemale = new JRadioButton("여자");
buttonGroup.add(rdbtnFemale);
rdbtnFemale.setBounds(181, 6, 121, 23);
frame.getContentPane().add(rdbtnFemale);
JButton btnCheck = new JButton("확인");
btnCheck.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 남자 선택인 경우 -> 확인버튼 클릭 -> textArea에 "성별 : 남자" 출력
if(rdbtnMale.isSelected()) {
textArea.setText(rdbtnMale.getText());
}
// 여자 선택인 경우 -> 확인버튼 클릭 -> textArea에 "성별 : 여자" 출력
else {
textArea.setText(rdbtnFemale.getText());
}
}
});
btnCheck.setBounds(8, 30, 97, 23);
frame.getContentPane().add(btnCheck);
}
}
package edu.java.gui07;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuiMain07 {
private JFrame frame;
private static final String[] STRINGS = { "1. 김근수", "2. 김시훈", "3. 김주영", "4. 노성욱", "5. 박정민", "6. 신중비", "7. 양준혁",
"8. 우제영", "9. 위승혁", "10. 이현우", "11. 조현", "12. 최정현", "13. 홍지안", "14. 김한빈", "15. 정의원", "16. 임창준", "17. 민예인",
"18. 박혜준", "19. 권보성" };
private int index = 0;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain07 window = new GuiMain07();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GuiMain07() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 465, 646);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblOutput = new JLabel();
lblOutput.setText(STRINGS[0]);
lblOutput.setFont(new Font("굴림", Font.BOLD, 42));
lblOutput.setBounds(12, 10, 423, 216);
frame.getContentPane().add(lblOutput);
JButton btnPrev = new JButton("이전");
btnPrev.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (index > 0) {
index--;
} else {
index = STRINGS.length - 1;
}
lblOutput.setText(STRINGS[index]);
}
});
btnPrev.setBounds(12, 230, 204, 247);
frame.getContentPane().add(btnPrev);
JButton btnNext = new JButton("다음");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (index < STRINGS.length - 1) {
index++;
} else { // 초기화
index = 0;
}
lblOutput.setText(STRINGS[index]);
}
});
btnNext.setBounds(224, 230, 211, 247);
frame.getContentPane().add(btnNext);
JButton btnNewButton = new JButton("랜덤");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double rX = Math.random();
int x = (int) (19 * rX);
lblOutput.setText(STRINGS[x]);
}
});
btnNewButton.setBounds(173, 518, 97, 23);
frame.getContentPane().add(btnNewButton);
}
}
package edu.java.gui08;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuiMain08 {
private JFrame frame;
private static final ImageIcon[] IMAGE_ICONS = {
new ImageIcon("res/dog1.jpg"),
new ImageIcon("res/dog2.jpg"),
new ImageIcon("res/dog3.jpg"),
new ImageIcon("res/dog4.jpg")
};
private int index = 0;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiMain08 window = new GuiMain08();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GuiMain08() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 465, 646);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblOutput = new JLabel();
lblOutput.setIcon(IMAGE_ICONS[0]);
lblOutput.setFont(new Font("굴림", Font.BOLD, 42));
lblOutput.setBounds(12, 10, 423, 216);
frame.getContentPane().add(lblOutput);
JButton btnPrev = new JButton("이전");
btnPrev.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (index > 0) {
index--;
} else {
index = IMAGE_ICONS.length - 1;
}
lblOutput.setIcon(IMAGE_ICONS[index]);
}
});
btnPrev.setBounds(12, 230, 204, 247);
frame.getContentPane().add(btnPrev);
JButton btnNext = new JButton("다음");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (index < IMAGE_ICONS.length - 1) {
index++;
} else { // 초기화
index = 0;
}
lblOutput.setIcon(IMAGE_ICONS[index]);
}
});
btnNext.setBounds(224, 230, 211, 247);
frame.getContentPane().add(btnNext);
JButton btnNewButton = new JButton("랜덤");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double rX = Math.random();
int x = (int) (IMAGE_ICONS.length * rX);
lblOutput.setIcon(IMAGE_ICONS[x]);
}
});
btnNewButton.setBounds(173, 518, 97, 23);
frame.getContentPane().add(btnNewButton);
}
}