📄 colormenuex.java
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class ColorMenuEx extends JFrame implements ActionListener {
public ColorMenuEx() {
menuBar = new JMenuBar();
colorMenu = new JMenu( "Colors" );
controlMenu = new JMenu( "Control" );
init();
setSize( 300, 150 );
getContentPane().setBackground( orig );
setVisible( true );
}
public void actionPerformed( ActionEvent e ) {
String s = e.getActionCommand();
if ( s.equals( "Exit" ) )
System.exit( 0 );
else if ( s.equals( "Restore" ) )
getContentPane().setBackground( orig );
else
setColor( s );
}
private void init() {
initMenu( colorMenu, colorNames );
initMenu( controlMenu, controls );
menuBar.add( colorMenu ); menuBar.add( controlMenu );
setJMenuBar( menuBar );
}
private void initMenu( JMenu menu, String[ ] items ) {
for ( int i = 0; i < items.length; i++ ) {
JMenuItem item = new JMenuItem( items[ i ] );
item.addActionListener( this );
menu.add( item );
}
}
private void setColor( String c ) {
for ( int i = 0; i < colorNames.length; i++ )
if ( c.equals( colorNames[ i ] ) ) {
getContentPane().setBackground( colors[ i ] );
break;
}
}
private JMenuBar menuBar;
private JMenu colorMenu;
private JMenu controlMenu;
private static final Color orig = Color.white;
private static final String[ ] colorNames =
{ "Blue", "Cyan", "Gray", "Orange", "Pink" };
private static final Color[ ] colors =
{ Color.blue, Color.cyan, Color.gray, Color.orange, Color.pink };
private static final String[ ] controls = { "Restore", "Exit" };
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -