基本上,问题是当我点击或将鼠标悬停在JComboBox上时,我的程序的外观会出现问题.
以下是一段时间点击其中一个JComboBox'es后发生的事情:
我正在研究类似的问题,在这个页面,有人建议不要使用绝对定位; 但是我没有在我的计划中这样做,所以我没有想法.
下面是我在那里构建我的程序布局和组合框,它出现在我的类的构造函数,这是我从推出我的代码的部分main
使用:EventQueue.invokeLater(() -> new DownloadCalculator());
.
<编辑:代码替换为可运行的示例.请参阅下面的更新#1.>
更新1: 这是一个可运行的演示来说明问题.一段时间摆弄它们后,组合框会出现完全相同的问题.仅包含重现毛刺所需的代码,执行主要计算的功能部分已被删除,因为它与问题无关:
import java.awt.*;
import javax.swing.*;
public class RunnableExample
{
private final JFrame mainframe;
private JTextField downloadSize, downloadSpeed, answerBox;
private JComboBox<String> byteTypeSize, byteTypeSpeed, answerTimeUnit;
public RunnableExample()
{
mainframe = new JFrame("Download Calculator");
mainframe.setLayout(new BoxLayout(mainframe.getContentPane(), BoxLayout.Y_AXIS));
mainframe.setSize(new Dimension(600, 300));
mainframe.setResizable(false);
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setLocationRelativeTo(null);
initLook();
mainframe.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(() -> new RunnableExample());
}
private void calculateDownloadTime(String size, int sizeUnitType, String speed,
int speedUnitType)
{
return; // program's core logic goes here
}
private void initLook()
{
JPanel subLine1 = new JPanel(), subLine2 = new JPanel(), subLine3 = new JPanel();
JLabel question1 = new JLabel("File size to be downloaded:");
downloadSize = new JTextField(10);
byteTypeSize = new JComboBox<>(new String[]{"Bytes", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes"});
byteTypeSize.setSelectedIndex(3);
JLabel question2 = new JLabel("Average download speed:");
downloadSpeed = new JTextField(10);
byteTypeSpeed = new JComboBox<>(new String[]{"Bytes / s", "Kilobytes / s", "Megabytes / s", "Gigabytes / s", "Terabytes / s"});
byteTypeSpeed.setSelectedIndex(1);
JButton buttonCalc = new JButton("Calculate");
answerTimeUnit = new JComboBox<>(new String[]{"Seconds", "Minutes", "Hours", "Days"});
answerTimeUnit.setSelectedIndex(2);
buttonCalc.addActionListener((e) -> new Thread(() -> calculateDownloadTime(downloadSize.getText(), byteTypeSize.getSelectedIndex(), downloadSpeed.getText(), byteTypeSpeed.getSelectedIndex())).start());
answerBox = new JTextField("Hit \"Calculate\" button to see answer here.", 25);
answerBox.setEnabled(true);
subLine1.setLayout(new FlowLayout(FlowLayout.CENTER));
subLine2.setLayout(new FlowLayout(FlowLayout.CENTER));
subLine3.setLayout(new FlowLayout(FlowLayout.CENTER));
subLine1.add(question1);
subLine1.add(downloadSize);
subLine1.add(byteTypeSize);
subLine2.add(question2);
subLine2.add(downloadSpeed);
subLine2.add(byteTypeSpeed);
subLine3.add(answerBox);
subLine3.add(answerTimeUnit);
subLine3.add(buttonCalc);
mainframe.getContentPane().add(subLine1);
mainframe.getContentPane().add(subLine2);
mainframe.getContentPane().add(subLine3);
mainframe.pack();
}
}
Run Code Online (Sandbox Code Playgroud)
更新2: 只要我不点击/更改任何组合框,看起来图形工作正常.一旦我点击任何一个组合框,然后将鼠标移动到我单击的组合框区域之外,它就会开始绘制那些奇怪的glitched图形.根据我收到的反馈,似乎这个问题可能是特定于平台的.
更新3: 我想我会分享我最终发现的东西,所以其他有类似问题的人也可以尝试:重新安装Java并重新启动后,问题似乎已经消失了.感谢所有建议这个和所有其他建议的人!