java实例化代码------------------监听事件
2016-03-16 10:38
567 查看
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
{
public static void main(String args[])
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("Button Test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 800;
public static final int DEFAULT_HEIGHT = 500;
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
//定义button
JButton yellowButton = new JButton("yellow");
JButton blueButton = new JButton("blue");
JButton redButton = new JButton("red");
//添加button到面板
add(yellowButton);
add(blueButton);
add(redButton);
//创建按钮动作
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
//关联按钮与动作
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
{
public static void main(String args[])
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("Button Test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 800;
public static final int DEFAULT_HEIGHT = 500;
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
//定义button
JButton yellowButton = new JButton("yellow");
JButton blueButton = new JButton("blue");
JButton redButton = new JButton("red");
//添加button到面板
add(yellowButton);
add(blueButton);
add(redButton);
//创建按钮动作
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
//关联按钮与动作
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
相关文章推荐
- Java学习笔记--堆和栈
- java中在创建对象时候的初始化顺序
- JavaWeb之Servlet编程实例二
- 关于hibernate中@Transient注解使用的一点心得
- 通过 Spring AOP 验证方法的参数是否合法
- Log4j实现对Java日志的配置全攻略
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行
- java多线程-使用notify和wait完成多线程的顺序执行