按钮监听难题 我有以下几个问题,请高手指点一下,万分感激!1、请问如何在按钮上实现组合键的监听?2、如何写代码才能区分是ActionEvent 事件还是KeyEvent事件,因为我想做一个按钮,当鼠标单击时做出一个响应,当用快捷键时做出另外一个响应。谢谢谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.awt.event.*;public class ButtonTest extends Frame implements KeyListener,ActionListener{private Button b1;private Button b2;public ButtonTest(int i,int j) {addKeyListener(this);setLayout(new FlowLayout(1));b1=new Button("yellow");b1.addActionListener(this);b1.addKeyListener(this);b2=new Button("blue");b2.addActionListener(this);b2.addKeyListener(this);setSize(i, j);add(b1);add(b2);pack();setVisible(true);this.setFocusable(true);}//实现ActionListener接口方法public void actionPerformed(ActionEvent a) {if (a.getActionCommand().equals("yellow")) {b1.setBackground(Color.red);b2.requestFocus();//点击button1时把事件焦点给b2} else if (a.getActionCommand().equals("blue")) {b2.setBackground(Color.BLUE);}}//实现keylistener的3个方法public void keyTyped(KeyEvent e){System.out.println("KeyTyped"+" "+e);}public void keyPressed(KeyEvent e){System.out.println("KeyPressed"+" "+e);}public void keyReleased(KeyEvent e){System.out.println("KeyReleased"+" "+e);}public static void main(String[] args) {ButtonTest my=new ButtonTest(300,300);my.setSize(200,200);}}快捷键,我不太会 确实热键很难实现,我也不太懂,好像要用windows API的函数! 这个小代码 可以通过 Alt+U 来触发JButton事件。import java.awt.Insets;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.JFrame;import javax.swing.JButton;import javax.swing.SwingUtilities;//swing 方法集合import javax.swing.UIManager;//UIManager UI管理器public class windowsLookAndFeel{ public static void main(String[] args) { Frame winGai=new Frame("windows和java的感观GUI设置"); winGai.setVisible(true); }}class Frame extends JFrame implements ActionListener{ private boolean changsfeel=true; private JButton feelButton; Frame(String name) { super(name); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(null); feelButton = new JButton("windows 感观设置,变换成java感观设置(UI)"); feelButton.setMnemonic('U');//设置快捷捷。 feelButton.setMargin(new Insets(1,1,1,1)); feelButton.setBounds(10,20,280,25); feelButton.addActionListener(this); getContentPane().add(feelButton); LookAndFeel(this); } public void actionPerformed(ActionEvent e) { LookAndFeel(this); } public void LookAndFeel(JFrame frame) { String lookAndFeelName="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";//感观类所在地址 if(changsfeel) { feelButton.setText("windows 感观设置,变换成java感观设置(UI)"); feelButton.setMnemonic('U'); lookAndFeelName="com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; changsfeel=false; } else { feelButton.setText("Java 感观设置,变换成windows感观设置(UI)"); feelButton.setMnemonic('U'); lookAndFeelName="javax.swing.plaf.metal.MetalLookAndFeel"; changsfeel=true; } try { UIManager.setLookAndFeel(lookAndFeelName);//通过UIManager管理器来设置,GUI 的UI界面 SwingUtilities.updateComponentTreeUI(frame);//通过SingUtilities来frame容器UI外观。 } catch(Exception e) { } }} 用KeyStore实现 ActionMap 查查不少现成的代码//我测试使用的代码button.getActionMap().put("Hello", new BtAction());button.getInputMap(button.WHEN_IN_FOCUSED_WINDOW).put( KeyStroke.getKeyStroke("F1"), "Hello"); import java.awt.Container;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.AbstractAction;import javax.swing.ActionMap;import javax.swing.InputMap;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.KeyStroke;public class KeyEventTest extends JFrame { public KeyEventTest() { super(); JButton button = new JButton("button"); button.setPreferredSize(new Dimension(50, 20)); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(new JButton("11111")); c.add(button); AbstractAction keyAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { System.out.println("Motivated by key"); } }; InputMap inputMap = button .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); // 这里设置你绑定的组合键 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, KeyEvent.CTRL_DOWN_MASK), "aaaaa"); ActionMap actionMap = button.getActionMap(); actionMap.put("aaaaa", keyAction); button.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent arg0) { System.out.println("Motivated by mouse"); } }); setSize(200, 200); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new KeyEventTest(); }}这段代码,实现了一个按钮鼠标点击和快捷键的分开处理。我绑定的是CTRL+SPACE。如果你想更改,在我注释的地方更改你想要的快捷键。 在Java中使用C语言中的goto语句 内部类怎么调用外部同名方法呢? 昨天讨论起JAVA,怎么觉得JAVA除了JSP,另外没有什么任何优势了. 关于JTAPI的问题,请求高手指点迷津 jdbTable和queryDataSet显示某表,如何操作数据? 谁有关于java入门的学习资料?? 关于写配置文件 静态变量为什么自加后一直不变的呢 请教JDBC访问Ms SQL Server的问题。 使用jbuilder的困惑,在线等待 如何调用paintComponent()方法,生成圆点 请问一个JAVA插入数据库
import java.awt.event.*;public class ButtonTest extends Frame implements KeyListener,ActionListener{
private Button b1;
private Button b2;public ButtonTest(int i,int j) {
addKeyListener(this);
setLayout(new FlowLayout(1));
b1=new Button("yellow");
b1.addActionListener(this);
b1.addKeyListener(this);
b2=new Button("blue");
b2.addActionListener(this);
b2.addKeyListener(this);
setSize(i, j);
add(b1);add(b2);
pack();
setVisible(true);
this.setFocusable(true);
}
//实现ActionListener接口方法
public void actionPerformed(ActionEvent a) {
if (a.getActionCommand().equals("yellow")) {
b1.setBackground(Color.red);
b2.requestFocus();//点击button1时把事件焦点给b2
} else if (a.getActionCommand().equals("blue")) {
b2.setBackground(Color.BLUE);
}
}
//实现keylistener的3个方法
public void keyTyped(KeyEvent e){
System.out.println("KeyTyped"+" "+e);
}
public void keyPressed(KeyEvent e){
System.out.println("KeyPressed"+" "+e);
}
public void keyReleased(KeyEvent e){
System.out.println("KeyReleased"+" "+e);
}public static void main(String[] args) {
ButtonTest my=new ButtonTest(300,300);
my.setSize(200,200);
}}
快捷键,我不太会
这个小代码 可以通过 Alt+U 来触发JButton事件。
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;//swing 方法集合
import javax.swing.UIManager;//UIManager UI管理器public class windowsLookAndFeel
{
public static void main(String[] args)
{
Frame winGai=new Frame("windows和java的感观GUI设置");
winGai.setVisible(true);
}
}class Frame extends JFrame implements ActionListener
{
private boolean changsfeel=true;
private JButton feelButton;
Frame(String name)
{
super(name);
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null); feelButton = new JButton("windows 感观设置,变换成java感观设置(UI)");
feelButton.setMnemonic('U');//设置快捷捷。
feelButton.setMargin(new Insets(1,1,1,1));
feelButton.setBounds(10,20,280,25);
feelButton.addActionListener(this);
getContentPane().add(feelButton);
LookAndFeel(this);
}
public void actionPerformed(ActionEvent e)
{
LookAndFeel(this);
}
public void LookAndFeel(JFrame frame)
{
String lookAndFeelName="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";//感观类所在地址
if(changsfeel)
{
feelButton.setText("windows 感观设置,变换成java感观设置(UI)");
feelButton.setMnemonic('U');
lookAndFeelName="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
changsfeel=false;
}
else
{
feelButton.setText("Java 感观设置,变换成windows感观设置(UI)");
feelButton.setMnemonic('U');
lookAndFeelName="javax.swing.plaf.metal.MetalLookAndFeel";
changsfeel=true;
}
try
{
UIManager.setLookAndFeel(lookAndFeelName);//通过UIManager管理器来设置,GUI 的UI界面
SwingUtilities.updateComponentTreeUI(frame);//通过SingUtilities来frame容器UI外观。
}
catch(Exception e)
{
}
}
}
button.getActionMap().put("Hello", new BtAction());
button.getInputMap(button.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("F1"), "Hello");
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;public class KeyEventTest extends JFrame {
public KeyEventTest() {
super(); JButton button = new JButton("button");
button.setPreferredSize(new Dimension(50, 20));
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(new JButton("11111"));
c.add(button);
AbstractAction keyAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) { System.out.println("Motivated by key");
}
}; InputMap inputMap = button
.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); // 这里设置你绑定的组合键
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,
KeyEvent.CTRL_DOWN_MASK), "aaaaa");
ActionMap actionMap = button.getActionMap();
actionMap.put("aaaaa", keyAction); button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
System.out.println("Motivated by mouse");
}
}); setSize(200, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
} public static void main(String args[]) {
new KeyEventTest();
}
}这段代码,实现了一个按钮鼠标点击和快捷键的分开处理。我绑定的是CTRL+SPACE。
如果你想更改,在我注释的地方更改你想要的快捷键。