I was playing around with JFrame today and had trouble getting a JButton component to display text properly. The text displayed in JButton is cut off at the end. I tried resizing the JButton component in order to make sure that the text could fit, but the same problem occurred. The problem looks like this:
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 the other 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 might be a problem with my project's setup, so if anyone needs me to post it I can do so.
Remove the statement
button.setPreferredSize(new Dimension(200, 25));
which is being used by the panel's layout manager to constrain the button width