±Û¾´ÀÌ :WINOOP 2000³â 5¿ù 15ÀÏ 12:32:36
ÀÌ ÄÚµå´Â JMenuÀ» ¿¬½ÀÇϱâ À§ÇÑ ÄÚµåÀε¥ óÀ½ ¸Þ´º°¡
¶ã¶§ Sample Text¶ó´Â ±ÛÀ» ¶ç¿ö³õ°í ¸Þ´º¿¡¼ Ç׸ñÀ» ¼±ÅÃÇßÀ»¶§ ¿©±â¿¡ ¸Â°Ô ³»¿ëÀÌ °»½ÅµÇ´Â °ÍÀÔ´Ï´Ù.
±×·±µ¥ JRadioButtonMenuItemÀÇ Ç׸ñÀ» ¼±ÅÃÇßÀ»¶§¿¡´Â
³»¿ëÀÌ Á¦´ë·Î °»½ÅµÇ´Âµ¥ JCheckBoxMenItemÀÇ Ç׸ñµéÀº
À̺¥Æ®°¡ Á¦´ë·Î ¸ÔÈ÷Áö ¾Ê°í ÀÖ½À´Ï´Ù.
¾Æ¹«·¡µµ repaint()¿¡¼ ¹®Á¦°¡ ¹ß»ýÇÑ°Í °°Àºµ¥ Á¤È®ÇÑ ÀÌÀ¯¸¦ ¸ð¸£°Ú½À´Ï´Ù. ¾Æ½Ã´Â ºÐµéÀº Á¦¹ß µµ¿ÍÁÖ¼¼¿ä..import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JMenuTest extends JFrame
{
private Color colorValues[] =
{Color.black, Color.blue, Color.red, Color.green };
private JRadioButtonMenuItem colorItems[],fonts[];
private JCheckBoxMenuItem styleItems[];
private JLabel display; //º¸¿©ÁÖ´Â ¹®ÀÚ¸¦ ÀǹÌÇÑ´Ù.
private ButtonGroup fontGroup,colorGroup;
private int style;
public JMenuTest()
{
super("Using Swing Menu");
JMenuBar bar = new JMenuBar(); //create menubar
setJMenuBar(bar); //set the menubar for the JFrame
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F'); //´ÜÅöÅ°¸¦ ¼³Á¤ÇÏ°Ú´Ù.
JMenuItem aboutItem = new JMenuItem("About...");
aboutItem.setMnemonic('A');
//ÀÌÁ¨ À̺¥Æ®¸¦ µî·Ï ½ÃÄÑ º¼±î³ª.
aboutItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent a_e)
{
JOptionPane.showMessageDialog(JMenuTest.this,
"This is an example \nof using menus",
"About",JOptionPane.PLAIN_MESSAGE);
}
} //end A
);
fileMenu.add(aboutItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('x');
exitItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent a_e)
{
System.exit(0);
}
}
);
fileMenu.add(exitItem);
bar.add(fileMenu); //add file menu
JMenu formatMenu = new JMenu("Format");
formatMenu.setMnemonic('r');
// Create Color submenu
String colors[] =
{"Black","Blue","Red","Green"};
JMenu colorMenu = new JMenu("Color");
colorMenu.setMnemonic('c');
colorItems = new JRadioButtonMenuItem[colors.length];
colorGroup = new ButtonGroup();
ItemHandler itemHandler = new ItemHandler();
for ( int i=0; i<colors.length; i++)
{
colorItems[i] = new JRadioButtonMenuItem(colors[i]);
colorMenu.add(colorItems[i]);
colorGroup.add(colorItems[i]); //ÀÌ ¹öÆ°µéÀ» °°Àº ±×·ì³»¿¡ Æ÷ÇÔ½ÃŲ´Ù.
colorItems[i].addActionListener(itemHandler);
}
colorItems[0].setSelected(true);
formatMenu.add(colorMenu);
formatMenu.addSeparator();
//create Font submenu
String fontNames[] =
{"TimesRoman","Courier","Helvetica"};
JMenu fontMenu = new JMenu("Font");
fontMenu.setMnemonic('n');
fonts = new JRadioButtonMenuItem[fontNames.length];
fontGroup = new ButtonGroup();
for(int i=0;i<fonts.length;i++)
{
fonts[i]= new JRadioButtonMenuItem(fontNames[i]);
fontMenu.add(fonts[i]);
fontGroup.add(fonts[i]);
fonts[i].addActionListener(itemHandler);
}
fonts[0].setSelected(true);
fontMenu.addSeparator();
String styleNames[]={"Bold","Italic"};
styleItems = new JCheckBoxMenuItem[styleNames.length];
StyleHandler styleHandler = new StyleHandler();
for(int i=0;i<styleNames.length;i++)
{
styleItems[i] = new JCheckBoxMenuItem(styleNames[i]);
fontMenu.add(styleItems[i]);
styleItems[i].addItemListener(styleHandler); //checkbox¿¡ ´ëÇÑ À̺¥Æ®¸¦ ´Þ¾ÆÁØ´Ù.
}
formatMenu.add(fontMenu);
bar.add(formatMenu);
display = new JLabel(
"Sample Text",SwingConstants.CENTER);
display.setForeground(colorValues[0]); //default·Î °ËÁ¤»öÀ» ¼±ÅÃÇÏ°Ú´Ù.
display.setFont(
new Font("TimesRoman",Font.PLAIN,72)); //Default·Î TimesRomanÀ» ¼±ÅÃÇÏ°Ú´Ù.
getContentPane().setBackground(Color.cyan);
getContentPane().add(display,BorderLayout.CENTER);
setSize(500,200);
show();
} //end of Constructor
public static void main(String args[])
{
JMenuTest my_app = new JMenuTest();
my_app.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent w_e)
{
System.exit(0);
}
}
);
} //end of main
//inner classµéÀ» ¼±¾ðÇÏ°Ú´Ù.
//¼±ÅõǾîÁø »ö»ó°ú Æ÷¸ËÀ» Àû¿ë½ÃÅ°°Ú´Ù.
class ItemHandler implements ActionListener{
public void actionPerformed(ActionEvent w_e)
{
for(int i=0;i<colorValues.length;i++)
if(colorItems[i].isSelected())
{
display.setForeground(colorValues[i]);
break;
}
for (int i = 0;i<fonts.length;i++)
if(w_e.getSource() == fonts[i]) {
display.setFont(new Font(
fonts[i].getText(),style,72));
break;
}
repaint();
}
}
public void showmenu()
{
removeAll();
repaint();
}
//CheckBox¿¡ ´ëÇØ À̺¥Æ®¸¦ Àû¿ë½ÃÅ°°Ú´Ù.
//code debugging..
class StyleHandler implements ItemListener{
public void itemStateChanged(ItemEvent w_e)
{
style = 0;
if(styleItems[0].isSelected()==true)
style += Font.BOLD;
if(styleItems[1].isSelected()==true)
style += Font.ITALIC;
display.setFont(new Font(
display.getFont().getName(),style,72));
JOptionPane.showMessageDialog(
null,"Style Code : " + style);
repaint();
// showmenu();
}
}
} //end of class