为什么我的jar文件GUI可以在我的计算机上完美显示,而不能在另一台计算机上完美显示?

Nandan A :

我使用Java开发了基于GUI的应用程序。我曾经GridBagLayout建立过UI。当我jar在系统上运行文件时,UI会根据需要看起来很完美。(请参见下图)

在此处输入图片说明

但是,当我在其他计算机上运行相同的jar文件时,UI如下所示,

  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问题?

报警图像:

在此处输入图片说明

阿布拉:

您发布的代码不是最小的,可复制的示例我假设您将图像设置为TestPane的背景,但是缺少执行该操作的代码。您发布的警报图像为HUGE我将其缩小为原始大小的30%。之后,您只需要调用setPreferredSize()class的构造函数即可TestPane对我来说,宽度450和高度260可以很好地工作,即

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

这就是我的计算机上的样子。

报警

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Apache poi Java:在另一台计算机上运行JAR文件时,图像未显示在文档中

在另一台计算机上运行

为什么timedelta命令会在另一台计算机上创建错误?

在另一台计算机上运行我的C#exe?

在另一台计算机上运行Jar文件,出现NoClassDefFound错误

为什么从另一台计算机上无法使用GRPC?

VB6“内存不足”可以在一台计算机上编译,但不能在另一台计算机上编译

为什么GL.EnableVertexArrayAttrib在计算机上崩溃而不在另一台计算机上崩溃?

为什么django sqlite3数据库不能在一台计算机上与另一台计算机上相同?

为什么我可以从一台计算机克隆,但不能从另一台计算机克隆?

如何在另一台计算机上显示xclock?

为什么C ++代码在一台计算机上的运行时间明显长于另一台计算机?

我如何流式传输在一台计算机上运行的游戏以在另一台计算机上玩?

无法在一台计算机上访问网站,但可以在另一台计算机上访问

我可以将Time Machine备份中的文件还原到另一台计算机上吗?

ProcessStartInfo可在一台计算机上运行,但不能在另一台计算机上运行

我可以在一台计算机上进行编译,而在另一台计算机上进行“安装”吗?

为什么一台计算机上的带有子项的GPG解密失败,而另一台计算机上却失败?

如何在另一台计算机上使用我的项目

要使.NET应用程序在另一台计算机上工作,我需要什么?

为什么我不能在一台计算机上同时使用两个或多个键盘/鼠标?

为什么图像适合我的个人计算机,但在另一台计算机上却尺寸不一样?HTML / CSS

Vagrantfile挂在一台计算机上,但未挂在另一台计算机上

我无法在另一台Ubuntu计算机上访问文件

为什么该程序在我的计算机上可以正常工作,但在另一台计算机上导致溢出?

如何备份我的整个系统并在另一台计算机上还原?

Zend应用程序可以在一台计算机上运行而不能在另一台计算机上运行的原因可能是什么?

我可以使用在另一台计算机上创建的 Windows 10 恢复 USB 吗?

当 ssh 对两台计算机都有效时,为什么 rsync 可以完美地在一台计算机上运行,但不能在另一台计算机上运行