gpt4 book ai didi

java - Swing 形式被卡住

转载 作者:行者123 更新时间:2023-12-01 13:16:01 27 4
gpt4 key购买 nike

根据http://docs.oracle.com/javase/7/docs/technotes/guides/2d/flags.html#opengl
我将系统属性设置如下,以便 JVM 使用 GPU opengl 硬件加速(如果可用)。
没有发生崩溃,并且详细消息表明管道已激活为屏幕 0 上的默认配置启用了 OpenGL 管道

        public static void main(String args[]) {
System.setProperty("sun.java2d.opengl", "True");
try {
UIManager.setLookAndFeel(new WindowsClassicLookAndFeel());
} catch (Exception ex) {}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new RootForm().setVisible(true);
}
});
//no difference
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
// new RootForm().setVisible(true);
// }
// });
//no difference
// new Thread(new Runnable() {
// @Override
// public void run() {
// new RootForm().setVisible(true);
// }
// }).start();
}

但是挥杆形态被卡住了!甚至 repaint() 调用也不起作用。
会出现什么问题?
平台:Win7X64,GPU:AMD HD5870(更新驱动)。
提前致谢。
编辑
我尝试了几次并且成功了。并且没有新问题。它随机工作。很奇怪。只要需要就可以工作。

最佳答案

对我来说效果很好...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestOpenGL {

public static void main(String args[]) {
System.setProperty("sun.java2d.opengl", "True");
new TestOpenGL();
}

public TestOpenGL() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private int yPos = 0;
private int yDelta = 2;
private JLabel label;

public TestPane() {
setLayout(new GridBagLayout());
label = new JLabel("Bouncy, Bouncy...");
add(label);
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
yPos += yDelta;
if (yPos > getHeight()) {
yPos = getHeight();
yDelta *= -1;
} else if (yPos < 0) {
yPos = 0;
yDelta *= -1;
}
label.setText("Bouncy, Bouncy...@ " + yPos);
repaint();
}
});
timer.start();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.GREEN);
int x = (getWidth() - 2) / 2;
g2d.drawOval(x, yPos - 2, 4, 4);
g2d.dispose();
}
}
}

关于java - Swing 形式被卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22465401/

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