因此,我在编写的Java程序中有几个JButton,它们显然包含一个值(如按钮上显示的文本)。

当我在Windows中运行该应用程序时,此文本始终适合JButton内-不管我使窗口多么小(我已经为窗口设置了最小大小)。但是,当我在Linux中运行该应用程序时,文本似乎不适合按钮内。

我正在这样定义我的JButton:

public class ButtonPanel extends JPanel {

private JButton undoButton;

/**
 * Button panel constructor.
 */
public ButtonPanel() {
    // Set the layout
    this.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5));

    // Add all buttons and register their listeners
    undoButton = new JButton("Undo");
    undoButton.setPreferredSize(new Dimension(100, 50));
    undoButton.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent e) {
                                        System.out.println("Undo button pressed");
                                        try {
                                            App.performActivity(Activity.UNDO);
                                        } catch (Exception e1) {
                                            e1.printStackTrace();
                                        }
                                    }
                                });
    this.add(undoButton);
}


有人有什么想法吗?

谢谢,

卡勒姆

最佳答案

undoButton.setPreferredSize(new Dimension(100, 50));


摆脱那句话!

不要硬编码组件的首选大小。每个组件将确定其首选大小,布局管理器将使用此信息将组件放置在面板上。

07-24 09:49
查看更多