📄 guidemo4.java
字号:
//Example 4 of Chapter 5
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUIDemo4
{
public static void main(String args[])
{
JFrame f = new JFrame("颜色盘");
JPanel contentpane = new JPanel();
contentpane.setLayout( new GridLayout( 4 , 3 ));
f.setContentPane(contentpane);
JButton bblue = new JButton("blue");
JButton bcyan = new JButton("cyan");
JButton bgray = new JButton("gray");
JButton bgreen = new JButton("green");
JButton bmagenta = new JButton("magenta");
JButton borange = new JButton("orange");
JButton bpink = new JButton("pink");
JButton bred = new JButton("red");
JButton bwhite = new JButton("white");
JButton byellow = new JButton("yellow");
JButton bdarkGray = new JButton("darkGray");
JButton blightGray = new JButton("lightGray");
bblue.addActionListener(new ActionHandler(bblue));
bcyan.addActionListener(new ActionHandler(bcyan));
bgray.addActionListener(new ActionHandler(bgray));
bgreen.addActionListener(new ActionHandler(bgreen));
bmagenta.addActionListener(new ActionHandler(bmagenta));
borange.addActionListener(new ActionHandler(borange));
bpink.addActionListener(new ActionHandler(bpink));
bred.addActionListener(new ActionHandler(bred));
bwhite.addActionListener(new ActionHandler(bwhite));
byellow.addActionListener(new ActionHandler(byellow));
bdarkGray.addActionListener(new ActionHandler(bdarkGray));
blightGray.addActionListener(new ActionHandler(blightGray));
contentpane.add(bblue);
contentpane.add(bcyan);
contentpane.add(bgray);
contentpane.add(bgreen);
contentpane.add(bmagenta);
contentpane.add(borange);
contentpane.add(bpink);
contentpane.add(bred);
contentpane.add(bwhite);
contentpane.add(byellow);
contentpane.add(bdarkGray);
contentpane.add(blightGray);
f.setSize(360 , 160);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class ActionHandler implements ActionListener
{
JButton bb;
public ActionHandler(JButton b)
{
bb=b;
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("blue"))
bb.setBackground(Color.blue);
if(e.getActionCommand().equals("cyan"))
bb.setBackground(Color.cyan);
if(e.getActionCommand().equals("gray"))
bb.setBackground(Color.gray);
if(e.getActionCommand().equals("green"))
bb.setBackground(Color.green);
if(e.getActionCommand().equals("magenta"))
bb.setBackground(Color.magenta);
if(e.getActionCommand().equals("orange"))
bb.setBackground(Color.orange);
if(e.getActionCommand().equals("pink"))
bb.setBackground(Color.pink);
if(e.getActionCommand().equals("red"))
bb.setBackground(Color.red);
if(e.getActionCommand().equals("white"))
bb.setBackground(Color.white);
if(e.getActionCommand().equals("yellow"))
bb.setBackground(Color.yellow);
if(e.getActionCommand().equals("darkGray"))
bb.setBackground(Color.darkGray);
if(e.getActionCommand().equals("lightGray"))
bb.setBackground(Color.lightGray);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -