2014-12-27 45 views
0

我试图运行一个实验,我似乎无法找到任何提供任何帮助的地方。不同的按钮执行不同的操作

我的实验是一组多个按钮,每个按钮在SwingView的文本框中分别打印一行文本。

我有多个按钮,但每个按钮都会导致相同的ActionListener。

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

public class TextArea1 implements ActionListener{ 
JTextArea text; 
int numClick = 0; 

public static void main(String[] args){ 
    TextArea1 gui = new TextArea1(); 
    gui.go(); 
    } 

public void go(){ 
    JFrame aFrame = new JFrame(); 

    JPanel aPanel = new JPanel(); 
    JPanel aPanel2 = new JPanel(); 
    JPanel aPanel3 = new JPanel(); 
    JPanel aPanel4 = new JPanel(); 
    JPanel aBoard = new JPanel(); 

    JButton aButton = new JButton("Just Click it"); 
    JButton aButton1 = new JButton("1"); 
    JButton aButton2 = new JButton("2"); 
    ... 
    JButton aButton9 = new JButton("9");   

    aPanel2.setBackground(Color.darkGray); 
    aPanel3.setBackground(Color.darkGray); 
    aPanel4.setBackground(Color.darkGray); 
    aBoard.setLayout(new GridLayout(3,3)); 

    aBoard.add(aButton1); 
    aBoard.add(aButton2); 
    ... 
    aBoard.add(aButton9); 

    aButton.addActionListener(this); 
    aButton1.addActionListener(this); 
    aButton2.addActionListener(this); 
    ... 
    aButton9.addActionListener(this); 

    text = new JTextArea(3,20); 
    text.setLineWrap(true); 

    JScrollPane scroller = new JScrollPane(text); 
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
    scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 

    aPanel.add(scroller); 

    aFrame.getContentPane() .add(BorderLayout.EAST, aPanel2); 
    aFrame.getContentPane() .add(BorderLayout.WEST, aPanel3); 
    aFrame.getContentPane() .add(BorderLayout.NORTH, aPanel4); 
    aFrame.getContentPane() .add(BorderLayout.SOUTH, aPanel); 
    aFrame.getContentPane() .add(BorderLayout.CENTER, aBoard); 

    aFrame.setSize(350,300); 
    aFrame.setVisible(true); 
    } 

public void actionPerformed(ActionEvent ev){ 
    numClick++; 
    text.append("button clicked " + numClick + "time(s) \n"); 
    } 
} 

这是我迄今写的。每次点击一个按钮时,我已经获得了用于打印新文本的代码。但代码不会每个按钮分开来,所以如果它的按钮1或BUTTON2不要紧,同样的情况发生

+2

我有一个疯狂的想法:使用不同的一个'ActionListener'为每个不同的按钮。 – Tom

+0

为您想要做不同事情的按钮编写不同的'ActionListener'类。 –

+0

请考虑看看[如何使用操作](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) – MadProgrammer

回答

1

如果需要使用相同的ActionListener所有JButton S,然后用

aButton.setActionCommand("First"); 

同样,对于其他JButton S设定ActionCommands也和在actionPerformed方法,使用

if(ev.getActionCommand.equals("First")) 
    // aButton was pressed as the actionCommand of it is "First" 

而且同样添加其他if s到检查OT她按了一下JButton

1

看一看How to Use Actions,这可以让你隔离每个按钮的功能,但也使其可用于其他事情,如键绑定和菜单。

public class JustClickIt extends AbstractAction { 
    public JustClickIt() { 
     putValue(NAME, "Just Click It"); 
    } 

    public void actionPerformed(ActionEvent evt) { 
     // Make it happen 
    } 
} 

然后,只需将它应用到按钮...

JButton aButton = new JButton(new JustClickIt()); 
相关问题