1

I am writing some Java code that allows the user to see a frame with JLabel, JTextField and JButton.

I want the JLabel to be called "Count" and I have a problem with FlowLayout. I want the interface to look like this:

enter image description here

Instead, I have this:

enter image description here

This is my code:

package modul1_Interfate_Grafice;

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

import javax.swing.*;

public class Exercitiu04 implements ActionListener {
private JFrame frame;
private JLabel labelCount;
private JTextField tfCount;
private JButton buttonCount;
private int count = 0;

    public void go() {
        frame = new JFrame("Java Counter");
        labelCount = new JLabel("Counter");
        labelCount.setLayout(new FlowLayout());
        frame.getContentPane().add(BorderLayout.CENTER, labelCount);

        tfCount = new JTextField(count + " ", 10);
        tfCount.setEditable(false);
        labelCount.add(tfCount);

        buttonCount = new JButton("Count");
        labelCount.add(buttonCount);
        buttonCount.addActionListener(this);

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(350, 150);
        frame.setLocation(400, 200);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        count++;
        tfCount.setText(count + "");
    }

    public static void main(String[] args) {
        Exercitiu04 a  = new Exercitiu04();
        a.go();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    `frame.setSize(350, 150);` should instead be `frame.pack();` .. – Andrew Thompson Jul 31 '16 at 16:56
  • 2
    `frame.setLocation(400, 200)` should be `frame.setLocationByPlatform(true)` as stated by [this question and answer](http://stackoverflow.com/questions/7143287/how-to-best-position-swing-guis). It should also be before `frame.setVisible(true)`. – MasterBlaster Jul 31 '16 at 17:12
  • 2
    You should always put swing methods in the Event Dispatch Thread. Take a look at [Concurrency in Swing - The Java™ Tutorials](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html). – MasterBlaster Jul 31 '16 at 17:20

3 Answers3

0

Solve it.

Instead of labelCount.setLayout(new FlowLayout());` i should have had

frame.setLayout(new FlowLayout());
0

From description of JLabel class,

JLabel is:

A display area for a short text string or an image, or both.

But here: labelCount.add(tfCount) and here labelCount.add(buttonCount) you're trying to put a textfield and a button into a label. In this case, positions of button and textfield are controlled by FlowLayout but position of the text in the label is not.

Instead of this, you should put all of your elements in common JPanel, like this:

    ...
    frame = new JFrame("Java Counter");
    frame.setLayout(new BorderLayout());

    JPanel wrapper = new JPanel(); // JPanel has FlowLayout by default

    labelCount = new JLabel("Counter");
    labelCount.setLayout(new FlowLayout());
    wrapper.add(labelCount);

    tfCount = new JTextField(count + " ", 10);
    tfCount.setEditable(false);
    wrapper.add(tfCount);

    buttonCount = new JButton("Count");
    buttonCount.addActionListener(this);
    wrapper.add(buttonCount);

    frame.add(BorderLayout.CENTER, wrapper);
    ...

And, like MasterBlaster said, you should put swing methods in EDT.

Community
  • 1
  • 1
0

There are only two things you should know about FlowLayout:

a) It is a default layout manager of the JPanel component b) It is good for nothing.

This trivial layout cannot be achieved with FlowLayout. When doing layouts in Swing, you should familiarize yourself with some powerful layout managers. I recommend MigLayout and GroupLayout.

package com.zetcode;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;

/*
Simple UI with a MigLayout manager.
Author Jan Bodnar
Website zetcode.com
*/

public class MigLayoutCounterEx extends JFrame {

    public MigLayoutCounterEx() {

        initUI();
    }

    private void initUI() {

        JLabel lbl = new JLabel("Counter");
        JTextField field = new JTextField(10);
        JButton btn = new JButton("Count");

        createLayout(lbl, field, btn);

        setTitle("Java Counter");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        setLayout(new MigLayout());

        add(arg[0]);
        add(arg[1]);
        add(arg[2]);

        pack();
    }    

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutCounterEx ex = new MigLayoutCounterEx();
            ex.setVisible(true);
        });        
    }
}

The example is trivial. You just put the three components into the cells.

Screenshot:

enter image description here

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77