more..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;
public class IntroExample extends JMenuBar {
String[ ] fileItems = new String[ ] { "New", "Open", "Save", "Exit" };
String[ ] editItems = new String[ ] { "Undo", "Cut", "Copy", "Paste" };
char[ ] fileShortcuts = { 'N','O','S','X' };
char[ ] editShortcuts = { 'Z','X','C','V' };
String[ ] editItems = new String[ ] { "Undo", "Cut", "Copy", "Paste" };
char[ ] fileShortcuts = { 'N','O','S','X' };
char[ ] editShortcuts = { 'Z','X','C','V' };
public IntroExample( ) {
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu otherMenu = new JMenu("Other");
JMenu subMenu = new JMenu("SubMenu");
JMenu subMenu2 = new JMenu("SubMenu2");
JMenu editMenu = new JMenu("Edit");
JMenu otherMenu = new JMenu("Other");
JMenu subMenu = new JMenu("SubMenu");
JMenu subMenu2 = new JMenu("SubMenu2");
// 밑줄 단축키가 있는 파일 메뉴 생성
ActionListener printListener = new ActionListener( ) {
public void actionPerformed(ActionEvent event) {
System.out.println("Menu item [" + event.getActionCommand( ) +
"] was pressed.");
}
};
for (int i=0; i < fileItems.length; i++) {
JMenuItem item = new JMenuItem(fileItems[i], fileShortcuts[i]);
item.addActionListener(printListener);
fileMenu.add(item);
}
ActionListener printListener = new ActionListener( ) {
public void actionPerformed(ActionEvent event) {
System.out.println("Menu item [" + event.getActionCommand( ) +
"] was pressed.");
}
};
for (int i=0; i < fileItems.length; i++) {
JMenuItem item = new JMenuItem(fileItems[i], fileShortcuts[i]);
item.addActionListener(printListener);
fileMenu.add(item);
}
// 일반 단축키가 있는 파일 메뉴 생성
for (int i=0; i < editItems.length; i++) {
JMenuItem item = new JMenuItem(editItems[i]);
item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],
Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));
item.addActionListener(printListener);
editMenu.add(item);
}
for (int i=0; i < editItems.length; i++) {
JMenuItem item = new JMenuItem(editItems[i]);
item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],
Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));
item.addActionListener(printListener);
editMenu.add(item);
}
// Insert a separator in the Edit menu in Position 1 after "Undo".
editMenu.insertSeparator(1);
editMenu.insertSeparator(1);
// 다른 메뉴의 서브 클래스 생성
JMenuItem item;
subMenu2.add(item = new JMenuItem("Extra 2"));
item.addActionListener(printListener);
subMenu.add(item = new JMenuItem("Extra 1"));
item.addActionListener(printListener);
subMenu.add(subMenu2);
JMenuItem item;
subMenu2.add(item = new JMenuItem("Extra 2"));
item.addActionListener(printListener);
subMenu.add(item = new JMenuItem("Extra 1"));
item.addActionListener(printListener);
subMenu.add(subMenu2);
// 메뉴 자신을 생성
otherMenu.add(subMenu);
otherMenu.add(item = new JCheckBoxMenuItem("Check Me"));
item.addActionListener(printListener);
otherMenu.addSeparator( );
ButtonGroup buttonGroup = new ButtonGroup( );
otherMenu.add(item = new JRadioButtonMenuItem("Radio 1"));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.add(item = new JRadioButtonMenuItem("Radio 2"));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.addSeparator( );
otherMenu.add(item = new JMenuItem("Potted Plant",
new ImageIcon("image.gif")));
item.addActionListener(printListener);
otherMenu.add(subMenu);
otherMenu.add(item = new JCheckBoxMenuItem("Check Me"));
item.addActionListener(printListener);
otherMenu.addSeparator( );
ButtonGroup buttonGroup = new ButtonGroup( );
otherMenu.add(item = new JRadioButtonMenuItem("Radio 1"));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.add(item = new JRadioButtonMenuItem("Radio 2"));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.addSeparator( );
otherMenu.add(item = new JMenuItem("Potted Plant",
new ImageIcon("image.gif")));
item.addActionListener(printListener);
// 마지막으로 메뉴바에 모든 메뉴 삽입
add(fileMenu);
add(editMenu);
add(otherMenu);
}
add(fileMenu);
add(editMenu);
add(otherMenu);
}
public static void main(String s[ ]) {
JFrame frame = new JFrame("Simple Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(new IntroExample( ));
frame.pack( );
frame.setVisible(true);
}
}
출처: http://cafe.naver.com/javalove.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=83 자바사랑
JFrame frame = new JFrame("Simple Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(new IntroExample( ));
frame.pack( );
frame.setVisible(true);
}
}
출처: http://cafe.naver.com/javalove.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=83 자바사랑
댓글 없음:
댓글 쓰기