【问题标题】:JFrame title won't align properlyJFrame 标题无法正确对齐
【发布时间】:2012-11-26 19:04:27
【问题描述】:

我有一个简单的 JFrame 窗口,它的标题一直向右浮动(虽然是用英文写的)。 住在中东但使用英文版的 Windows 7。 正确对齐标题​​文本和左端的正确方法是什么 标题栏?

imports...

public class MainWindow extends JFrame {

    private JPanel contentPane;
    private final String arr[] = {"1","2","3","4","5","6","7","8","9","10"};

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainWindow frame = new MainWindow("The Simulator");

                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainWindow(String title) {
        super(title);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 582, 435);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        JButton btnRandom = new JButton("Generate new random DCSP");
        contentPane.add(btnRandom);

        JButton btnManual = new JButton("Generate new manual DCSP");

        contentPane.add(btnManual);
    }

}

没有帮助

frame.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

frame.applyComponentOrientation(ComponentOrientation.getOrientation(Locale.US));

【问题讨论】:

  • The Simulator 在这台机器上出现左对齐,使用显示的代码(带导入)。好像是 Locale 依赖?
  • 这个L&F solution 可以帮助你。

标签: java swing user-interface alignment jframe


【解决方案1】:

标题文本对齐的方向,实际上取决于它运行的操作系统。我知道在 Windows 8 上的计算机上,文本在中间对齐,在 osx 中相同,但在 Windows 7 中它在右侧。 This solution 虽然是一种解决方法,但我不知道它是否适用于所有操作系统。

【讨论】:

    【解决方案2】:

    如果我没有误解这个问题。您想在不同的位置对齐标题文本。例如左、中还是右?

    我认为标题栏的位置是由操作系统设置的。新的 windows 8 将所有标题设置在标题栏的中心,而 windows 7 将其设置在左侧。

    我认为解决这个问题的方法是查看标题占用的空间并通过添加空格来操作它。

    我会尝试将我的标题设置为一堆“AAAAAA”,看看需要多少“AAAAAA”才能填满整个标题栏,

    例如,如果我的标题栏需要 100 个 A,而我的宽度是 100(减去图标和按钮),那么我知道每个字母占用 100/100=1 个空间。知道了,假设我想将标题设置为 Hi" 并将其对齐到最右边的角,我可以在 "Hi" 前面添加 98 个空格

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 2017-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多