我是java的新手,我正在尝试更换我创建的Button上的文本。我的主要类的代码如下:
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)方法中更改为文本。就像我说的,我是新人,所以请保持温柔。谢谢:))
答案 0 :(得分:4)
您正在使用java.awt.Button
。 java.awt.Button中没有setText()
方法。您可以改用setLabel(String)
。
并且您不必导入java.lang。*,因为默认情况下java.lang包可用于所有Java程序。
如果您更改了该行:
Button BtnStart;
到
JButton BtnStart;
和
BtnStart = new Button("add row/column");
到
BtnStart = new JButton("add row/column");
然后您将使用Swing按钮,您将能够调用setText();
答案 1 :(得分:1)
您需要知道的第一件事是您尝试使用AWT或Swing组件创建Applet。您导入Swing类但使用AWT组件。现在大多数人都使用Swing。
在Swing中,你永远不会覆盖Applet的paint()方法。您可以从扩展JApplet开始,然后只需将组件添加到applet的内容窗格即可。如果您需要进行自定义绘制,那么可以通过覆盖JComponent或JPanel的paintComponent()方法来实现。
首先阅读Swing教程,了解使用applet的工作示例。
答案 2 :(得分:1)
正如你所说想要交换文本那么你应该使用setLabel()方法而不是setText,但是为了更改Label的文本,你可以使用setText()方法。