gpt4 book ai didi

java - Netbeans 之外奇怪的 GUI/jar 行为

转载 作者:搜寻专家 更新时间:2023-11-01 03:06:34 29 4
gpt4 key购买 nike

我用 Swing 编写了一些小的 Java 程序。当我通过 Netbeans 运行它们时它们工作正常,但是当我在 Netbeans 之外将它们作为 jar 运行时,Swing 元素部分呈现或根本不呈现。

这是一个比较:

通过 Netbeans:

enter image description here

Netbeans 之外:

enter image description here

有些问题是在框架出现时出现的,而另一些是当我将光标移到某个元素上时出现的。主要问题是文本未呈现,按钮文本、边框和背景随机出现和消失。框架的边框不是问题。两个屏幕截图之间的差异仅仅是 Windows 7 中清晰的框架以及我在制作屏幕截图时在程序下打开了哪些窗口的结果。

这是一个简化版的 GUI 代码,去掉了事件处理和程序逻辑:

package alarmclock;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class AlarmClockGUI {

private JFrame frame;
private JButton onOffButton;
private JComboBox<String> alarmList;
private JLabel remainingTime, currentTime,
alarmTime;
private final EventHandler eventHandler; //Handles GUI events
private final boolean time24Mode = false; //Indicates 24H (true) or 12H (false) time

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
AlarmClockGUI acg = new AlarmClockGUI();
}
});
}

public AlarmClockGUI() {
eventHandler = new EventHandler(); //Handles GUI events
createFrameOptions();
}

class EventHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//Code added here
}
}

private void updateTimeDisplay() {
String tempTime = String.format("%tT", new Date());
if (time24Mode) {
currentTime.setText(tempTime);
} else {
currentTime.setText(String.format("%tr", new Date()));
}
}

private void createFrameOptions() {
frame = new JFrame("Alarm Clock");
frame.getContentPane().add(createMainPanel());
createTimer(); //must follow main panel creation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.pack();
frame.setLocationRelativeTo(null); //Centers frame. Must follow pack()
frame.setVisible(true);
}

private JPanel createTimePanel() {
JPanel timePanel = new JPanel(new GridBagLayout());

remainingTime = new JLabel("00:00:00", SwingConstants.CENTER);
remainingTime.setFont(
remainingTime.getFont().deriveFont(36.0f));
remainingTime.setPreferredSize(new Dimension(164, 80));
remainingTime.setOpaque(true);
remainingTime.setBorder(
BorderFactory.createTitledBorder("Remaining Time"));
currentTime = new JLabel(String.format("%tT", new Date()),
SwingConstants.CENTER);
currentTime.setFont(
currentTime.getFont().deriveFont(16.0f));
currentTime.setPreferredSize(new Dimension(164, 40));
currentTime.setOpaque(false);
currentTime.setBorder(
BorderFactory.createTitledBorder("Current Time"));
alarmTime = new JLabel("00:00:00", SwingConstants.CENTER);
alarmTime.setFont(
alarmTime.getFont().deriveFont(16.0f));
alarmTime.setPreferredSize(new Dimension(164, 40));
alarmTime.setOpaque(false);
alarmTime.setBorder(
BorderFactory.createTitledBorder("Alarm Time"));

GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridheight = 2;
c.fill = GridBagConstraints.VERTICAL;
timePanel.add(remainingTime, c);
c.gridx = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.NONE;
timePanel.add(currentTime, c);
c.gridy = 1;
timePanel.add(alarmTime, c);
return timePanel;
}

private JComboBox createAlarmList() {
alarmList = new JComboBox<>();
alarmList.addActionListener(eventHandler);
alarmList.setActionCommand("List");
alarmList.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
return alarmList;
}

private JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel();
onOffButton = createButton("Alarm Off", eventHandler);
onOffButton.setPreferredSize(new Dimension(88, 26));
buttonPanel.add(createButton("New Alarm", eventHandler));
buttonPanel.add(createButton("Edit", eventHandler));
buttonPanel.add(onOffButton); //Not anonymous; button text changes
buttonPanel.add(createButton("Delete", eventHandler));
return buttonPanel;
}

private JButton createButton(String buttonName, ActionListener al) {
JButton b = new JButton(buttonName);
b.setActionCommand(buttonName);
b.addActionListener(al);
return b;
}

private JPanel createMainPanel() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.add(createTimePanel());
mainPanel.add(Box.createRigidArea(new Dimension(10, 10)));
mainPanel.add(createAlarmList());
mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
mainPanel.add(createButtonPanel());
mainPanel.setBorder(
BorderFactory.createEmptyBorder(10, 10, 10, 10));
return mainPanel;
}

private void createTimer() {
Timer timer = new Timer(1000, eventHandler);
timer.setActionCommand("Timer");
timer.setInitialDelay(0);
timer.start();
}
}

我刚刚将我的 JRE 更新到最新版本(问题也发生在这之前),Netbeans 与我的系统的其余部分使用相同的 JRE。

有没有人知道为什么我的程序在 Netbeans 之外呈现不正确?

---更新--------------------

我尝试按照建议设置外观,但这没有帮助。

最佳答案

经过进一步研究,我发现这个问题可能与在 JRE 上或在程序中显式设置某些系统属性有关。具体来说,我能够添加:

    System.setProperty("sun.java2d.noddraw", "true");

到 main 方法,这解决了我的问题。我在程序中明确设置了该属性,这样我就可以通过双击 jar 文件来运行程序。从控制台启动 JVM 时,也可以将此属性设置为标志:

-Dsun.java2d.noddraw=true

事实证明,Netbeans 使用该属性启动 JVM,这解释了为什么它在 Netbeans 中运行良好,但在 Netbeans 之外却不行。

类似的问题引用here , 和 here是甲骨文对属性(property)的说法。显然,该属性可以在 JVM 上显式设置为标志,但我没能在我的机器上运行它,所以我在程序中设置了它。

关于java - Netbeans 之外奇怪的 GUI/jar 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20273441/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com