我想以编程方式从 显示对话框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