Java JOptionPane 在背景上自定义绘制
Java JOptionPane custom draw on background
我用 Swing GUI 制作了一个 Java 桌面应用程序。在菜单栏中有一个 "About" 条目。单击此 "About" 菜单项时,将使用 JOptionPane.showMessageDialog(...) 显示一个 JDialog,其中包含一个图标、一段关于我的程序的文本和一个 "OK"-关闭对话框的按钮。
我想绘制一个或多个球(圆圈)在对话框背景中移动并改变颜色的效果。
使用@Titus 的解决方案更新代码:
自定义 JOptionPane class:
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
对象。
您可以重现 showMessageDialog
正在做的事情,即:
/**
* This method shows an INFORMATION_MESSAGE type message dialog.
*
* @param parentComponent The component to find a frame in.
* @param message The message displayed.
*/
public static void showMessageDialog(Component parentComponent, Object message){
JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(parentComponent, null);
dialog.show();
}
你可以把它改成这样:
MyEffectPane pane = new MyEffectPane("message....", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(this, null);
dialog.show();
此外,您必须将此构造函数添加到您的 MyEffectPane
class:
MyEffectPane(String message, int type){
super(message, type);
}
I want to draw an effect of one or more balls (circles) that move around in the background of the dialog and changeing their colors.
不要使用 JOptionPane。
相反,您可以创建您的自定义 JDialog
。然后你会添加一个 JPanel 到你的对话框中来进行自定义绘画。
阅读 Custom Painting 上的 Swing 教程部分了解更多信息。
请注意,在进行自定义绘制时,您的覆盖 paintComponent(..)
,而不是 paint(...)。
或者,如果需要,您可以将自定义 JPanel 添加到 JOptionPane。无论如何,关键是在 JPanel 上进行自定义绘画。
您不能覆盖 JOptionPane。它是静态的,主要用于方便创建确认选项。
你想要什么,你可以使用JDialog。 JDialog 使您可以更轻松地创建具有自定义内容的弹出窗口 window。
由于您已经在尝试使用 JOptionPane,我假设您有一个包含面板来触发 JOptionPane。您只需执行以下操作:
- 使用
SwingUtilities.getWindowAncestor(panel)
从父面板中提取包含的 window。
- 使用提取的 window 创建 JDialog:
JDialog dialog = new JDialog(containerWindow)
- 根据您的喜好自定义 JDialog 功能。
您的部分代码应与此类似:
Window containerWindow = SwingUtilities.getWindowAncestor(panel);
JDialog dialog = new JDialog(containerWindow);
dialog.setLayout(new BorderLayout());
dialog.setLocationRelativeTo(containerWindow);
dialog.setResizeable(false);
dialog.add(effectPanel, BorderLayout.CENTER);
在这种情况下,effectPanel 是您自己的自定义面板,其中包含图形和您想要执行的所有操作。这不是编写代码的最佳方式,但我把它写下来只是为了给你举个例子。您可以在此处查看 JDialog 的更多功能:JDialog
我用 Swing GUI 制作了一个 Java 桌面应用程序。在菜单栏中有一个 "About" 条目。单击此 "About" 菜单项时,将使用 JOptionPane.showMessageDialog(...) 显示一个 JDialog,其中包含一个图标、一段关于我的程序的文本和一个 "OK"-关闭对话框的按钮。
我想绘制一个或多个球(圆圈)在对话框背景中移动并改变颜色的效果。
使用@Titus 的解决方案更新代码:
自定义 JOptionPane class:
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
对象。
您可以重现 showMessageDialog
正在做的事情,即:
/**
* This method shows an INFORMATION_MESSAGE type message dialog.
*
* @param parentComponent The component to find a frame in.
* @param message The message displayed.
*/
public static void showMessageDialog(Component parentComponent, Object message){
JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(parentComponent, null);
dialog.show();
}
你可以把它改成这样:
MyEffectPane pane = new MyEffectPane("message....", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(this, null);
dialog.show();
此外,您必须将此构造函数添加到您的 MyEffectPane
class:
MyEffectPane(String message, int type){
super(message, type);
}
I want to draw an effect of one or more balls (circles) that move around in the background of the dialog and changeing their colors.
不要使用 JOptionPane。
相反,您可以创建您的自定义 JDialog
。然后你会添加一个 JPanel 到你的对话框中来进行自定义绘画。
阅读 Custom Painting 上的 Swing 教程部分了解更多信息。
请注意,在进行自定义绘制时,您的覆盖 paintComponent(..)
,而不是 paint(...)。
或者,如果需要,您可以将自定义 JPanel 添加到 JOptionPane。无论如何,关键是在 JPanel 上进行自定义绘画。
您不能覆盖 JOptionPane。它是静态的,主要用于方便创建确认选项。
你想要什么,你可以使用JDialog。 JDialog 使您可以更轻松地创建具有自定义内容的弹出窗口 window。
由于您已经在尝试使用 JOptionPane,我假设您有一个包含面板来触发 JOptionPane。您只需执行以下操作:
- 使用
SwingUtilities.getWindowAncestor(panel)
从父面板中提取包含的 window。 - 使用提取的 window 创建 JDialog:
JDialog dialog = new JDialog(containerWindow)
- 根据您的喜好自定义 JDialog 功能。
您的部分代码应与此类似:
Window containerWindow = SwingUtilities.getWindowAncestor(panel);
JDialog dialog = new JDialog(containerWindow);
dialog.setLayout(new BorderLayout());
dialog.setLocationRelativeTo(containerWindow);
dialog.setResizeable(false);
dialog.add(effectPanel, BorderLayout.CENTER);
在这种情况下,effectPanel 是您自己的自定义面板,其中包含图形和您想要执行的所有操作。这不是编写代码的最佳方式,但我把它写下来只是为了给你举个例子。您可以在此处查看 JDialog 的更多功能:JDialog