Jump to content

Jframe class not working properly


Recommended Posts

Posted

I have 2 classes: one "main.java" and the other "Gui.java".

 

i want to start running the code from Gui when main.java starts but I'm getting errors (it's probably one line of code that's wrong).

Tried everything also is this my first JFrame code if somone could advise me on how i could approach it better;

 

(main.java)

 

public class main extends Script {
    
    private Gui Kies = new Gui();

    ...
    Kies.getKeuze();

 

 

(Jfarme.java)

 

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

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class Gui extends JFrame implements ActionListener {
    
    private static final long serialVersionUID = 1L;
    private String[] fish = {"Trout", "Tuna", "Lobster"};
    private String Keuze;
    
    private JComboBox<String> Dropdown = new JComboBox<String>();

    public static void main(String[] args) {
        new Gui().setVisible(true);
    }
    
    public Gui() {
        super("Hylian selector");
        setSize(380, 250);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        
        JButton button = new JButton("Select");
        button.addActionListener(this);
        
        for(int i = 0; i < 3; i++){
            Dropdown.addItem(fish);
        }
        add(button);
        add(Dropdown);
    }
    
    void Derp(){
        System.out.println("Derp");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        this.Keuze = (String) (Dropdown.getSelectedItem());
        System.out.println(this.Keuze);
        dispose();
    }

    public String getKeuze() {
        return this.Keuze;
    }
}
 

Posted

It's better if you create the private field and initialI've it in the onstart. Then do kies.setVisible(true)

Also remove that main void method in the gui class. Since you have to do it in the main class.

Giving us the error code would of help me explain what's going wrong with the script.

J combo box has a parameter of an array and or enums.

Meaning you can do something like new JComboxBox <string>(array);

Also just use a method that grabs the value of the JComboxBox instead setting the value in a sepreate private field.

If you are only using one option I suggest you use a jOptionPane.

Posted

 this.Keuze = (String) (Dropdown.getSelectedItem());

        System.out.println(this.Keuze);

        dispose();

 

Keuze = Dropdown.getSelectedItem().toString();

dispose()

 

also.. instead of EXIT_ON_CLOSE use DISPOSE_ON_CLOSE

 

Typecasting the selected item should work fine

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...