📄 colormenuex3.java
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class ColorMenuEx3 extends JFrame implements ActionListener {
public ColorMenuEx3() {
menuBar = new JMenuBar();
colorMenu = new JMenu( "Colors" );
controls = new JToolBar();
init();
setSize( 300, 150 );
getContentPane().setBackground( orig );
setVisible( true );
}
public void actionPerformed( ActionEvent e ) {
String s = e.getActionCommand();
if ( s.equals( exitString ) )
System.exit( 0 );
else if ( s.equals( restoreString ) )
getContentPane().setBackground( orig );
else
setColor( s );
}
private void init() {
initMenu( colorMenu, colorNames );
menuBar.add( colorMenu );
setJMenuBar( menuBar );
addButton( exitString ); addButton( restoreString );
getContentPane().add( controls, BorderLayout.SOUTH );
}
private void addButton( String n ) {
JButton j = new JButton( n );
controls.add( j );
j.addActionListener( this );
}
private void initMenu( JMenu menu, String[ ] items ) {
for ( int i = 0; i < items.length; i++ ) {
JMenuItem item = new JMenuItem( items[ i ],
items[ i ].charAt( 0 ) );
item.addActionListener( this );
menu.add( item );
}
}
private void setColor( String c ) {
int i = 0;
while ( i < colorNames.length )
if ( c.equals( colorNames[ i ] ) ) {
getContentPane().setBackground( colors[ i ] );
break;
}
else
i++;
}
private JMenuBar menuBar;
private JToolBar controls;
private JMenu colorMenu;
private JButton exit;
private JButton restore;
private static final String exitString = " Exit ";
private static final String restoreString = " Restore ";
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 };
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -