java - 通过 EventListener 调用 repaint() 和/或 revalidate() - Swing

标签 java swing jframe jbutton repaint

最近我在 Swing 方面遇到了一些问题。我正在创建一个需要在 JFrame 和 JPanel 中频繁编辑内容(在本例中为 JButton 上显示的字符串)的项目,我想了解如何执行此操作。

我搜索了很长时间,找到的主要答案是我需要调用 .repaint(),可能在调用 .revalidate() 之后。但是,我无法让我的代码正常运行。

现在,框架将按原样绘制,但按下按钮后,其中的文本不会改变 - 事实上,它会产生一个大错误日志,可在此处查看:https://pastebin.com/7P85cB8h

下面是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Actions extends JFrame implements ActionListener
{
    JButton Beans;
    String String1;
    JPanel things;

    public static void main (String[] args)
    {
        new Actions();
    }

    public Actions()
    {
        JPanel things = new JPanel();
        String1 = "Beans";

        this.setSize(400,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Hello there");

        Box theBox = Box.createHorizontalBox();

        Beans = new JButton("" + String1);
        Beans.addActionListener(this);

        theBox.add(Box.createHorizontalStrut(50));
        theBox.add(Beans);

        this.add(theBox);

        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        String1 = "Surprise!";
        things.revalidate();
        things.repaint();
    }
}

所以,澄清一下,我有一个 JButton,在 JPanel 中,在 JFrame 中。该按钮本身显示一个字符串,最初显示为“Beans”。当我按下按钮时,我希望字符串现在显示为“Surprise!”。

感谢您的宝贵时间。

最佳答案

您的问题是将对象与引用变量混淆,认为更改 String1 的文本会神奇地导致 JButton 显示的文本发生变化,但这不是 Java 的 OOP 模型的工作方式。了解 JButton 正在显示一个 String 对象,String1 最初引用的是同一个对象,但是当您更改 String1 引用的 String 时,这对原始 String 对象没有影响。为了更改显示的字符串,您必须更改 JButton 显示的字符串对象,方法是调用 JButton 的 setText(...) 方法并将新的字符串传递给它。这是唯一可行的方法。

public void actionPerformed(ActionEvent e) {
    Beans.setText("Surprise!");
}

查看评论:

// 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

    //....

enter image description here

顺便说一句,您需要学习和使用 Java naming conventions .变量名应全部以小写字母开头,而类名应以大写字母开头。了解并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。

旁注 #2:如果您实际上绘制 字符串,那么您的原始代码就可以工作。请注意,在下面的代码中,我有一个字符串变量 currentString,它最初指的是字符串数组 TEXTS 中的第一项,字符串 "One"。在 JButton 的 ActionListener 中,我更新了名为 index 的数组索引变量,并将 currentString 变量设置为数组中的下一个 String 项,然后调用 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());
    }
}

关于java - 通过 EventListener 调用 repaint() 和/或 revalidate() - Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488908/

相关文章:

java - 尝试将多个面板对象添加到创建基本 JFrame 的不同类

Java( Swing ): Annoyance resizing JFrame by dragging the bottom left corner

java - Apache NetBeans 9.0 ant 构建失败

java - Java 中不稳定的 GUI

java - paintComponent 在 JPanel 上不起作用

java - Swing应用问题

java - setVisible(true/false)错误JFrame

java - 将位字符串转换为 double

java - 升级 SQLiteDatabase Android

java - 使用 Jackson 使用循环创建 Json 字符串