您的问题是将对象与引用变量混淆,认为更改 String1 的文本会神奇地导致 JButton 显示的文本发生更改,但这不是 Java 的 OOP 模型的工作方式。了解 JButton 正在显示一个 String 对象,即 String1 最初引用的同一对象,但是当您更改 String1 引用的 String 时,这 对原始 String 对象没有影响。为了更改显示的字符串,您必须通过调用 JButton 的setText(...) 方法来更改 JButton 显示的字符串对象并将新字符串传递给它。这是唯一可行的方法。
public void actionPerformed(ActionEvent e) {
Beans.setText("Surprise!");
}
见 cmets:
// here are several reference variables
// all without assigned objects, and thus
// all holding "null" values:
JButton Beans;
String String1;
JPanel things;
public Actions() {
//.....
// here you assign the String object, "Beans" to the String1 variable
String1 = "Beans";
// .....
// here you create a JButton and pass in String1's current object, "Beans"
// into the constructor (note the "" + is NOT needed for Strings, only for numberrs)
Beans = new JButton("" + String1);
//.....
}
public void actionPerformed(ActionEvent e) {
// here you change the object that String1 refers to
String1 = "Surprise!";
// but this has no effect on the original String object, "Beans" displayed in the
// JButton, but rather all it does is change the state of String1.
// To change the state of the JButton, you must explicitly do this
// by calling setText on it
//....
顺便说一句,你会想学习和使用Java naming conventions。变量名应全部以小写字母开头,而类名应以大写字母开头。学习这一点并遵循这一点将使我们能够更好地理解您的代码,并让您更好地理解其他人的代码。
请注意#2:如果您确实绘制字符串,那么您的原始代码将可以工作。请注意,在下面的代码中,我有一个字符串变量currentString,它最初引用字符串数组中的第一项,TEXTS,即字符串"One"。在JButton 的ActionListener 中,我更新了数组索引变量index,并将currentString 变量设置为数组中的下一个字符串项,然后调用repaint()。这段代码起作用的原因是因为我在JPanel的绘画方法paintComponent(...)中绘制currentString持有的文本:
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.*;
public class DrawStringPanel extends JPanel {
private static final String[] TEXTS = {
"One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten"
};
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final Font TEXT_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 40);
private static final int TEXT_X = 150;
private static final int TEXT_Y = 200;
private int index = 0;
// Note that this String variable holds the first item in the TEXTS array
private String currentString = TEXTS[index];
public DrawStringPanel() {
setPreferredSize(new Dimension(PREF_W, PREF_H));
JButton nextBtn = new JButton("Next");
add(nextBtn);
nextBtn.addActionListener(e -> {
// update the array index
index++; // get next index
index %= TEXTS.length; // but don't let get bigger then array length
// and in the ActionListener here I'm changing the variable and calling repaint
// this works because this variable is actually painted within this JPanel's
// paintComponent method....
currentString = TEXTS[index];
repaint();
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setFont(TEXT_FONT);
// ****** HERE ****** I draw the contents of the currentString variable
g2.drawString(currentString, TEXT_X, TEXT_Y);
}
private static void createAndShowGui() {
DrawStringPanel mainPanel = new DrawStringPanel();
JFrame frame = new JFrame("DrawStringPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}