我想以編程方式從 顯示對話框JColorChooser
和JFileChooser
,這是我提交按鈕時調用的方法。JColorChooser和JFileChooser打開問題
點擊按鈕後,該方法被調用,但對話框將不會顯示。
我有一個空的佈局一個JFrame(絕對定位)和,例如,下面的代碼:
public class _TEST_ extends JFrame
{
private JColorChooser color_chooser;
private JFileChooser file_chooser;
public _TEST_()
{
super("_TEST_");
setLayout(null);
final JButton b = new JButton("Color chooser");
final JButton b2 = new JButton("File chooser");
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b)
{
createJColorChooser();
}
else if (e.getSource() == b2)
{
createJFileChooser();
}
}
};
b.addActionListener(al);
b2.addActionListener(al);
b.setBounds(1, 1, 160, 20);
b2.setBounds(1, 30, 160, 20);
add(b);
add(b2);
}
public void createJColorChooser()
{
color_chooser = new JColorChooser();
color_chooser.setBounds(1, 70, 225, 50);
add(color_chooser);
repaint();
}
public void createJFileChooser()
{
file_chooser = new JFileChooser();
file_chooser.setBounds(330, 70, 225, 50);
add(file_chooser);
repaint();
}
public static void main(String args[])
{
_TEST_ window = new _TEST_();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setLocationRelativeTo(null);
}
}
有什麼不對?
您可以發佈一個自包含的代碼示例,用主要方法來演示問題嗎?沒有足夠的代碼來解決發生了什麼問題。 – 2010-09-30 10:22:36
@Steve ok我做到了。 – xdevel2000 2010-09-30 11:32:10
用validate()代替示例代碼中的repaint()行並且代碼示例工作 – 2010-09-30 15:11:56