Jump to content

Jframe class not working properly


erenjwz

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 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

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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