0

我使用 Java 开发了一个基于 GUI 的应用程序。我曾经GridBagLayout构建UI。当我在我的系统上运行jar文件时,UI 看起来很完美,正如我所需要的那样。(请看下图)

在此处输入图像描述

但是当我在其他机器上运行相同的 jar 文件时,用户界面如下所示,

  1. Jtextfield变得像 [] 这样并且不允许用户输入输入。
  2. 窗口大小已更改。您可以看到框架上的标题被截断。

在此处输入图像描述

下面是我在测试窗格中使用的代码。

        public TestPane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        try {
            URL url = getClass().getResource("alarm5.jpg");
            image = ImageIO.read(url);
        } catch (IOException ex) {
            System.out.println("Error: "+ex);
        }

        Font myFont = new Font("Calibri", Font.PLAIN | Font.BOLD, 16);
        Font myFont2 = new Font("Calibri", Font.PLAIN | Font.BOLD, 14);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        selectall = new JCheckBox("Select all");
        selectall.setForeground(Color.WHITE);
        selectall.setOpaque(false);
        selectall.setFont(myFont);
        selectall.addActionListener(this);
        add(selectall, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        monday = new JCheckBox("Monday");
        monday.setForeground(Color.WHITE);
        monday.setOpaque(false);
        monday.setFont(myFont2);
        monday.addActionListener(this);
        add(monday, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        tuesday = new JCheckBox("Tuesday");
        tuesday.setForeground(Color.WHITE);
        tuesday.setOpaque(false);
        tuesday.addActionListener(this);
        tuesday.setFont(myFont2);
        add(tuesday, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        wednesday = new JCheckBox("Wednesday");
        wednesday.setForeground(Color.WHITE);
        wednesday.setOpaque(false);
        wednesday.addActionListener(this);
        wednesday.setFont(myFont2);
        add(wednesday, gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.WEST;
        thursday = new JCheckBox("Thursday");
        thursday.setForeground(Color.WHITE);
        thursday.setOpaque(false);
        thursday.addActionListener(this);
        thursday.setFont(myFont2);
        add(thursday, gbc);

        gbc.gridx = 0;
        gbc.gridy = 5;
        friday = new JCheckBox("Friday");
        friday.setForeground(Color.WHITE);
        friday.setOpaque(false);
        friday.addActionListener(this);
        friday.setFont(myFont2);
        add(friday, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        saturday = new JCheckBox("Saturday");
        saturday.setForeground(Color.WHITE);
        saturday.setOpaque(false);
        saturday.addActionListener(this);
        saturday.setFont(myFont2);
        add(saturday, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        sunday = new JCheckBox("Sunday");
        sunday.setForeground(Color.WHITE);
        sunday.setOpaque(false);
        sunday.addActionListener(this);
        sunday.setFont(myFont2);
        add(sunday, gbc);

        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 2;
        gbc.insets = new Insets(10, 0, 0, 0);
        JLabel env = new JLabel("At what time, should i remind you? ");
        env.setForeground(Color.WHITE);
        env.setFont(myFont);
        add(env, gbc);      

        gbc.gridx = 3;
        gbc.gridy = 7;
        gbc.insets = new Insets(10, 0, 10, 0);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        final JTextField tm = new JTextField(5);
        tm.setText("21:00:00");
        tm.setToolTipText("Input should be in HH:MM:SS format and time format should be 24 hours.");
        add(tm, gbc);

        gbc.gridx = 1;
        gbc.gridy = 8;
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridwidth = 2;
        Set = new JButton(" Set ");
        Set.setFocusable(false);
        add(Set, gbc);
        }

下面是我用于框架的代码。

                frame2 = new JFrame("Select days and time");
                frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame2.setLayout(new BorderLayout());
                frame2.add(new TestPane());
                frame2.pack();
                frame2.setResizable(false);
                frame2.setLocationRelativeTo(null);
                frame2.setVisible(true);
                frame2.setAlwaysOnTop(true);

两个系统具有相同的分辨率设置。你能帮帮我吗?我怎样才能在任何系统上让它看起来一样?我该如何解决这个JTextfield问题?

报警图像:

在此处输入图像描述

4

1 回答 1

2

您发布的代码不是最小的、可重现的示例。我假设您将图像设置为TestPane班级的背景,但缺少执行此操作的代码。您发布的警报图像是巨大的。我将它缩小到原始大小的 30%。之后,您只需要调用setPreferredSize()class 的构造函数TestPane。对我来说,450 的宽度和 260 的高度效果很好,即

public TestPane() {
    setPreferredSize(new Dimension(450, 260);
    // Rest of your code unchanged.
}

这就是它在我电脑上的样子。

警报

于 2020-06-08T18:42:15.807 回答