Java:将 setText() 方法与 Button 一起使用时出现问题

发布于 2024-08-08 12:43:36 字数 3300 浏览 3 评论 0原文

我是java新手,我正在尝试交换我创建的按钮上的文本。我的主类的代码如下:

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

public class TeamProject extends Applet implements ActionListener, MouseListener
{
char[][] charValues = new char[10][10];
Table aTable;
boolean allowUserInput = false;
Button BtnStart;
Button randomChangeBtn;
boolean guessMode;
private AudioClip[] sounds = new AudioClip[5];
private int counter = 0;
//JSObject jso;

public void init()
{
        //setup buttons
    BtnStart = new Button("add row/column");
    BtnStart.addActionListener((ActionListener)this);   //cast
    randomChangeBtn = new Button("change one value");
    randomChangeBtn.addActionListener((ActionListener)this);

            //add button
    this.add(BtnStart);

    //add image to Image objects
    Image imgO = getImage(getCodeBase(), "images/not.gif");
    Image imgX= getImage(getCodeBase(), "images/cross.gif");

            //setup table
    aTable = new Table(100, 100, 75, 55, 5, 5, imgX, imgO);

    //setBackground(Color.LIGHT_GRAY);
    super.resize(700, 700);

            //add mouse listener
    addMouseListener(this);

    //initially guessMode will be false
    guessMode = false;

            //to talk to javascript
    //jso = JSObject.getWindow(this);

    sounds[0] = getAudioClip (getCodeBase(), "images/buzzthruloud.wav");
    sounds[1] = getAudioClip (getCodeBase(), "images/inconceivable4.wav");
    sounds[2] = getAudioClip (getCodeBase(), "images/foghorn.wav");
    sounds[3] = getAudioClip (getCodeBase(), "images/waiting.wav");
    sounds[4] = getAudioClip (getCodeBase(), "images/whistldn.wav");
}

  public void paint(Graphics g)
  {
g.setColor(Color.black);
aTable.draw(g);
  }

  //Mouse listener methods
  public void mousePressed (MouseEvent e)
  {
    if(!guessMode){
  if ((allowUserInput)){
    aTable.swapSquareValue(e.getX(), e.getY());
    repaint();
  }
    }
    else{
      System.out.println("guessed row = " + e.getY() + " guessed col = " + e.getX());
      if(aTable.checkGuess(e.getX(), e.getY())){
        int n = JOptionPane.showConfirmDialog(null, "Excellent!! Would you like to progress to next level",

                                         "Correct!!!", JOptionPane.YES_NO_OPTION);

    if (n == JOpionPane.YES_OPTION) {


               }
 else{

    JOptionPane.showMessageDialog(null, "Nope", "alert", JOptionPane.INFORMATION_MESSAGE);

    sounds[counter].play();

  }



  //repaint();

}



  }



  public void mouseClicked (MouseEvent e) {}

  public void mouseEntered (MouseEvent e) {}

  public void mouseReleased (MouseEvent e) {}

  public void mouseExited (MouseEvent e) {}



  //Button action listener

public void actionPerformed(ActionEvent e)

{

    if (e.getSource() == BtnStart) {

      aTable.addRow();

      aTable.addColumn();

      BtnStart.setText("Roseindia.net");

      //this.remove(BtnStart);

      //this.add(randomChangeBtn);

      super.resize(700, 700);

      repaint();

}

else if (e.getSource() == randomChangeBtn) {

  aTable.randomChangeFunc();

  repaint();

  guessMode = true;

}



    allowUserInput = true;

    System.out.println(aTable.toString());

}



}

我试图在我的actionPerformed(ActionEvent e) 方法中更改为文本。就像我说的,我是新人,所以请温柔点。谢谢 :)

I'm new to java and I'm trying to swap out the text on a Button I've created. The code for my main class is as follows:

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

public class TeamProject extends Applet implements ActionListener, MouseListener
{
char[][] charValues = new char[10][10];
Table aTable;
boolean allowUserInput = false;
Button BtnStart;
Button randomChangeBtn;
boolean guessMode;
private AudioClip[] sounds = new AudioClip[5];
private int counter = 0;
//JSObject jso;

public void init()
{
        //setup buttons
    BtnStart = new Button("add row/column");
    BtnStart.addActionListener((ActionListener)this);   //cast
    randomChangeBtn = new Button("change one value");
    randomChangeBtn.addActionListener((ActionListener)this);

            //add button
    this.add(BtnStart);

    //add image to Image objects
    Image imgO = getImage(getCodeBase(), "images/not.gif");
    Image imgX= getImage(getCodeBase(), "images/cross.gif");

            //setup table
    aTable = new Table(100, 100, 75, 55, 5, 5, imgX, imgO);

    //setBackground(Color.LIGHT_GRAY);
    super.resize(700, 700);

            //add mouse listener
    addMouseListener(this);

    //initially guessMode will be false
    guessMode = false;

            //to talk to javascript
    //jso = JSObject.getWindow(this);

    sounds[0] = getAudioClip (getCodeBase(), "images/buzzthruloud.wav");
    sounds[1] = getAudioClip (getCodeBase(), "images/inconceivable4.wav");
    sounds[2] = getAudioClip (getCodeBase(), "images/foghorn.wav");
    sounds[3] = getAudioClip (getCodeBase(), "images/waiting.wav");
    sounds[4] = getAudioClip (getCodeBase(), "images/whistldn.wav");
}

  public void paint(Graphics g)
  {
g.setColor(Color.black);
aTable.draw(g);
  }

  //Mouse listener methods
  public void mousePressed (MouseEvent e)
  {
    if(!guessMode){
  if ((allowUserInput)){
    aTable.swapSquareValue(e.getX(), e.getY());
    repaint();
  }
    }
    else{
      System.out.println("guessed row = " + e.getY() + " guessed col = " + e.getX());
      if(aTable.checkGuess(e.getX(), e.getY())){
        int n = JOptionPane.showConfirmDialog(null, "Excellent!! Would you like to progress to next level",

                                         "Correct!!!", JOptionPane.YES_NO_OPTION);

    if (n == JOpionPane.YES_OPTION) {


               }
 else{

    JOptionPane.showMessageDialog(null, "Nope", "alert", JOptionPane.INFORMATION_MESSAGE);

    sounds[counter].play();

  }



  //repaint();

}



  }



  public void mouseClicked (MouseEvent e) {}

  public void mouseEntered (MouseEvent e) {}

  public void mouseReleased (MouseEvent e) {}

  public void mouseExited (MouseEvent e) {}



  //Button action listener

public void actionPerformed(ActionEvent e)

{

    if (e.getSource() == BtnStart) {

      aTable.addRow();

      aTable.addColumn();

      BtnStart.setText("Roseindia.net");

      //this.remove(BtnStart);

      //this.add(randomChangeBtn);

      super.resize(700, 700);

      repaint();

}

else if (e.getSource() == randomChangeBtn) {

  aTable.randomChangeFunc();

  repaint();

  guessMode = true;

}



    allowUserInput = true;

    System.out.println(aTable.toString());

}



}

I'm trying to change to text in my actionPerformed(ActionEvent e) method. Like I said, I'm new, so please be gentle. Thanks :)

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

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

发布评论

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

评论(3

乞讨 2024-08-15 12:43:36

您正在使用java.awt.Button。 java.awt.Button 中没有 setText() 方法。您可以使用 setLabel(String) 代替。

而且您也不必导入 java.lang.*,因为默认情况下 java.lang 包可供所有 Java 程序使用。

如果您将行: 更改

 Button BtnStart;

 JButton BtnStart;

 BtnStart = new Button("add row/column");

to

 BtnStart = new JButton("add row/column");

那么您将使用 Swing Button 并且您将能够调用 setText();

You are using java.awt.Button. There is no setText() method in the java.awt.Button. You may use setLabel(String) instead.

And you do not have to import java.lang.* either since the java.lang package is available to all your Java programs by default.

If you change the line:

 Button BtnStart;

to

 JButton BtnStart;

and

 BtnStart = new Button("add row/column");

to

 BtnStart = new JButton("add row/column");

then you will be using the Swing Button and you will be able to call setText();

混浊又暗下来 2024-08-15 12:43:36

您需要知道的第一件事是您是否尝试使用 AWT 或 Swing 组件创建 Applet。您导入 Swing 类,但使用 AWT 组件。现在大多数人都使用 Swing。

在 Swing 中,您永远不会重写 Applet 的 Paint() 方法。您可以从扩展 JApplet 开始,然后只需将组件添加到小程序的内容窗格中。如果您需要进行自定义绘制,那么您可以通过重写 JComponent 或 JPanel 的 PaintComponent() 方法来实现。

首先阅读 Swing 教程,了解使用小程序的工作示例。

The first thing you need to know is are you trying to create an Applet using AWT or Swing components. You import the Swing classes but are using AWT components. Most people these days use Swing.

In Swing your would never override the paint() method of the Applet. You would start by extending JApplet, then you would simply add components to the content pane of the applet. If you need to do custom painting then you do that by overriding the paintComponent() method of a JComponent or JPanel.

Start by reading the Swing tutorial for working examples of using applets.

想念有你 2024-08-15 12:43:36

正如您所说,您想交换文本,那么您应该使用 setLabel() 方法而不是 setText,但是要更改标签的文本,您可以使用 setText() 方法。

As you said that you want to swap the text then you should use the setLabel() method instead of setText, but for changing the text of a Label then you can use the setText() method.

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