vendredi 19 janvier 2018

False output in if/else [duplicate]

This question already has an answer here:

Hello I am new to Java and in my hello world programm I tried this, and get a flase output with this code:

package all;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class helloworld {

    public static String button = "FHD";

    public static void main(String[] args) {
        JButton buttonFHD = new JButton("FullHD");
        JButton buttonHD = new JButton("HD");
        JFrame frame = new JFrame();     
        frame.setTitle("Mein JDialog Beispiel");        
        frame.setSize(200,200);     
        frame.setVisible(true);
        frame.setResizable(false);       
        JPanel panel = new JPanel();        
        panel.add(buttonFHD);
        panel.add(buttonHD);
        frame.add(panel);
        frame.pack();
        buttonFHD.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
              String button = "FHD";
              Window.open();
              System.out.print(button);
            }
        });
        buttonHD.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
               String button = "HD";
               Window.open();
               System.out.print(button);
            }
        });
    }
    }

They print out the right value(HD/FHD), but in this class:

package all; 
import javax.swing.*;

public class Window {
    public static void open() {
        JFrame window = new JFrame();
            int x = 0;
            int y = 0;
        if(helloworld.button == "FHD") {
            x = 1920;
            y = 1080;
        } else if(helloworld.button == "HD") {
            x = 1280;
            y = 720;
        } else {
            JDialog warning = new JDialog();
            warning.setTitle("ERROR");
            warning.setSize(200, 200);
            JLabel label = new JLabel("ERROR OCCURRED");
            warning.add(label);
            warning.setVisible(true);
        };
        window.setSize(x, y);
        window.setVisible(true);
        window.setResizable(false);
        System.out.println(x + y + helloworld.button);
    }
}

I get every time a Full HD window, but the value is the value i had choosen. For example: I choose HD, get a FHD window but it tells me that helloworld.button is equal to HD.

Aucun commentaire:

Enregistrer un commentaire