2

I am trying to implement a GUI in Java Swing; but I am stuck on the button size, which I am trying to make smaller. I have tried to use setSize, setPrefferedSize and setmaximumSize, but nothing worked. Any ideas?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.*;
import javax.swing.border.Border;

public class WorkStationGUI extends JFrame implements ActionListener {

    JButton Worker1Process1;
    JButton Worker1Process2;
    JButton StopWorking;
    //   JScrollPane scrollList;
    JTextArea OrderList;
    JTextArea ProductList;
    JTextField orders;
    JTextField products;
    JTextField Worker1;
    JTextField Worker2;
    private HashMap<String, Order> OrdersList;
    private HashMap<String, Product> ProductsList;

    public WorkStationGUI(HashMap<String, Order> orders, HashMap<String, Product> Products) {
        this.OrdersList = orders;
        this.ProductsList = Products;
        setTitle("Work Station");
        setupEastandWest();
        setupSouthPanel();
        pack();
        setVisible(true);
    }

    private JLabel createOneLabel(String s, Color c) {
        Font f = new Font(Font.SANS_SERIF, Font.BOLD, 18);
        JLabel label = new JLabel(s, JLabel.CENTER);
        label.setFont(f);
        label.setBackground(c);
        label.setOpaque(true);
        return label;
    }

    private void setupEastandWest() {

        // search panel contains label, text field and button
        JPanel Panel = new JPanel();
        Worker1Process1 = new JButton("Worker 1");

        Worker1Process1.setPreferredSize(new Dimension(10, 10));

        Panel.add(Worker1Process1);
        Worker1 = new JTextField(20);
        Worker1Process1.setPreferredSize(new Dimension(90, 90));
        Worker1.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
        Worker1.setEditable(false);
        Panel.setLayout(new GridLayout(2, 1));
        Panel.add(Worker1);
        JPanel Panel2 = new JPanel();
        Worker1Process2 = new JButton("Worker 2");
        Panel2.add(Worker1Process2);
        Worker2 = new JTextField(20);
        Worker2.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
        Worker2.setEditable(false);
        Panel2.setLayout(new GridLayout(2, 1));
        Panel2.add(Worker2);            

        Panel.setLayout(new GridLayout(2, 2));
        this.add(Panel, BorderLayout.EAST);
        this.add(Panel2, BorderLayout.WEST);
        // this.add(Panel2, BorderLayout.SOUTH);
    }

    private void setupSouthPanel() {
        JPanel Panel = new JPanel();

        orders = new JTextField(1);
        orders.setBackground(Color.LIGHT_GRAY);
        Panel.add(orders);
        JPanel Panel2 = new JPanel();
        products = new JTextField(1);
        Panel.add(products);
        products.setBackground(Color.LIGHT_GRAY);
        Panel.setLayout(new GridLayout(2, 2));
        this.add(Panel, BorderLayout.CENTER);
        this.add(Panel2, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e) {
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
dori naji
  • 980
  • 1
  • 16
  • 41

1 Answers1

3

You are using GridLayout which will cause the component to take up all container space, relative to the grid. It's not really the best choice for buttons. You might try using a FlowLayout, or GridBagLayout instead.

Also, you are setting the layout manager AFTER you are setting the JButton on the panel.

btantlinger
  • 803
  • 8
  • 11