简体   繁体   中英

How am I to run my GUI? Or to be more exact, what do I put in my main?

public class StatsGUI extends JFrame implements ActionListener {


    JLabel label;
    JLabel label2;
    JTextField input;
    JTextField output;
    JButton getButton;
    JButton exitButton;

    public StatsGUI()
    {
        JPanel panel = new JPanel();
        label = new JLabel("Enter number");
        panel.add(label);
        input = new JTextField(10);
        input.addActionListener(this);
        panel.add(input);

        label2 = new JLabel("Statistics");
        output = new JTextField(10);
        output.setEditable(false);
        panel.add(output);

        getButton = new JButton("Go");
        getButton.addActionListener(this);
        panel.add(getButton);

    exitButton = new JButton("Exit");
    exitButton.addActionListener(this);
    panel.add(exitButton);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == exitButton)
        {
            System.exit(0);
        }
        else
        {
            String text = input.getText();
            output.setText(text + "COUNTER");
        }


    }

    public static void main(String[] args)
    {



    }

This is my simple GUI program. I have placed all buttons and other gadgets within the constructor. However, I am not sure what I should be putting inside my main in order to get my GUI to actually show up. I am sure I am missing something incredibly simple here however I am not sure what. Help would be much appreciated.

You are not that far from getting things to work. Just a few things to know:

  1. Your UI should be started on the Event dispatching thread (EDT)
  2. You actually need to add your panels/components to your frame
  3. You need to pack() your Window/Frame
  4. You need to make it visible
  5. (Design stuff, optional but when you are at it, why not just fix that as well), no need to extend JFrame , so let's just drop that.

So eventually, taking these advices into consideration leads you to something like this:

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class StatsGUI implements ActionListener {

    JLabel label;
    JLabel label2;
    JTextField input;
    JTextField output;
    JButton getButton;
    JButton exitButton;

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == exitButton) {
            System.exit(0);
        } else {
            String text = input.getText();
            output.setText(text + "COUNTER");
        }

    }

    public void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        label = new JLabel("Enter number");
        panel.add(label);
        input = new JTextField(10);
        input.addActionListener(this);
        panel.add(input);

        label2 = new JLabel("Statistics");
        output = new JTextField(10);
        output.setEditable(false);
        panel.add(output);

        getButton = new JButton("Go");
        getButton.addActionListener(this);
        panel.add(getButton);

        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);
        panel.add(exitButton);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new StatsGUI().initUI();
            }
        });
    }
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


class StatsGUI extends JFrame implements ActionListener {


JLabel label;
JLabel label2;
JTextField input;
JTextField output;
JButton getButton;
JButton exitButton;

public StatsGUI()
{
    JPanel panel = new JPanel();
    label = new JLabel("Enter number");
    panel.add(label);
    input = new JTextField(10);
    input.addActionListener(this);
    panel.add(input);

    label2 = new JLabel("Statistics");
    output = new JTextField(10);
    output.setEditable(false);
    panel.add(output);

    getButton = new JButton("Go");
    getButton.addActionListener(this);
    panel.add(getButton);

    exitButton = new JButton("Exit");
    exitButton.addActionListener(this);
    panel.add(exitButton);
    add(panel);
    }

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == exitButton)
    {
        System.exit(0);
    }
    else
    {
        String text = input.getText();
        output.setText(text + "COUNTER");
    }


}

public static void main(String[] args)
{

 StatsGUI s= new StatsGUI();
 s.setVisible(true);
 s.setSize(1000,1000);
}
}

You are not calling a contructor .call costructor like 'StatsGUI s= new StatsGUI();' or 'new StatsGUI();' . 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM