📄 showcolors2.java
字号:
// Fig. 12.6: ShowColors2.java
// Choosing colors with JColorChooser.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowColors2 extends JFrame {
private JButton changeColorButton;
private Color color = Color.LIGHT_GRAY;
private Container container;
// set up GUI
public ShowColors2()
{
super( "Using JColorChooser" );
container = getContentPane();
container.setLayout( new FlowLayout() );
// set up changeColorButton and register its event handler
changeColorButton = new JButton( "Change Color" );
changeColorButton.addActionListener(
new ActionListener() { // anonymous inner class
// display JColorChooser when user clicks button
public void actionPerformed( ActionEvent event )
{
color = JColorChooser.showDialog(
ShowColors2.this, "Choose a color", color );
// set default color, if no color is returned
if ( color == null )
color = Color.LIGHT_GRAY;
// change content pane's background color
container.setBackground( color );
}
} // end anonymous inner class
); // end call to addActionListener
container.add( changeColorButton );
setSize( 400, 130 );
setVisible( true );
} // end ShowColor2 constructor
// execute application
public static void main( String args[] )
{
ShowColors2 application = new ShowColors2();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class ShowColors2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -