好吧,我有一个简单的java applet,带有两个按钮和一个屏幕。两个按钮都做同样的事情。我想改变这一点。我无法找到它是什么改变了当按下其中一个按钮时执行的动作。他们都是一样的东西,我不想要这个。所以我的问题是如何更改库存按钮以显示“Hello world”而不是行数?Java按钮做同样的事情,我该如何改变这一点?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class projectApplet extends JApplet implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea textArea;
private int lineNumber = 0; // this is just to test
public void init() {
JPanel panel = new JPanel();
textArea = new JTextArea();
textArea.setBackground(Color.BLACK);
textArea.setForeground(Color.WHITE);
JScrollPane sp = new JScrollPane(textArea);
panel.add(sp);
Container window = getContentPane();
window.setLayout(new BorderLayout());
window.add(sp,BorderLayout.CENTER);
// this is just to test------------------
JButton b = new JButton("Clik to add a line");
b.addActionListener(this);
window.add(b, BorderLayout.SOUTH);
JButton inventory = new JButton("Inventory");
inventory.addActionListener(this);
window.add(inventory, BorderLayout.NORTH);
//---------------------------------------
}
public void actionPerformed(ActionEvent arg0) {
lineNumber++;
textArea.append("\nLine number: " + lineNumber);
}
public void actionPerformed1(ActionEvent arg0) {
lineNumber++;
textArea.append("RPFL");
}
}
谢谢你,这工作,我想要它!我会尽快接受这个答案! – 2011-02-19 05:01:56
虽然这符合当前的目的,但用于区分哪个按钮导致此操作的功能在开发更大的应用程序时会很有帮助,例如使用event.getSource()等 – sashank 2011-02-19 05:02:05