Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将图像添加到JOptionPane_Java_Image_Swing_Joptionpane_Messagedialog - Fatal编程技术网

Java 将图像添加到JOptionPane

Java 将图像添加到JOptionPane,java,image,swing,joptionpane,messagedialog,Java,Image,Swing,Joptionpane,Messagedialog,我想知道如何将图像添加到Message对话框中。我尝试了下面的代码,但没有找到图像 else if(button == B){ String text = "blahblahblahblahblah"; JTextArea textArea = new JTextArea(text); textArea.setColumns(30); textArea.setLineWrap( true ); textArea.setWrapStyleWord( true ); textArea.se

我想知道如何将图像添加到Message对话框中。我尝试了下面的代码,但没有找到图像

else if(button == B){
String text = "blahblahblahblahblah";
    JTextArea textArea = new JTextArea(text);

textArea.setColumns(30);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.setSize(textArea.getPreferredSize().width, 1);
Font font = new Font("Verdana", Font.BOLD, 12);
textArea.setFont(font);
textArea.setForeground(Color.BLUE);
JOptionPane.showMessageDialog(
null, textArea, "Border States", JOptionPane.PLAIN_MESSAGE);

image2 = new ImageIcon(getClass().getResource("borderstates.jpg"));
  label2 = new JLabel(image2);
  add(label2);

什么是MessageDialogBox?如果您的意思是将图像添加到JOptionPane中,则存在接受图标的方法重载,因此这是解决此问题的一种方法。另一种方法是使用图像和其他组件创建JPanel或JLabel,然后在JOptionPane中显示它。

来自JOptionPane上的javadoc:

public static void showMessageDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType,
                                     Icon icon)
                          throws HeadlessException
只需为图像制作一个
图标
,并将其添加为第五个参数

JOptionPane.showMessageDialog(null, textArea, "Border States", 
    JOptionPane.PLAIN_MESSAGE, image2);

在使用image2之前不要忘记定义它(向上移动队列)

JOptionPane
是一个非常灵活的API

您的第一个呼叫端口应该是和,具体


什么是MessageDialogBox?从showMessageDialog方法感谢大家的帮助!我是根据梅尔和麦迪的建议想出的!正如您在代码中看到的那样,我使用了一个JLabel,但是如何将它放在JOptionPane中呢。注意:我不熟悉Java和常规+1编程,还有用于显示多个图像和在运行时调整其大小的HTML格式。Nice globe。这是我见过的最好的“Hello world”GUI版本。:)
public class TestOptionPane04 {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                ImageIcon icon = new ImageIcon(TestOptionPane04.class.getResource("/earth.png"));
                JOptionPane.showMessageDialog(
                        null,
                        "Hello world",
                        "Hello", JOptionPane.INFORMATION_MESSAGE,
                        icon);
                JOptionPane.showMessageDialog(
                        null,
                        new JLabel("Hello world", icon, JLabel.LEFT),
                        "Hello", JOptionPane.INFORMATION_MESSAGE);

            }
        });
    }
}