Jump to content

[Beginner] Java/GUI help


Recommended Posts

Posted

Hey :) I am trying to learn how to make a GUI from expLvl's Tutorial :)

I can't get the JButton to show when i run the main method, why is this?

I am fairly new to java, and am in process of learning it. So sry, if it is apparent haha. Any help/tips appreciated :D

Thanks!

package gui;

import data.TreeInfo;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class GUIDialog extends JDialog {

    private final JDialog mainDialog;
    
    private final JPanel mainPanel;

    private final JPanel selectTreeTypePanel;

    private final JLabel treeTypeLabel;

    private final JButton startButton;

    private final JComboBox<TreeInfo> treeTypeComboBox;

    public GUIDialog() {

        mainDialog = new JDialog();

        mainDialog.setTitle("Tasks Woodcutter");
        mainDialog.setModal(true);
        mainDialog.setModalityType(JDialog.ModalityType.APPLICATION_MODAL);

        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
    
        mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));


        selectTreeTypePanel = new JPanel();
        selectTreeTypePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  
        treeTypeLabel = new JLabel("Select Tree : ");
        selectTreeTypePanel.add(treeTypeLabel);

        treeTypeComboBox = new JComboBox<>(TreeInfo.values());
        selectTreeTypePanel.add(treeTypeComboBox);

        mainPanel.add(selectTreeTypePanel);

        startButton = new JButton("Start");
        startButton.addActionListener(e -> {
            started = true;
            close();
        });

        mainPanel.add(startButton);


        mainDialog.getContentPane().add(mainPanel);
        mainDialog.getContentPane().add(selectTreeTypePanel);

        mainDialog.pack();
        mainDialog.setLocationRelativeTo(null);
    }

    public void open() {

        mainDialog.setVisible(true);
    }

    public void close() {

        mainDialog.setVisible(false);
        mainDialog.dispose();
    }

    public static void main(String... args) {

        new GUIDialog().open();
    }
}

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...