通过按钮更改标签(Java / Swing问题)

时间:2013-05-31 01:10:30

标签: java swing jbutton actionlistener jlabel

新手程序员:

我刚做了第一个按钮。我想按钮改变标签" Hello World!" to" Hello Universe!"。我试图通过public void actionPerformed(ActionEvent e)搜索更改标签的方法,但未能找到任何标签。如果有人愿意向我解释如何更改public void actionPerformed(ActionEvent e)中的评论部分以更改标签,请解释!

谢谢!

我的代码:

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

2 个答案:

答案 0 :(得分:3)

那是因为你无法获得对标签的引用来改变它....

将标签的减速度移动到班级......

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

然后在您的actionPerformed方法中,您将能够引用它......

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

答案 1 :(得分:0)

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