Java处理按键事件
2014-12-12 15:10
253 查看
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonFrameTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ButtonFrame bf=new ButtonFrame();
bf.setTitle("ButtonFrame");
bf.setDefaultCloseOperation(ButtonFrame.EXIT_ON_CLOSE);
bf.setVisible(true);
}
});
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonFrame extends JFrame{
private JPanel buttonPanel;
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULT_HEIGHT=200;
public ButtonFrame()
{
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
JButton redButton=new JButton("Red");
buttonPanel=new JPanel();
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
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
{
private Color backgroundColor;
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
}
}
}
import java.awt.event.*;
import javax.swing.*;
public class ButtonFrameTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ButtonFrame bf=new ButtonFrame();
bf.setTitle("ButtonFrame");
bf.setDefaultCloseOperation(ButtonFrame.EXIT_ON_CLOSE);
bf.setVisible(true);
}
});
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonFrame extends JFrame{
private JPanel buttonPanel;
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULT_HEIGHT=200;
public ButtonFrame()
{
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
JButton redButton=new JButton("Red");
buttonPanel=new JPanel();
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
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
{
private Color backgroundColor;
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
}
}
}
相关文章推荐
- Java Swing事件处理——键盘事件及监听处理 KeyListener 按键测试
- Java事件处理模式
- java小程序中鼠标事件的响应处理(包含右键、双击等)
- 读Java事件处理机制
- Java事件处理与发送
- 为您的Java应用程序添加退出事件处理
- Java Swing中的键盘事件处理
- [转]利用Java事件处理机制实现录制、回放功能
- 为您的Java应用程序添加退出事件处理
- Java 事件处理实例
- Java事件处理与发送
- 浅谈Java与C#的事件处理机制
- Java事件处理学习笔记
- Java Swing中的键盘事件处理
- java的事件处理
- Java事件处理与发送原理
- DHTML中的事件处理- IE和Netscape分析比较 - 转 "java与模式",文章不错
- 让非模态对话框处理TAB按键事件的三种方法
- 使用handleEvent()方法,action()方法和mouseDown()方法处理事件的JAVA Application程序.
- java的事件处理机制