Java 带有3个JButton的GUI,以及一个存储所选按钮/列表参数并可单独显示它们的列表

Java 带有3个JButton的GUI,以及一个存储所选按钮/列表参数并可单独显示它们的列表,java,Java,我想制作一个GUI,其中有一个列表,其中可以选择一个项目,3个JButtons、ON、OFF和STATE。选择Jbutton“State”时,会弹出一个单独的框,告诉用户是否选择了开/关,以及从JList中选择了哪个参数。这是到目前为止我的代码。弹出框一直告诉我加热器已打开,因为它一直显示on=true。此外,我试图从列表中选择的部分也是完全错误的,因为某些原因我看不到 import java.awt.*; import java.awt.event.*; import javax.swing.

我想制作一个GUI,其中有一个列表,其中可以选择一个项目,3个JButtons、ON、OFF和STATE。选择Jbutton“State”时,会弹出一个单独的框,告诉用户是否选择了开/关,以及从JList中选择了哪个参数。这是到目前为止我的代码。弹出框一直告诉我加热器已打开,因为它一直显示on=true。此外,我试图从列表中选择的部分也是完全错误的,因为某些原因我看不到

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class Heater extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private Object state;
private boolean on;
private Object inten;

//private JList HeatIntensity;
private String intensity[] = {"Level 1", "Level 2", "Level 3", "Level 4", "Level 5"};



public Heater() {

    super("Heater");

    //creating box
    box(intensity);
}


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

@Override
public void actionPerformed(ActionEvent ae) {
    String action = ae.getActionCommand();

    //if on button is pressed, on = true
    if (action.equals("ON")) {
        on = true;
    }

    //if off pressed on = false
    else if (action.equals("OFF")) {
        on = false;
    }
    if (action.equals("State")) {

        //if the on/off button was pressed then the state button, a new box pops up     telling if on / off
        if(on = true){
            JOptionPane.showMessageDialog(null, "on");
        }
        else if(on = false)
            JOptionPane.showMessageDialog(null, "The Heawdadwqwdqwdater is off");

        }

    //when somthing is pressed on the list, show that in a new pop up box
    /*intensity.additemListener(
            new itemlistener(){
            public void itemChanged(ItemEvent event){
                if(event.getStateChange() == ItemEvent.SELECTED){
                    JOptionPane.showMessageDialog(null, ItemEvent.SELECTED);
                }
                } 
            } 
            );
            */
    }



    public void box(String[] a){

    //creating the box
    this.getContentPane().setLayout(new FlowLayout());
    setSize(new Dimension(900, 300));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    //creating Jbuttons
    JButton buttonon = new JButton("ON");
    JButton buttonoff = new JButton("OFF");
    JButton buttonState = new JButton("State");
    //creating list

    JList list = new JList(a);
    list.setVisibleRowCount(5);
    add(list);

    //adding buttons
    add(buttonon);
    add(buttonoff);
    add(buttonState);
    buttonon.addActionListener(this);
    buttonoff.addActionListener(this);
    buttonState.addActionListener(this);
    setVisible(true);   
}
}

问题在于这段代码:

if(on = true){
    JOptionPane.showMessageDialog(null, "on");
}
else if(on = false)
    JOptionPane.showMessageDialog(null, "The Heawdadwqwdqwdater is off");
执行
if(on=true)
true
分配给
on
而不是检查它是否为
true
。这意味着块中的代码将始终被执行。这同样适用于
else if
,尽管它永远不会到达它。 正确的方法是

if (on) {
    JOptionPane.showMessageDialog(null, "on");
}
else {
    JOptionPane.showMessageDialog(null, "The Heawdadwqwdqwdater is off");
}

你看不到你的钮扣了吗?我能看到。就在我选择它们之后,我的输出就全错了。