Dan
Dan

Reputation: 1

Simple Java GUI Program

My JPanel and JTextField are for some reason not appearing. The programs only task is to add a number to the counter every time the button is clicked. No compiling errors nor console issues. Simply not showing up??

import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 

public class Swing  
{
public static void main(String[] args) 
{
   SwingUtilities.invokeLater(new Runnable()
    {
        public void run() 
        {
            final JFrame mainFrame = new JFrame ("Counter (Program 1/2)"); 
            mainFrame.setVisible(true);
            mainFrame.setSize(400, 200);
            mainFrame.setLayout(new BorderLayout());
            mainFrame.setLocationRelativeTo(null); 
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


            JButton countButton = new JButton("Count up");
            mainFrame.add(countButton, BorderLayout.SOUTH);

            countButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    JTextField clicks = new JTextField(BorderLayout.CENTER);
                    JPanel firstPanel = new JPanel();
                    mainFrame.add(firstPanel);
                    mainFrame.add(clicks, BorderLayout.NORTH);      
                    int counter = 1;
                    counter++;
                    String textField = String.valueOf(counter);
                    clicks.setText(textField);

                }
            });     
        }
    });     
}

}

Upvotes: 0

Views: 82

Answers (2)

rdonuk
rdonuk

Reputation: 3956

Your mistakes:

  1. Add clicks and firstpanel to the frame in run method, not in the actionPerformed
  2. BorderLayout.CENTER should be pass as a parameter to add method, not to the text field constructor.
  3. Define count as static. This is not the best way but in your situation it is the easiest way.
  4. Call mainFrame.setVisible(true); after you added all your components.

Here is the working code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Swing {

    public static int counter = 1;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final JFrame mainFrame = new JFrame("Counter (Program 1/2)");

                mainFrame.setSize(400, 200);
                mainFrame.setLayout(new BorderLayout());
                mainFrame.setLocationRelativeTo(null);
                mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JButton countButton = new JButton("Count up");
                mainFrame.add(countButton, BorderLayout.SOUTH);

                final JTextField clicks = new JTextField(String.valueOf(counter));
                JPanel firstPanel = new JPanel();
                mainFrame.add(firstPanel, BorderLayout.CENTER);
                mainFrame.add(clicks, BorderLayout.NORTH);

                countButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        counter++;
                        String textField = String.valueOf(counter);
                        clicks.setText(textField);

                    }
                });

                mainFrame.setVisible(true);
            }
        });
    }
}

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Don't add the JTextField and JPanel inside the ActionListener. This makes no sense since you'll be re-adding new components each time the button is pressed. Instead add them on GUI creation, before calling setVisible(true) on the GUI. Then update the text field's text in the ActionListener.

As a side recommendation: try to make your class more object oriented by giving it fields, a constructor, and getting most all of that code out of the static main method.

Upvotes: 2

Related Questions