mojo's Blog
명품 JAVA Programming 제 14장 실습문제 (4, 6번) 본문
4번 Code
package Part14;
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Random;
import java.util.Vector;
import java.awt.*;
public class Problem14_4 extends JFrame{
private JTextField tf = new JTextField(20);
private Container cp;
private void createToolBar() {
JToolBar toolBar = new JToolBar("Menu");
toolBar.setBackground(Color.LIGHT_GRAY);
toolBar.add(new JLabel("학번 :"));
toolBar.add(tf);
tf.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char key = e.getKeyChar();
if(key<'0' || key>'9') {
JOptionPane.showMessageDialog(null, key + "는 숫자 키가 아닙니다.\n숫자를 입력하세요.",
"경고", JOptionPane.ERROR_MESSAGE);
e.consume();
}
}
});
cp.add(toolBar,BorderLayout.SOUTH);
}
Problem14_4(){
setTitle("14장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cp = getContentPane();
createToolBar();
setSize(400, 300);
setVisible(true);
setFocusable(true);
requestFocus();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Problem14_4();
}
}
6번 코드
package Part14;
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Random;
import java.util.Vector;
import java.awt.*;
class MyDialog1 extends JDialog{
private JLabel jl = new JLabel("두 수를 더합니다.");
private JTextField tf1 = new JTextField(15);
private JTextField tf2 = new JTextField(15);
private JButton jb = new JButton("Add");
MyDialog1(JFrame frame, String title){
super(frame, title, true);
setLayout(new FlowLayout());
add(jl);
add(tf1);
add(tf2);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
add(jb);
setSize(200,300);
}
int getValue1() {
int x = Integer.parseInt(tf1.getText());
return x;
}
int getValue2() {
int x = Integer.parseInt(tf2.getText());
return x;
}
}
public class Problem14_6 extends JFrame{
private JButton jbtn = new JButton("Calculate");
private JLabel jl = new JLabel("계산 결과 출력");
private MyDialog1 dialog;
Problem14_6(){
setTitle("14장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER));
dialog = new MyDialog1(this, "14장 연습한다.");
jbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);
int x = dialog.getValue1();
int y = dialog.getValue2();
jl.setText(Integer.toString(x+y));
}
});
c.add(jbtn);
jl.setOpaque(true);
jl.setBackground(Color.GREEN);
c.add(jl);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Problem14_6();
}
}
'Java' 카테고리의 다른 글
Socket Programming (0) | 2021.08.23 |
---|---|
네트워크 TCP/IP (0) | 2021.08.23 |
명품 JAVA Programming 제 14장 Open Challenge (0) | 2021.08.18 |
팝업 다이얼로그 / 탭팬 (0) | 2021.08.18 |
고급 스윙 컴포넌트(메뉴, 툴바, 툴팁, 다이얼로그) (0) | 2021.08.18 |
Comments