简体   繁体   English

JButton,setText不起作用?

[英]JButton, setText doesn't work?

I tried to make button which changes text of other button, but settext doesn't work. 我试图使按钮更改其他按钮的文本,但settext不起作用。

This is appdroid.java: 这是appdroid.java:

package appdroid; 包appdroid;

public class appdroid{

    static int a = 640;
    static int b = 400;
    static int c = 100;
    static int d = 100;

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

this is gui.java: 这是gui.java:

package appdroid;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class gui extends JFrame implements ActionListener {

    String[] a = {"Search device","Device not found","Error, bad device","Device found"};
    String[] b = {"Device not found","Error, bad device","Compile"};
    String[] c = {"Device not found","Error, bad device","Unpack"};
    String d = a[0];
    String f = b[0];
    String g = c[0];
    private JButton b1;
    private JButton b2;
    private JButton b3;

    public gui(){

        super("APPDROID");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(appdroid.a,appdroid.b);
        setLocation(appdroid.c,appdroid.d);
        setLayout(new FlowLayout());

        JButton b1 = new JButton(d);
        JButton b2 = new JButton(f);
        JButton b3 = new JButton(g);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);

        add (b1);
        add (b2);
        add (b3);
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if(source == b1){
            d = a[3];
            f = b[2];
            b1.setText(d);
            b2.setText(f);
        }

        if(source == b2){
            g = c[2];
            b3.setText(g);
        }

        if(source == b3 && g == c[2]){
            ;
        }
    }
}

I added action event to button, etc how you can see, but it's not working. 我向按钮等添加了动作事件,如何查看,但不起作用。

You have created 3 local variables within the gui() constructor 您已经在gui()构造函数中创建了3个局部变量

    JButton b1 = new JButton(d);
    JButton b2 = new JButton(f);
    JButton b3 = new JButton(g);

I think you wanted to initialize the fields? 我想您想初始化字段?

    b1 = new JButton(d);
    b2 = new JButton(f);
    b3 = new JButton(g);

since you are comparing source with the fields b1 , b2 .. in actionPerformed(ActionEvent e) { 由于您将sourceactionPerformed(ActionEvent e) {的字段b1b2 ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM