【发布时间】:2016-07-10 20:53:29
【问题描述】:
我用 Swing GUI 制作了一个 Java 桌面应用程序。在菜单栏中有一个“关于”条目。单击此“关于”菜单项时,使用 JOptionPane.showMessageDialog(...) 显示一个 JDialog,其中带有一个图标、关于我的程序的文本和一个“确定”按钮以关闭对话框。
我想绘制一个或多个在对话框背景中移动并改变颜色的球(圆)的效果。
使用@Titus 的解决方案更新代码:
自定义 JOptionPane 类:
class MyEffectPane extends JOptionPane {
public MyEffectPane(String message, int type){
super(message, type);
makeOpa(this);
}
// needed to make the drawings visible
void makeOpa(JComponent comp){
for(Component c : comp.getComponents()){
if(c instanceof JPanel){
JPanel p = (JPanel)c;
p.setOpaque(false);
makeOpa(p);
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
g.setColor(Color.BLACK);
g.fillOval(width/2-50, height/2-50, 50, 50);
}
}
调用/显示对话框:
MyEffectPane pane = new MyEffectPane("message....", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(this, "About");
//dialog.setIconImage(icon.getImage());
dialog.setVisible(true);
dialog.dispose();
【问题讨论】:
-
showMessageDialog方法是static(您不能覆盖它),它会创建自己的JOptionPane对象。 -
@Titus 谢谢,你知道我的问题的解决方案吗?
-
调试语句不要使用
System.out.println()。它不一定会在每次执行时打印一行,因为System.out是一个缓冲流。而是使用System.err.println()来保证立即输出。 -
@JoeCoder 谢谢!我不知道..
标签: java swing user-interface joptionpane jdialog