我使用的是带 Aero 的 Windows 7,并且有一个非常快的显卡 (Radeon 6870),用于游戏。
在调整用 java 编写的非常简单的程序大小时,我遇到了一些问题。例如,这个程序完全不执行任何操作。它没有动作监听器,没有循环。它只是一个带有按钮的 GUI 界面。
关闭 OpenGL 加速时调整大小:
[View fullscreen] https://i.stack.imgur.com/9y2Xl.png
调整组件大小大约需要一秒钟。对我来说,这是非常引人注目的。
使用 OpenGL 加速调整大小:
我尝试启用OpenGl加速来解决这个问题。我编译了 JAR 并
运行它java -Dsun.java2d.opengl=true -jar C:\Test.jar
。结果是稍微少一点窗户周围有黑色区域,但闪烁程度更大。事实上,闪烁在上面的屏幕截图中显示为灰色。
其他软件中是否也存在该问题?
不会。Eclipse、Netbeans、Chrome 和其他应用程序已经过测试。没有人有这个问题。因此我必须得出结论,代码一定存在问题。许多人运行过这段代码并表示“没有问题”。如果您要测试它,请确保将窗口大小从屏幕的最小尺寸调整到最大尺寸,同时以圆周运动移动鼠标。
Code:
import java.awt.*;
import javax.swing.*;
public class JFrameWithButtonsTest {
private int iScreen = 25;
private int iLocation = 10;
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
public JFrameWithButtonsTest() {
JPanel northButtonPanel = new JPanel();
northButtonPanel.setLayout(new GridLayout(2,2));
northButtonPanel.add(new JButton(" I do nothing"));
northButtonPanel.add(new JButton(" I do nothing"));
northButtonPanel.add(new JButton(" I do nothing"));
northButtonPanel.add(new JButton(" I do nothing"));
contentPane.add(northButtonPanel, BorderLayout.NORTH);
JPanel southButtonPanel = new JPanel();
southButtonPanel.setLayout(new GridLayout(2,2));
southButtonPanel.add(new JButton(" I do nothing"));
southButtonPanel.add(new JButton(" I do nothing"));
southButtonPanel.add(new JButton(" I do nothing"));
southButtonPanel.add(new JButton(" I do nothing"));
contentPane.add(southButtonPanel, BorderLayout.SOUTH);
JPanel eastButtonPanel = new JPanel();
eastButtonPanel.setLayout(new GridLayout(2,2));
eastButtonPanel.add(new JButton(" I do nothing"));
eastButtonPanel.add(new JButton(" I do nothing"));
eastButtonPanel.add(new JButton(" I do nothing"));
eastButtonPanel.add(new JButton(" I do nothing"));
contentPane.add(eastButtonPanel, BorderLayout.EAST);
boolean packFrame = false;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
frameSize.height = (int) (iScreen * screenSize.height / 100);
frameSize.width = (int) (iScreen * screenSize.width / 100);
frame.setSize(frameSize);
frame.setLocation((screenSize.width - frameSize.width) / iLocation,
(screenSize.height - frameSize.height) / iLocation);
if (packFrame) {
frame.pack();
packFrame = true;
} else {
frame.validate();
}
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFrameWithButtonsTest();
}
});
}
}
请注意,如果没有此行,问题仍然存在:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());