2016-03-10 26 views
1

所以我想我的按钮被标记为1-9,但我不想列出每个按钮的所有动作侦听器和动作命令。我怎样才能做到这一点如何把actionlistenerand actioncommand放到多个jbuttons

,也是我无法使用add.ActionListener(本),所以我可以用

JButton[] button = new JButton[9]; 
    panel.setLayout(new GridLayout(3,3)); 
    for (int i = 0; i < button.length; i++) { 
     button[i] = new JButton(); 
     panel.add(button[i]); 
     String bu = Integer.toString(i); 
     button[i].setActionCommand(bu); 
     button[i].addActionListener(new ActionListener()); 

对不起进出口新的的Java Swing所以其升技混淆仍然

+1

也许可以考虑使用'Action'代替,参见[如何使用操作(http://docs.oracle.com/javase /tutorial/uiswing/misc/action.html) – MadProgrammer

+0

@kina'我无法使用add.ActionListener(this)'我会将其解释为“您仍然可以使用ActionListener”只是您不想让容器类实现了一个ActionListener。对? – user3437460

回答

2

我不能使用add.ActionListener(本),所以我可以用

您创建一个实现一个ActionListener类。

最好还是创建一个实现Action的类。 Action与ActionListener相同。好处是可以对键绑定使用Action。

这是一个基本的例子:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 

public class CalculatorPanel extends JPanel 
{ 
    private JTextField display; 

    public CalculatorPanel() 
    { 
     Action numberAction = new AbstractAction() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
//    display.setCaretPosition(display.getDocument().getLength()); 
       display.replaceSelection(e.getActionCommand()); 
      } 
     }; 

     setLayout(new BorderLayout()); 

     display = new JTextField(); 
     display.setEditable(false); 
     display.setHorizontalAlignment(JTextField.RIGHT); 
     add(display, BorderLayout.NORTH); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new GridLayout(0, 5)); 
     add(buttonPanel, BorderLayout.CENTER); 

     for (int i = 0; i < 10; i++) 
     { 
      String text = String.valueOf(i); 
      JButton button = new JButton(text); 
      button.addActionListener(numberAction); 
      button.setBorder(new LineBorder(Color.BLACK)); 
      button.setPreferredSize(new Dimension(30, 30)); 
      buttonPanel.add(button); 

      InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 
      inputMap.put(KeyStroke.getKeyStroke(text), text); 
      inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text); 
      button.getActionMap().put(text, numberAction); 
     } 
    } 

    private static void createAndShowUI() 
    { 
     JFrame frame = new JFrame("Calculator Panel"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new CalculatorPanel()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 

现在,您可以点击按钮或输入号码和值会被插入到文本字段中。

0

只需添加上主要行动执行方法。

例子是这样的:

public void actionPerformed(ActionEvent e){ 
     // your todo code here 
} 

确保导入相应的软件包。

0

你必须实现ActionListener

公共类对接实现的ActionListener {

public JPanel method() 
{ 

JPanel panel = new JPanel(); 
JButton[] button = new JButton[9]; 
panel.setLayout(new GridLayout(3, 3)); 
for (int i = 0; i < button.length; i++) 
{ 
    button[i] = new JButton(""+i); 
    panel.add(button[i]); 
    String bu = Integer.toString(i); 
    button[i].setActionCommand(bu); 
    button[i].addActionListener(this); 

} 
return panel; 
} 

@Override 
public void actionPerformed(ActionEvent e) 
{ 

} 

public static void main(String[] args) 
{ 

JFrame frame = new JFrame(); 
frame.add(new Butt().method()); 
frame.setVisible(true); 
frame.setSize(500, 500); 
} 

} 看看现在有没有错误。

results

0

,也是我无法使用add.ActionListener(本),所以我可以用什么

我会解释你在这里的意思是“你不能让容器类实现了ActionListener,但仍允许使用ActionListener“。

如果是这样的话,你至少有2种选择:

  • 为ActionListener的
  • 创建一个匿名类的ActionListener的

创建一个内部类使用GridLayout与内部类Actionlistener的示例:

enter image description here

如何把actionlistenerand actioncommand多个Jbuttons中

下使用内部类来处理按钮的动作。

class MainPanel extends JPanel //not implementing ActionListener here 
{ 
    private JButton[] btns; 

    public MainPanel(){ 
     setPreferredSize(new Dimension(150, 150)); 
     setLayout(new GridLayout(3, 3)); 
     initComponents(); 
     addComponents(); 
    } 

    private void initComponents(){ 
     btns = new JButton[9]; 
     ButtonHandler bh = new ButtonHandler(); 
     for(int x=0; x<btns.length; x++){ 
      btns[x] = new JButton(Integer.toString(x+1)); 
      btns[x].addActionListener(bh); //NOT using addActionListener(this) 
     }     
    } 

    private void addComponents(){ 
     for(int x=0; x<btns.length; x++) 
      add(btns[x]);      //Add in sequential order into grid layout 
    } 

    private class ButtonHandler implements ActionListener 
    { 
     @Override 
     public void actionPerformed(ActionEvent e){ 
      String s = ((JButton)e.getSource()).getText(); 
      JOptionPane.showMessageDialog(null, "Button " + s + " was clicked."); 
     } 
    } 
} 

最后,在美国东部时间运行代码:

class TestRunner 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable(){ 
      @Override 
      public void run(){ 
       JFrame frame = new JFrame("Buttons Pad"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new MainPanel()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true);    
      } 
     }); 
    } 
} 
+0

@kina你可以看看这里,看看它是否有帮助。 – user3437460