Jump to content

[Beginner] Java/GUI help


t0r3

Recommended Posts

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();
    }
}
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...