why does the JTextField go beyond the border of the JFrame?

32 Views Asked by At

Good evening. I'm having a strange problem with the JTextField in my gui for a calculator. in few word the Text field goes beyond the border of the frame (which is 500), the strange thing is that i've set the lenght bound of the text field as the lenght of the frame (0,0, 500, 50).

this is the code:

package calcolatrice;

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

public class Calcolatrice {
 Calcolatrice(){
    JFrame frame = new JFrame("Simple Calculator");
    frame.setSize(500, 500);
    frame.setResizable(false);
    frame.setLayout(null);

    JPanel displayPanel = new JPanel();
    displayPanel.setBackground(Color.darkGray);
    displayPanel.setBounds(0, 0, frame.getWidth(), 50);
    displayPanel.setLayout(null);
    frame.add(displayPanel);

    JTextField display = new JTextField("Prova");
    display.setBounds(0, 0, displayPanel.getWidth(), displayPanel.getHeight());
    display.setHorizontalAlignment(JTextField.RIGHT);
    displayPanel.add(display);

    frame.setVisible(true);
}
}

and this is the following output:

bug

I hope that someone can explain me what is going on with that JTextField and help me solve the problem.

Thanks in advance.

0

There are 0 best solutions below