mojo's Blog
고급 스윙 컴포넌트(메뉴, 툴바, 툴팁, 다이얼로그) 본문
간단하게 메뉴 만드는 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 MenuEx extends JFrame{
private void createMenu() {
JMenuBar mb = new JMenuBar();
JMenu screenMenu = new JMenu("Screen");
screenMenu.add(new JMenuItem("Load"));
screenMenu.add(new JMenuItem("Hide"));
screenMenu.add(new JMenuItem("ReShow"));
screenMenu.addSeparator();
screenMenu.add(new JMenuItem("Exit"));
mb.add(screenMenu);
mb.add(new JMenu("Edit"));
mb.add(new JMenu("Source"));
mb.add(new JMenu("Project"));
mb.add(new JMenu("Run"));
setJMenuBar(mb);
}
MenuEx(){
setTitle("14장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMenu();
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MenuEx();
}
}
메뉴아이템 선택하면 Action 하도록 하는 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 MenuEx extends JFrame{
private JLabel imgLabel = new JLabel();
class MenuActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
switch(cmd) {
case "Load":
if(imgLabel.getIcon()!=null) {
return;
}
imgLabel.setIcon(new ImageIcon("C:\\\\Temp/lion.gif"));
break;
case "Hide":
break;
case "ReShow":
imgLabel.setVisible(true);
break;
case "Exit":
System.exit(0);
}
}
}
private void createMenu() {
JMenuBar mb = new JMenuBar();
JMenuItem []menuItem=new JMenuItem[4];
String []itemTitle= {"Load","Hide","ReShow","Exit"};
JMenu screenMenu = new JMenu("Screen");
MenuActionListener listener = new MenuActionListener();
for(int i=0; i<menuItem.length; i++) {
menuItem[i]=new JMenuItem(itemTitle[i]);
menuItem[i].addActionListener(listener);
screenMenu.add(menuItem[i]);
}
mb.add(screenMenu);
setJMenuBar(mb);
}
MenuEx(){
setTitle("14장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMenu();
getContentPane().add(imgLabel, BorderLayout.CENTER);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MenuEx();
}
}
툴바 만드는 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 ToolBarEx extends JFrame{
private Container contentPane;
private void createToolBar() {
JToolBar toolBar = new JToolBar("Kitae Menu");
toolBar.setBackground(Color.LIGHT_GRAY);
toolBar.add(new JButton("New"));
toolBar.add(new JButton(new ImageIcon("C:\\\\Temp/lion.gif")));
toolBar.addSeparator();
toolBar.add(new JLabel("Search"));
toolBar.add(new JTextField("text field"));
JComboBox<String> combo = new JComboBox<String>();
combo.addItem("Java");
combo.addItem("C#");
combo.addItem("C");
combo.addItem("Python");
toolBar.add(combo);
contentPane.add(toolBar, BorderLayout.NORTH);
}
ToolBarEx(){
setTitle("14장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = getContentPane();
createToolBar();
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ToolBarEx();
}
}
툴팁 달기 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 ToolTipEx extends JFrame{
private Container contentPane;
private void createToolBar() {
JToolBar bar = new JToolBar("Kitae Menu");
bar.setBackground(Color.LIGHT_GRAY);
JButton newBtn = new JButton("New");
newBtn.setToolTipText("파일을 생성합니다.");
bar.add(newBtn);
JButton openBtn = new JButton(new ImageIcon("C:\\\\Temp/lion.gif"));
openBtn.setToolTipText("이건 라이언이다.");
bar.add(openBtn);
bar.addSeparator();
JTextField tf = new JTextField("text field");
tf.setToolTipText("찾고자 하는 문자열을 입력하세요.");
bar.add(tf);
contentPane.add(bar,BorderLayout.NORTH);
}
ToolTipEx(){
setTitle("14장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = getContentPane();
createToolBar();
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ToolTipEx();
}
}
툴팁 시간 제어하기는 다음 코드를 이용하면 된다.
ToolTipManager m = ToolTipManager.sharedInstance();
m.setInitialDelay(0);
m.setDismissDelay(3000);
JDialog 를 상속받아서 다이얼로그를 만드는 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.*;
class MyDialog extends JDialog{
private JTextField tf = new JTextField(10);
private JButton okButton = new JButton("OK");
MyDialog(JFrame frame, String title){
super(frame, title);
setLayout(new FlowLayout());
add(tf);
add(okButton);
setSize(200, 100);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
}
}
public class DialogEx extends JFrame{
private MyDialog dialog;
DialogEx(){
setTitle("14장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btn = new JButton("Show Dialog");
dialog = new MyDialog(this, "Test Dialog");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);
}
});
getContentPane().add(btn);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new DialogEx();
}
}
다이얼로그의 타입은 모달(Modal)과 모달리스(Modeless)두 가지가 있다.
Modal Type => 다이얼로그가 일단 출력되면 다이얼로그를 닫기 전에는 다른 작업을 전혀 할 수 없다.
즉, 사용자 입력을 독점하는 타입을 말함
Modeless => 다른 창과 모달리스 다이얼로그가 각자 독립적으로 실행이 가능하다.
모달 다이얼로그로부터 사용자의 입력 값을 알아내는 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.*;
class MyDialog extends JDialog{
private JTextField tf = new JTextField(10);
private JButton okButton = new JButton("OK");
MyDialog(JFrame frame, String title){
super(frame, title, true); // boolean 추가로 Modal(true), Modeless(false)를 결정한다.
setLayout(new FlowLayout());
add(tf);
add(okButton);
setSize(200, 100);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
}
public String getInput() {
return tf.getText();
}
}
public class DialogEx extends JFrame{
private MyDialog dialog;
DialogEx(){
setTitle("14장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btn = new JButton("Show Dialog");
dialog = new MyDialog(this, "Test Dialog");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);
String text = dialog.getInput();
btn.setText(text);
}
});
getContentPane().add(btn);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new DialogEx();
}
}
Modal Type 인 경우 => 입력하고 OK 버튼을 클릭하면 해당 Dialog가 종료되면서 받아온 String 으로 버튼의 text가 변경된다.
Modeless Type 인 경우 => 독립적으로 실행되므로 바로 Dialog의 TextField의 문자열을 가져와서 바로 적용된다.
'Java' 카테고리의 다른 글
명품 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 |
wait(), notify(), notifyAll() 을 이용하여 스레드를 동기화하기 (0) | 2021.08.09 |
Comments