mojo's Blog
팝업 다이얼로그 / 탭팬 본문
팝업 다이얼로그는 스윙 패키지에 구현된 간단한 팝업 창으로 사용자에게 메시지를 전달하거나 간단한 문자열을 입력받는 유용한 다이얼로그다.
JOptionPane 을 사용하여 팝업 다이얼로그를 작성한 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 OptionPane extends JFrame{
class MyPanel extends Panel{
private JButton inputBtn = new JButton("Input Name");
private JTextField tf = new JTextField(10);
private JButton confirmBtn = new JButton("Confirm");
private JButton messageBtn = new JButton("Message");
public MyPanel() {
setBackground(Color.LIGHT_GRAY);
add(inputBtn);
add(tf);
add(confirmBtn);
add(messageBtn);
inputBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// input Dialog
String name = JOptionPane.showInputDialog("이름을 입력하세요.");
if(name!=null) {
tf.setText(name);
}
}
});
confirmBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null,
"계속할 것입니까?","Confirm", JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.CLOSED_OPTION) {
tf.setText("Just Closed without Selection");
}
else if(result == JOptionPane.YES_OPTION) {
tf.setText("Yes");
}
else {
tf.setText("No");
}
}
});
messageBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "조심하세요",
"Message", JOptionPane.ERROR_MESSAGE);
}
});
}
}
OptionPane(){
setTitle("14장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.add(new MyPanel(), BorderLayout.NORTH);
setSize(500, 200);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new OptionPane();
}
}
탭팬 만드는 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 TabbedPaneEx extends JFrame{
class MyPanel extends Panel {
public MyPanel() {
this.setBackground(Color.YELLOW);
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.RED);
g.fillRect(10, 10, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(10, 70, 50, 50);
g.setColor(Color.BLACK);
g.drawString("tab 3에 들어가는 JPanel 입니다.", 30, 50);
}
}
private JTabbedPane createTabbedPane() {
JTabbedPane pane = new JTabbedPane();
pane.addTab("tab1", new JLabel(new ImageIcon("C:\\\\Temp/lion.gif")));
pane.addTab("tab2", new JLabel(new ImageIcon("C:\\\\Temp/lion.gif")));
pane.addTab("tab3", new MyPanel());
return pane;
}
TabbedPaneEx(){
setTitle("14장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
JTabbedPane pane = createTabbedPane();
c.add(pane,BorderLayout.CENTER);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TabbedPaneEx();
}
}
'Java' 카테고리의 다른 글
명품 JAVA Programming 제 14장 실습문제 (4, 6번) (0) | 2021.08.18 |
---|---|
명품 JAVA Programming 제 14장 Open Challenge (0) | 2021.08.18 |
고급 스윙 컴포넌트(메뉴, 툴바, 툴팁, 다이얼로그) (0) | 2021.08.18 |
명품 JAVA Programming 제 13장 실습문제 (0) | 2021.08.09 |
명품 JAVA Programming 제 13장 Open Challenge (0) | 2021.08.09 |
Comments