appletdemo2.java

来自「java程序设计语言源代码」· Java 代码 · 共 60 行

JAVA
60
字号
//Example 2 of Chapter 6

import  java.awt.*;
import  java.awt.event.*;
import  javax.swing.*;

public  class  AppletDemo2  extends  JApplet  implements  ActionListener
{
	Container container;
	JButton  BackButton, ForeButton, ResetButton;
	JLabel text;
	Color color1 = Color.lightGray;
	Color color2 = Color.black;
	public  void  init()
	{
		container = getContentPane();
		container.setLayout(new  FlowLayout());
		
		text = new JLabel("show a JColorChooser Using", SwingConstants.CENTER );
		text.setFont( new Font( "Dialog", Font.BOLD, 24));
		
		BackButton = new JButton("调整背景色");
		ForeButton = new JButton("调整前景色");
		ResetButton = new JButton("重置颜色");
		
		BackButton.addActionListener(this);
		ForeButton.addActionListener(this);
		ResetButton.addActionListener(this);
		
		container.add( text );
		container.add( BackButton );
		container.add( ForeButton );
		container.add( ResetButton );
    }
    
    public void actionPerformed( ActionEvent event )
    {
    	if( event.getSource() == BackButton )
    	{
    		color1 = JColorChooser.showDialog( this, "选择颜色", color1);
    		if( color1 == null ) color1 = Color.lightGray;
    		container.setBackground( color1 );
    	}
    	
    	if( event.getSource() == ForeButton )
    	{
    		color2 = JColorChooser.showDialog( this, "选择颜色", color2);
    		if(color2 == null)color2 = Color.black;
    		text.setForeground( color2 );
    	}
    	
    	if( event.getSource() == ResetButton )
    	{
    		color1 = Color.lightGray;
    		container.setBackground( color1 );
    		color2 = Color.black;
    		text.setForeground( color2 );
    	}
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?