简体   繁体   English

JFrame文本组件未正确显示

[英]JFrame text components not showing up properly

I was playing around with JFrame today and had trouble getting a JButton component to display text properly. 我今天正在玩JFrame并且无法让JButton组件正确显示文本。 The text displayed in JButton is cut off at the end. JButton中显示的文本最后被截断。 I tried resizing the JButton component in order to make sure that the text could fit, but the same problem occurred. 我尝试调整JButton组件的大小以确保文本适合,但发生了同样的问题。 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. 这是另一个.java文件中的另一个类。

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 面板的布局管理器使用它来约束按钮宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM