Changing a label through a button (Java/Swing issue)

删除回忆录丶 提交于 2019-12-12 05:15:05

问题


newbie programmer here:

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!

Thanks!

My code:

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();
    }
}

回答1:


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;

Move the label to reside in the panel

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");
    }
}



回答2:


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);
}

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


来源:https://stackoverflow.com/questions/16848311/changing-a-label-through-a-button-java-swing-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!