当前位置: 技术问答>java相关
to lianyunzxp(小刀)和诸位大虾:如何设置一个按钮的快捷键为回车键“Enter”?
来源: 互联网 发布时间:2015-06-15
本文导语: to lianyunzxp(小刀) 在我昨天的一个同主题帖子里面,你说的我看不大懂…… 能否用实例帮我演示一下?就是修改下面代码:将DialogMain class里面的jButton1设置快捷键为回车键“ENTER”,jButton2设置快捷键为“ESC”键。...
to lianyunzxp(小刀)
在我昨天的一个同主题帖子里面,你说的我看不大懂……
能否用实例帮我演示一下?就是修改下面代码:将DialogMain class里面的jButton1设置快捷键为回车键“ENTER”,jButton2设置快捷键为“ESC”键。注意,快捷键就是一个键,不是“Alt+Enter”等
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {
DialogMain dm = new DialogMain();
dm.show();
}
}
class DialogMain extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
public DialogMain() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
this.setSize(500,400);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(152, 134, -1, -1));
jPanel1.add(jButton2, new XYConstraints(229, 66, -1, -1));
jPanel1.add(jTextField1, new XYConstraints(81, 74, 117, -1));
}
void jButton1_actionPerformed(ActionEvent e) {
Dialog2 dl = new Dialog2(this, true);
dl.show();
}
void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText("this is a test");
}
}
class Dialog2 extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
public Dialog2(Dialog owner, boolean modal) {
super(owner, modal);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(500,400);
jPanel1.setLayout(xYLayout1);
jButton1.setText("faint");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(161, 116, -1, -1));
}
}
在我昨天的一个同主题帖子里面,你说的我看不大懂……
能否用实例帮我演示一下?就是修改下面代码:将DialogMain class里面的jButton1设置快捷键为回车键“ENTER”,jButton2设置快捷键为“ESC”键。注意,快捷键就是一个键,不是“Alt+Enter”等
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {
DialogMain dm = new DialogMain();
dm.show();
}
}
class DialogMain extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
public DialogMain() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
this.setSize(500,400);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(152, 134, -1, -1));
jPanel1.add(jButton2, new XYConstraints(229, 66, -1, -1));
jPanel1.add(jTextField1, new XYConstraints(81, 74, 117, -1));
}
void jButton1_actionPerformed(ActionEvent e) {
Dialog2 dl = new Dialog2(this, true);
dl.show();
}
void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText("this is a test");
}
}
class Dialog2 extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
public Dialog2(Dialog owner, boolean modal) {
super(owner, modal);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(500,400);
jPanel1.setLayout(xYLayout1);
jButton1.setText("faint");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(161, 116, -1, -1));
}
}
|
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {
DialogMain dm = new DialogMain();
dm.show();
}
}
class DialogMain extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
public DialogMain() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
this.setSize(500,400);
jButton1.setText("jButton1");
jButton2.setText("jButton2");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(152, 134, -1, -1));
jPanel1.add(jButton2, new XYConstraints(229, 66, -1, -1));
jPanel1.add(jTextField1, new XYConstraints(81, 74, 117, -1));
SymListener symListener = new SymListener();
jButton1.addActionListener(symListener);
jButton2.addActionListener(symListener);
jButton1.registerKeyboardAction(symListener,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
jButton2.registerKeyboardAction(symListener,
KeyStroke.getKeyStroke(KeyEvent.VK_E, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
}
class SymListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == jButton1) {
jButton1_actionPerformed(e);
} else if (obj == jButton2) {
jButton2_actionPerformed(e);
}
}
void jButton1_actionPerformed(ActionEvent e) {
Dialog2 dl = new Dialog2(this, true);
dl.show();
}
void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText("this is a test");
}
}
class Dialog2 extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
public Dialog2(Dialog owner, boolean modal) {
super(owner, modal);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(500,400);
jPanel1.setLayout(xYLayout1);
jButton1.setText("faint");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(161, 116, -1, -1));
}
}
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {
DialogMain dm = new DialogMain();
dm.show();
}
}
class DialogMain extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
public DialogMain() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
this.setSize(500,400);
jButton1.setText("jButton1");
jButton2.setText("jButton2");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(152, 134, -1, -1));
jPanel1.add(jButton2, new XYConstraints(229, 66, -1, -1));
jPanel1.add(jTextField1, new XYConstraints(81, 74, 117, -1));
SymListener symListener = new SymListener();
jButton1.addActionListener(symListener);
jButton2.addActionListener(symListener);
jButton1.registerKeyboardAction(symListener,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
jButton2.registerKeyboardAction(symListener,
KeyStroke.getKeyStroke(KeyEvent.VK_E, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
}
class SymListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == jButton1) {
jButton1_actionPerformed(e);
} else if (obj == jButton2) {
jButton2_actionPerformed(e);
}
}
void jButton1_actionPerformed(ActionEvent e) {
Dialog2 dl = new Dialog2(this, true);
dl.show();
}
void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText("this is a test");
}
}
class Dialog2 extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
public Dialog2(Dialog owner, boolean modal) {
super(owner, modal);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(500,400);
jPanel1.setLayout(xYLayout1);
jButton1.setText("faint");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(161, 116, -1, -1));
}
}
|
这样应该没问题了吧
呵呵,试试吧
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class KeyTest extends JFrame {
public KeyTest() {
this.setSize(300,200);
JButton btn = new JButton();
btn.setText("Test");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("CLICK");
}
});
KeyStroke stroke1 = KeyStroke.getKeyStroke('f');
btn.registerKeyboardAction(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("OK");
}
},stroke1,JComponent.WHEN_FOCUSED);
this.getContentPane().add(btn);
}
public static void main(String[] args) {
KeyTest keyTest1 = new KeyTest();
keyTest1.show();
}
}
呵呵,试试吧
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class KeyTest extends JFrame {
public KeyTest() {
this.setSize(300,200);
JButton btn = new JButton();
btn.setText("Test");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("CLICK");
}
});
KeyStroke stroke1 = KeyStroke.getKeyStroke('f');
btn.registerKeyboardAction(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("OK");
}
},stroke1,JComponent.WHEN_FOCUSED);
this.getContentPane().add(btn);
}
public static void main(String[] args) {
KeyTest keyTest1 = new KeyTest();
keyTest1.show();
}
}
|
你运行一下这个程序。可能会有点启发。如果你有《java2核心技术卷I基础知识》可以看看249-255页。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ColorAction extends AbstractAction
{ public ColorAction(String name, Icon icon, Color c, Component comp)
{ putValue(Action.NAME, name);
putValue(Action.SMALL_ICON, icon);
putValue("Color", c);
target = comp;
}
public void actionPerformed(ActionEvent evt)
{ Color c = (Color)getValue("Color");
target.setBackground(c);
target.repaint();
}
private Component target;
}
class ActionButton extends JButton
{ public ActionButton(Action a)
{ setText((String)a.getValue(Action.NAME));
Icon icon = (Icon)a.getValue(Action.SMALL_ICON);
if (icon != null)
setIcon(icon);
addActionListener(a);
}
}
class SeparateGUIFrame extends JFrame
{ public SeparateGUIFrame()
{ setTitle("SeparateGUITest");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
JPanel panel = new JPanel();
Action blueAction = new ColorAction("Blue",
new ImageIcon("blue-ball.gif"),
Color.blue, panel);
Action yellowAction = new ColorAction("Yellow",
new ImageIcon("yellow-ball.gif"),
Color.yellow, panel);
Action redAction = new ColorAction("Red",
new ImageIcon("red-ball.gif"),
Color.red, panel);
panel.add(new ActionButton(yellowAction));
panel.add(new ActionButton(blueAction));
panel.add(new ActionButton(redAction));
panel.registerKeyboardAction(yellowAction,
KeyStroke.getKeyStroke(KeyEvent.VK_Y, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
panel.registerKeyboardAction(blueAction,
KeyStroke.getKeyStroke(KeyEvent.VK_B, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
panel.registerKeyboardAction(redAction,
KeyStroke.getKeyStroke(KeyEvent.VK_R, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
Container contentPane = getContentPane();
contentPane.add(panel);
JMenu m = new JMenu("Color");
m.add(yellowAction);
m.add(blueAction);
m.add(redAction);
JMenuBar mbar = new JMenuBar();
mbar.add(m);
setJMenuBar(mbar);
}
}
public class SeparateGUITest
{ public static void main(String[] args)
{ JFrame frame = new SeparateGUIFrame();
frame.show();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ColorAction extends AbstractAction
{ public ColorAction(String name, Icon icon, Color c, Component comp)
{ putValue(Action.NAME, name);
putValue(Action.SMALL_ICON, icon);
putValue("Color", c);
target = comp;
}
public void actionPerformed(ActionEvent evt)
{ Color c = (Color)getValue("Color");
target.setBackground(c);
target.repaint();
}
private Component target;
}
class ActionButton extends JButton
{ public ActionButton(Action a)
{ setText((String)a.getValue(Action.NAME));
Icon icon = (Icon)a.getValue(Action.SMALL_ICON);
if (icon != null)
setIcon(icon);
addActionListener(a);
}
}
class SeparateGUIFrame extends JFrame
{ public SeparateGUIFrame()
{ setTitle("SeparateGUITest");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
JPanel panel = new JPanel();
Action blueAction = new ColorAction("Blue",
new ImageIcon("blue-ball.gif"),
Color.blue, panel);
Action yellowAction = new ColorAction("Yellow",
new ImageIcon("yellow-ball.gif"),
Color.yellow, panel);
Action redAction = new ColorAction("Red",
new ImageIcon("red-ball.gif"),
Color.red, panel);
panel.add(new ActionButton(yellowAction));
panel.add(new ActionButton(blueAction));
panel.add(new ActionButton(redAction));
panel.registerKeyboardAction(yellowAction,
KeyStroke.getKeyStroke(KeyEvent.VK_Y, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
panel.registerKeyboardAction(blueAction,
KeyStroke.getKeyStroke(KeyEvent.VK_B, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
panel.registerKeyboardAction(redAction,
KeyStroke.getKeyStroke(KeyEvent.VK_R, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
Container contentPane = getContentPane();
contentPane.add(panel);
JMenu m = new JMenu("Color");
m.add(yellowAction);
m.add(blueAction);
m.add(redAction);
JMenuBar mbar = new JMenuBar();
mbar.add(m);
setJMenuBar(mbar);
}
}
public class SeparateGUITest
{ public static void main(String[] args)
{ JFrame frame = new SeparateGUIFrame();
frame.show();
}
}
|
我试过了,按照magicshop提供的程序模式可以实现快捷键.如下:
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
class ButtonAction1 extends AbstractAction{
private Dialog2 d1 ;
ButtonAction1(JDialog frame) {
d1 = new Dialog2(frame,true);
}
public void actionPerformed(ActionEvent e) {
d1.show();
}
};
class ButtonAction2 extends AbstractAction{
private JTextField field;
ButtonAction2(JTextField fid){
field = fid;
}
public void actionPerformed(ActionEvent e) {
field.setText("this is a test");
}
};
public class Test {
public static void main(String[] args) {
DialogMain dm = new DialogMain();
dm.show();
}
}
class DialogMain extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
public DialogMain() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
this.setSize(500,400);
jButton1.setText("jButton1");
Action bAction1 = new ButtonAction1(this);
Action bAction2 = new ButtonAction2(jTextField1);
jButton1.addActionListener(bAction1);
jButton2.setText("jButton2");
jButton2.addActionListener(bAction2);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(152, 134, -1, -1));
jPanel1.add(jButton2, new XYConstraints(229, 66, -1, -1));
jPanel1.add(jTextField1, new XYConstraints(81, 74, 117, -1));
jPanel1.registerKeyboardAction(bAction1,
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
jPanel1.registerKeyboardAction(bAction2,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
}
}
class Dialog2 extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
public Dialog2(Dialog owner, boolean modal) {
super(owner, modal);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(500,400);
jPanel1.setLayout(xYLayout1);
jButton1.setText("faint");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(161, 116, -1, -1));
}
}
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
class ButtonAction1 extends AbstractAction{
private Dialog2 d1 ;
ButtonAction1(JDialog frame) {
d1 = new Dialog2(frame,true);
}
public void actionPerformed(ActionEvent e) {
d1.show();
}
};
class ButtonAction2 extends AbstractAction{
private JTextField field;
ButtonAction2(JTextField fid){
field = fid;
}
public void actionPerformed(ActionEvent e) {
field.setText("this is a test");
}
};
public class Test {
public static void main(String[] args) {
DialogMain dm = new DialogMain();
dm.show();
}
}
class DialogMain extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
public DialogMain() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
this.setSize(500,400);
jButton1.setText("jButton1");
Action bAction1 = new ButtonAction1(this);
Action bAction2 = new ButtonAction2(jTextField1);
jButton1.addActionListener(bAction1);
jButton2.setText("jButton2");
jButton2.addActionListener(bAction2);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(152, 134, -1, -1));
jPanel1.add(jButton2, new XYConstraints(229, 66, -1, -1));
jPanel1.add(jTextField1, new XYConstraints(81, 74, 117, -1));
jPanel1.registerKeyboardAction(bAction1,
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
jPanel1.registerKeyboardAction(bAction2,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
}
}
class Dialog2 extends JDialog {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
public Dialog2(Dialog owner, boolean modal) {
super(owner, modal);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(500,400);
jPanel1.setLayout(xYLayout1);
jButton1.setText("faint");
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, new XYConstraints(161, 116, -1, -1));
}
}
|
你要想用Enter直接响应,最好的办法是将每个组件都加键盘的响应事件,然后,执行你要的方法
例如:
public class Key_Listener implements KeyListener
{
DialogMain dlg
public void setDlg(DialogMain dlg)
{
this.dlg = dlg;
}
public void keyPressed(KeyEvent e)
{
key_press_event(e);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
void key_press_event(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_ENTER )
dlg.jButton1_actionPerformed();
if (e.getKeyCode()==KeyEvent.VK_ESCAPE)
dlg.jButton2_actionPerformed();
}
}
在你的程序中,每个组件都加响应事件
class DialogMain extends JDialog {
Key_Listener keyL = new Key_Listener();
keyL.setDlg(this);
........
jButton1.addKeyListener(keyL) ;
jButton2.addKeyListener(keyL) ;
........
jButton1的响应事件
public void jButton1_actionPerformed()
{
System.out.println("enter");
}
jButton2的响应事件
public void jButton2_actionPerformed()
{
System.out.println("exit");
}
}
例如:
public class Key_Listener implements KeyListener
{
DialogMain dlg
public void setDlg(DialogMain dlg)
{
this.dlg = dlg;
}
public void keyPressed(KeyEvent e)
{
key_press_event(e);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
void key_press_event(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_ENTER )
dlg.jButton1_actionPerformed();
if (e.getKeyCode()==KeyEvent.VK_ESCAPE)
dlg.jButton2_actionPerformed();
}
}
在你的程序中,每个组件都加响应事件
class DialogMain extends JDialog {
Key_Listener keyL = new Key_Listener();
keyL.setDlg(this);
........
jButton1.addKeyListener(keyL) ;
jButton2.addKeyListener(keyL) ;
........
jButton1的响应事件
public void jButton1_actionPerformed()
{
System.out.println("enter");
}
jButton2的响应事件
public void jButton2_actionPerformed()
{
System.out.println("exit");
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。