通过按钮更改标签(Java / Swing问题) [英] Changing a label through a button (Java/Swing issue)

查看:379
本文介绍了通过按钮更改标签(Java / Swing问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是新手程序员:

我刚刚按下了第一个按钮。我希望按钮更改标签 Hello World!。到你好宇宙!。我尝试通过 public void actionPerformed(ActionEvent e)寻找更改标签的方法,但未找到任何方法。如果有人愿意向我解释如何更改 public void actionPerformed(ActionEvent e)中的注释部分以更改标签,请解释!

I just made my first button. I want the button to change the label "Hello World!" to "Hello Universe!". I tried searching for ways to change the label through public void actionPerformed(ActionEvent e), but failed to find any. If anybody would be kind enough to explain to me how to change the commented section in public void actionPerformed(ActionEvent e) to change the label, please explain!

谢谢!

我的代码:

package game;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Javagame extends JPanel implements ActionListener{
    double x=Math.random()*500;
    double y=Math.random()*500;
    protected JButton b1;
    public Javagame() {
        b1 = new JButton("Button!");
        b1.setActionCommand("change");

        b1.addActionListener(this);
        add(b1);
    }
    public void actionPerformed(ActionEvent e) {
        if ("change".equals(e.getActionCommand())) {
            //I want a code here that changes "Hello World" to "Hello Universe". Thank you.
        }
    }
    private static void createWindow(){
        JFrame frame = new JFrame("Javagame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(500,500));

        JLabel label = new JLabel("Hello World!", SwingConstants.CENTER);
        label.setFont(new Font("Arial", Font.BOLD, 20));
        label.setForeground(new Color(0x009900));

        Javagame newContentPane = new Javagame();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.getContentPane().add(label, BorderLayout.CENTER);

        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        createWindow();
    }
}


推荐答案

因为您无从获取对标签的引用来对其进行更改。...

That's because you have no means of getting a reference to the label to change it....

将标签的减速度移至类级别...

Move the deceleration of the label to the class level...

public class Javagame extends JPanel implements ActionListener{
    double x=Math.random()*500;
    double y=Math.random()*500;
    protected JButton b1;
    // Add me...
    private JLabel label;

将标签移动到面板中

public Javagame() {
    b1 = new JButton("Button!");
    b1.setActionCommand("change");

    b1.addActionListener(this);
    add(b1);

    // Move me here
    label = new JLabel("Hello World!", SwingConstants.CENTER);
    label.setFont(new Font("Arial", Font.BOLD, 20));
    label.setForeground(new Color(0x009900));
    add(label);
}

然后在您的操作中执行方法,您将可以引用它...

Then in your actionPerformed method you will be able to reference it...

public void actionPerformed(ActionEvent e) {
    if ("change".equals(e.getActionCommand())) {
        label.setText("I have changed");
    }
}

这篇关于通过按钮更改标签(Java / Swing问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆