Search code examples
javaswingjlabel

Scaling with Java Swing JLabel doesn't perform well


Currently, I work on a project for my study but I have a problem and I can't neither find a solution nor understand the problem..

I made a responsive calculator with swing in Java, every keys are responsive. However, my display aera (JLabel) won't fill HORIZONTAL my window. If I incrase its size, it's all of the column which change its size ! ><

Can someone help me?

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class CalculatorUI {
static JButton designButton(GridBagConstraints gbc, String text)
{
     JButton button = new JButton(text);
     ImageIcon image;
     Border withoutBorder = new LineBorder(new Color(75, 75, 75), 1, true);
     button.setBorder(withoutBorder);   
     button.setFont(new Font("Gill Sans MT", Font.BOLD, 40));
     button.setBackground(new java.awt.Color(23,23,23));
     gbc.weightx = 0.5;
     gbc.weighty = 0.5;
     gbc.fill = GridBagConstraints.BOTH;
     gbc.gridwidth = 1;
     switch (text) {
     case "sqr":
         button.setText("\u221A");
         button.setForeground(new Color(150, 150, 150));
         break;
     case "sq":
         button.setText("x²");
         button.setForeground(new Color(150, 150, 150));
         break;
     case "1x":
         image = new ImageIcon("C:\\Users\\Emixam23\\Documents\\Workspace\\SimpleCount\\assets\\1x.png");
         button.setText(null);
         button.setIcon(image);
         break;
     case "DEL":
         image = new ImageIcon("C:\\Users\\Emixam23\\Documents\\Workspace\\SimpleCount\\assets\\del.png");
         button.setText(null);
         button.setIcon(image);
         break;
     case "DIV":
         image = new ImageIcon("C:\\Users\\Emixam23\\Documents\\Workspace\\SimpleCount\\assets\\div.png");
         button.setText(null);
         button.setIcon(image);
         break;
     // CS -> Change sign !
     case "CS":
         image = new ImageIcon("C:\\Users\\Emixam23\\Documents\\Workspace\\SimpleCount\\assets\\cs.png");
         button.setText(null);
         button.setIcon(image);
         break;          
     default:
         button.setForeground(new Color(150, 150, 150));
         break;
    }
     return (button);
}

static JLabel designTextField(GridBagConstraints gbc)
{
    JLabel display = new JLabel("---------------");
    Border withoutBorder = new LineBorder(new Color(0, 0, 0), 0, true);
    display.setBorder(withoutBorder);   
    //display.setFont(new Font("Gill Sans MT", Font.BOLD, 40));
    gbc.fill = GridBagConstraints.NORTH;
    gbc.weightx = 0.5;
    gbc.gridx = 0;
    gbc.gridy = 0;
    return (display);
}

public static void makeSimpleGUI(Container pane) {
    JButton button;

    pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    //Add of jlabel to display results
    pane.add(designTextField(gbc));

    String[] keys = {"%", "sqr", "sq", "1x", "CE", "C", "DEL", "DIV", "7", "8", "9", "X", "4", "5", "6", "-", "1", "2", "3", "+", "CS", "0", ".", "="};
    for (int i = 0; i < keys.length; i++)
    {
        button = designButton(gbc, keys[i]);
        gbc.gridx = i % 4;
        gbc.gridy = 1 + (i / 4);
        pane.add(button, gbc);
    }
}

public static void CalculatorGUI() {
    JFrame frame = new JFrame("SimpleCount");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    makeSimpleGUI(frame.getContentPane());
    frame.setMinimumSize(new Dimension(400, 400));
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            CalculatorGUI();
        }
    });
}
}

Solution

  • You add the JLabel to the panel using...

    pane.add(designTextField(gbc));
    

    but this does not apply the GridBagConstraints, which were modified by the method to the label, instead, you should be doing something more like...

    pane.add(designTextField(gbc), gbc);
    

    But, I'd discourage from doing things this way, your methods shouldn't have side effects, it should do one job and a do it well

    static JLabel designTextField()
    {
        JLabel display = new JLabel("---------------");
        Border withoutBorder = new LineBorder(new Color(0, 0, 0), 0, true);
        display.setBorder(withoutBorder);   
        //display.setFont(new Font("Gill Sans MT", Font.BOLD, 40));
        return (display);
    }
    
    public static void makeSimpleGUI(Container pane) {
        JButton button;
    
        pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        pane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 0.5;
        gbc.gridx = 0;
        gbc.gridy = 0;
    
        //Add of jlabel to display results
        pane.add(designTextField(), gbc);
    

    Also GridBagConstraints.NORTH is a anchor attribute, not a fill attribute

    Because the JLabel and buttons are sharing the same space (columns), you need to allow the label to span over multiple columns.

    Add...

    gbc.gridwidth = GridBagConstraints.REMAINDER;
    

    to the constraints

    Calculator

    I used a LineBorder with a red color so I could visually see the layout