我有一個簡單的帶有瀏覽按鈕的Java Swing GUI窗體。瀏覽按鈕在點擊時創建一個新的JFileChooser。Java Swing彈出窗口失去焦點並且沒有重新繪製
但是,如果在窗口打開後立即單擊瀏覽,文件選擇器窗口似乎鬆散焦點,顯示它後面的父窗口,但拒絕重新繪製自身。我必須將它從屏幕上拖出來,然後重新恢復正常。
我試圖減少我的代碼到最簡單的版本,仍然有問題。 (它只是讓一個非常大的瀏覽按鈕。
public class FormTest extends JFrame
{
private final int width = 490;
private final int height = 400;
private JPanel outerPanel;
private static FormTest myTest;
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
myTest = new FormTest();
myTest.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
myTest.setResizable(false);
myTest.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
closeWindow();
}
});
myTest.setVisible(true);
}
public FormTest()
{
super("Convert Ratings");
this.setSize(width, height);
initComponents();
}
private void initComponents()
{
outerPanel = new JPanel();
outerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 2, 0));
outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS));
outerPanel.add(Box.createRigidArea(new Dimension(0, 5)));
JButton myButton = new JButton("browse");
myButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(myTest);
}
});
outerPanel.add(myButton);
this.add(outerPanel);
}
private static void closeWindow()
{
int result = JOptionPane.showConfirmDialog(myTest, "Are you sure you want to close the application?",
"Question", JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
在這個例子中,瀏覽按鈕必須立即單擊窗口打開,約10秒後bug會脫穎而出後。
任何幫助或建議將非常感激。
感謝,
BJ
哪個操作系統您正在使用教程?在Linux下使用Swing時,我已經遇到類似的問題,Gnome作爲窗口管理器。這實際上是一個Java錯誤。 – 2010-08-30 15:52:57
我正在使用Windows – Benny 2010-08-30 16:03:03