你运行一下这个程序。可能会有点启发。如果你有《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();
      }
}

解决方案 »

  1.   

    各位如果昨天未看过同主题的帖子,请看http://www.csdn.net/expert/topic/774/774090.xml?temp=.5046198
      

  2.   

    to  magicshop(本着不懂就问的原则)
    我的和你给的例子不一样,我的每个按钮的actionPerformed(ActionEvent evt)事件所需要处理的事务都不同,不大好自定义一个JButton的扩展类,当然,如果可以重写actionPerformed的话,倒可以考虑。
    请问能否针对我上面的需求,帮我修改一下代码?
      

  3.   

    说实话我刚学java,以前也没什么编程经验。我刚学到这里。我看了你的代码,呵呵不是很明白。可能帮不上你太大的忙。不过我是多么希望可以帮别人解决问题阿,但水平有限。惭愧惭愧:(
      

  4.   

    我的代码是随便用jb生成的,就是两个按钮

    我昨天问了一天,就只知道可以设置"Alt+键"的
      

  5.   

    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));
      }
    }
      

  6.   

    我试过了,按照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));
      }
    }
      

  7.   

    不是呀!你只要写一个类继承ActionListener就可以了!
      

  8.   

    你要想用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");
    }
    }
      

  9.   

    以上的方法都不行,根本就没有解决实际问题
    其一:实际上我的每个按钮的事件并不是那么简单,比如楼上 chinaredflag(chinaredflag) 的这个public void jButton2_actionPerformed()就少了参数ActionEvent e,那我点击的事件就不好设置了(难道还要我做两个参数不同的方法?这样在扩展这个类的时候,重写此方法又会出问题)。
    其二:我的需要是设置快捷键,但不是为了设置快捷键就要改动原有的按钮按下的事件响应,比如 linkok() 的将按钮事件集成到一个扩展的JButton里面,这是肯定不行的,因为我调用这个页面的时候,很多情况下还要扩展这个页面,需要重写按钮事件等等。这样集成,我就没有办法针对不同情况下进行重写了。
    所以我的问题是:
    在不改动  
     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里面),设置快捷键。有无大虾能够实现这一点?
      

  10.   

    我刚刚试了一下, shihb()是最符合我要的
    结贴
      

  11.   

    这样应该没问题了吧
    呵呵,试试吧
    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();
      }
    }
      

  12.   

    这样应该没问题了吧
    呵呵,试试吧
    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();
      }
    }