gpt4 book ai didi

java - ImageIcon 帮助 + 关闭 JOptionPane

转载 作者:行者123 更新时间:2023-12-01 08:05:19 24 4
gpt4 key购买 nike

我试图在您单击“右键”时显示 PayPal Logo 。不幸的是,显示的只是带有一杯咖啡和一支笔的默认 Java Logo 。

另外,我怎样才能做到一旦您单击“确定”或“取消”,它就会关闭 JOptionPane,目前,当您单击“确定”时什么也没有发生,它会一直给您“确定”/“取消”选项.

rightbutton = new JButton("Right.");
add(rightbutton);
rightbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//what do we want to happen when we
//click the button
final ImageIcon icon = new ImageIcon("C:\\Users\\Scr3am\\Desktop\\paypal.jpg");
JOptionPane.showOptionDialog(null, "Congratulations, you clicked the button.", "Congrats", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[] { panel }, icon);
}
}
);

完整代码

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Password extends JFrame {

JButton leftbutton;
JButton centerbutton;
JButton rightbutton;
FlowLayout layout;
Container container;




Password(){
super("Toolbar");
layout = new FlowLayout();
//get bulk of window, so it knows where to put the stuff
container = getContentPane();
setLayout(layout);

//left stuff in here
leftbutton = new JButton("Left");
add(leftbutton);
leftbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//what do we want to happen when we
//click the button
layout.setAlignment(FlowLayout.LEFT);

}


}

);

final JPanel panel = new JPanel();
panel.add(new JButton("OK"));
panel.add(new JButton("Cancel"));

//center stuff in here
centerbutton = new JButton("Center");
add(centerbutton);
centerbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//what do we want to happen when we
//click the button
layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(container);

}
}
);

//right stuff in here
rightbutton = new JButton("Right.");
add(rightbutton);
rightbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//what do we want to happen when we
//click the button
try {
final ImageIcon icon = new ImageIcon(ImageIO.read(new File("paypalicon.gif")));

JOptionPane.showOptionDialog(
null,
"Congratulations, you clicked the button.",
"Congrats",
JOptionPane.OK_OPTION,
JOptionPane.PLAIN_MESSAGE,
icon,
new Object[]{"Okay"},
"Okay");
} catch (IOException exp) {
exp.printStackTrace();
}



}
}
);
}


}

错误:

javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1275)
at Password$3.actionPerformed(Password.java:79)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6414)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6179)
at java.awt.Container.processEvent(Container.java:2084)
at java.awt.Component.dispatchEventImpl(Component.java:4776)
at java.awt.Container.dispatchEventImpl(Container.java:2142)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
at java.awt.Container.dispatchEventImpl(Container.java:2128)
at java.awt.Window.dispatchEventImpl(Window.java:2492)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:690)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

最佳答案

我认为你的参数设置错误

enter image description here

try {
final ImageIcon icon = new ImageIcon(ImageIO.read(new File("paypalicon.gif")));

JOptionPane.showOptionDialog(
null,
"Congratulations, you clicked the button.",
"Congrats",
JOptionPane.OK_OPTION,
JOptionPane.PLAIN_MESSAGE,
icon,
new Object[]{"Okay"},
"Okay");
} catch (IOException exp) {
exp.printStackTrace();
}

JOptionPane 声明 showOptionDialog具有以下参数(按顺序)

  • Component,与对话框关联的父组件
  • 对象,要显示的消息
  • 字符串,对话框标题
  • int,选项类型(如果未指定),例如 JOptionPane.OKAY_CANCEL_OPTION
  • int,消息类型,例如JOptionPane.INFORMATION_MESSAGE,可以定义对话框将使用的图标
  • Icon,要在对话框上显示的图标
  • Object[],可供用户使用的选项(按钮)
  • Object,获得焦点的初始选项

你好像路过了...

  • null 作为父级,好的。
  • “恭喜,您点击了按钮。”,好的
  • “恭喜”,好的
  • JOptionPane.DEFAULT_OPTION,好的
  • JOptionPane.INFORMATION_MESSAGE 可能没问题,我会使用 JOptionPane.PLAIN_MESSAGE,但这就是我
  • null...没有图标?
  • new Object[] { panel },不确定这是否可以,但至少它在正确的位置
  • 图标...这甚至不是您传递的选项参数中的值...认为这是遗漏的地方。

按钮的问题归结为它们与对话框断开连接。对话框绝对无法知道按钮是否被单击,您必须通过某种 ActionListener 提供该功能...说实话,它只是传递 String 值作为 options 参数,并让 JOptionPane 将它们呈现为按钮,因为它将处理关闭对话框,否则它会变得非常困惑,非常快...

关于java - ImageIcon 帮助 + 关闭 JOptionPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22186026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com