📄 jmenudemo.java
字号:
package components;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
public class JMenuDemo extends JFrame {
private static final long serialVersionUID = 1L;
private static int count = 0;
public JMenuDemo() {
final JLabel label = new JLabel("Message");
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("file");
fileMenu.add("Open");
JMenuItem editMenuItem = new JMenuItem("Edit");
editMenuItem.setMnemonic('a');
editMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK, true));
editMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("OK" + count++);
}
});
fileMenu.add(editMenuItem);
fileMenu.addSeparator();
// fileMenu.add(label);
menuBar.add(fileMenu);
JMenu colorMenu = new JMenu("Color");
final JRadioButtonMenuItem red = new JRadioButtonMenuItem("red");
red.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == red) {
label.setBackground(Color.red);
}
}
});
colorMenu.add(red);
menuBar.add(colorMenu);
menuBar.add(label);
fileMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setBackground(Color.red);
}
});
setJMenuBar(menuBar);
Container container = getContentPane();
container.setLayout(new FlowLayout());
// container.add(label);
container.setBackground(Color.RED);
setSize(400,400);
setVisible(true);
}
public static void main(String[] args) {
JMenuDemo application = new JMenuDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -