Java 按钮做同样的事情,我该如何改变它?

发布于 2024-10-18 07:02:35 字数 1469 浏览 2 评论 0原文

好吧,我有一个简单的 Java 小程序,有两个按钮和一个屏幕。两个按钮执行相同的操作。我想改变这一点。我找不到是什么改变了按下任一按钮时执行的操作。他们都做同样的事情,我不想要这个。所以我的问题是如何更改库存按钮以显示“Hello world”而不是行数?

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

 }
}

Alright, I have a simple java applet with two buttons and a screen. Both the buttons do the same thing. I want to change this. I can't find what it is that changes the action that is performed when either one of the buttons is pressed. They both to the same thing and I don't want this. So my question is how would I change the Inventory button to display "Hello world" instead of a line count?

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

 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青丝拂面 2024-10-25 07:02:35

向其中添加一个新的动作侦听器。通常您可以使用匿名内部类:

inventory.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent ae) {
    textArea.append("Hello, world");
  }
});

Add a new action listener to it. Typically you can use an anonymous inner class:

inventory.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent ae) {
    textArea.append("Hello, world");
  }
});
满地尘埃落定 2024-10-25 07:02:35

只需使用一个 actionPerformed 方法,然后找出哪个按钮触发了它。

例如:

public void actionPerformed(ActionEvent arg0) {
    if(arg0.getLabel()=="Inventory") // Do the following
    if(arg0.getLabel()=="Click to add a new line") // Do the following
}

注意, getLabel() 方法已被弃用,因此您必须使用另一个...我记不起您应该使用哪个...也许是 getName()。但这是测试单击哪个按钮的简单方法;)

Just have the one actionPerformed method and then find out which button triggered it.

For example:

public void actionPerformed(ActionEvent arg0) {
    if(arg0.getLabel()=="Inventory") // Do the following
    if(arg0.getLabel()=="Click to add a new line") // Do the following
}

Note, getLabel() method is deprecated so you'll have to use another... can't remember off the top of my head which you should though... maybe getName(). But this is a simple way to test which button was clicked ;)

彩虹直至黑白 2024-10-25 07:02:35

您无法在操作执行方法中执行 arg0.getSource() 来检查哪个按钮生成了此事件。

You can't do arg0.getSOurce() inside the action performed method to checkout which button has generated this event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文