JFrame text components not displaying as expected

I was playing around with JFrame today and couldn't get the JButton component to display the text correctly. The text displayed in the JButton is truncated at the end. I tried to resize the JButton component to make sure the text could fit, but the same problem occurred. The problem looks like this:

enter image description here

Here is the code:

import javax.swing.JFrame;

public class Launcher {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("Frame");
        jFrame.setSize(400, 400);
        jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
        jFrame.setLocation(400, 400);
        jFrame.add(new Drawable(jFrame));
        jFrame.setVisible(true);
    }
}

      

Here is another class in another .java file.

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Drawable extends JPanel {
    private JButton button;
    private JFrame jFrame;
    public Drawable(JFrame jFrame) {
        this.jFrame = jFrame;
        button = new JButton("This text does not show properly");
        button.setPreferredSize(new Dimension(200, 25));
        button.setLocation(jFrame.getWidth() / 2 - 50, jFrame.getHeight() / 2 - 12);
        this.add(button);
    }
}

      

I understand that this could be a problem with my project setup, so if anyone needs me to post, I can do it.

+3


source to share


1 answer


Delete approval

button.setPreferredSize(new Dimension(200, 25));

      



which is used by the pane's layout manager to constrain the width of the button

+2


source







All Articles