java - 在面板之间交换

标签 java swing graphics jpanel imageicon

我正在尝试在面板中的图像上添加按钮。我也在尝试在面板之间切换。该程序正在运行,但是当我单击“说明”按钮时,它会在 cmd 中给出大量错误列表。这是什么问题?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JButton;
import javax.swing.JPanel;
public class htw10 extends JFrame
{
    final JFrame f=new JFrame("Hunt The Wumpus");
    private static final String FIRST_PANEL="first panel";
    private static final String SECOND_PANEL="second panel";
    private CardLayout cardLayout=new CardLayout();
    private JPanel content;

    public void start()
    {

        // Create a new panel, make 'content'  refer to it
    content = new JPanel();

    // Set the content pane of the window to the panel we just created
    f.setContentPane(content);

    // Create a button group and some buttons


    // Set the layout of the content panel and add buttons
    content.setLayout(new FlowLayout());

    // Create and add the intro panel and instruction panel to the content panel
    content.add(introPanel(),FIRST_PANEL);
    content.add(instructionPanel(),SECOND_PANEL);

    f.setSize(750,500);
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    f.setVisible(true);


}


    private JPanel instructionPanel()
        {
            JPanel secondPanel=new JPanel();
            ImageIcon icon=new ImageIcon("img2.jpg");
            JLabel pic2 = new JLabel(icon);
            secondPanel.add(pic2);
        JButton b1=new JButton("Back");
        content.add(b1);
        b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    cardLayout.show(content,FIRST_PANEL);
                }
            });
        secondPanel.repaint();
            return secondPanel;
        }
        public JPanel introPanel()
    {
            JPanel iPanel=new JPanel();
        ImageIcon icon=new ImageIcon("img1.jpg");
            JLabel picLabel = new JLabel(icon);
            iPanel.add(picLabel);

        ButtonGroup group=new ButtonGroup();
            JButton b1=new JButton("Instructions");
            JButton b2=new JButton("Play");
            JButton b3=new JButton("Exit");
        picLabel.add(b1);
        //f.getContentPane().add(picLabel,BorderLayout.SOUTH);
        content.add(b1);
            content.add(b2);
            content.add(b3);
        // Add a listener to the 'Instructions' button
            // so that the cardLayout is shown when the button is clicked
        b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    cardLayout.show(content,SECOND_PANEL);
                }
            });
        iPanel.repaint(); 
            return iPanel;
    }
    public static void main(String args[])throws Exception
    {
        htw10 obj=new htw10();
        obj.start();
    }

}

最佳答案

在第 14 行中,您声明了一个成员变量 content,但并未初始化它。如果您不自己初始化成员变量,它们将自动初始化为null:

private JPanel content;   // is automatically set to null

在第 25 行,您在 content 上调用方法 setLayout:

content.setLayout(new FlowLayout());

这将导致 NullPointerException,因为 contentnull

要了解有关 NullPointerException 是什么及其发生原因的更多信息,请参阅:What is a NullPointerException, and how do I fix it?

您需要将content设置为某些内容。看来这应该是指内容 Pane 。此外,您多次调用 introPanel() 方法,导致创建此面板的多个实例。那不是你想要的。该面板只应创建一次,然后您就应该使用该面板。不要多次调用introPanel()。您的 start() 方法应如下所示:

public void start()
{
    // Create a new panel, make 'content'  refer to it
    content = new JPanel();

    // Set the content pane of the window to the panel we just created
    f.setContentPane(content);

    // Create a button group and some buttons
    ButtonGroup group=new ButtonGroup();
    JButton b1=new JButton("Instructions");
    JButton b2=new JButton("Play");
    JButton b3=new JButton("Exit");

    // Set the layout of the content panel and add buttons
    content.setLayout(new FlowLayout());
    content.add(b1);
    content.add(b2);
    content.add(b3);

    // Create and add the intro panel and instruction panel to the content panel
    content.add(introPanel(),FIRST_PANEL);
    content.add(instructionPanel(),SECOND_PANEL);

    f.setSize(750,360);
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    f.setVisible(true);

    // Add a listener to the 'Instructions' button
    // so that the cardLayout is shown when the button is clicked
    b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cardLayout.show(content,SECOND_PANEL);
        }
    }); 
}

关于java - 在面板之间交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39868855/

相关文章:

math - 从平面上的 2D 点转换为 3D

java - 如何通过 servlet 提供批量文件下载?

java - 使用 Ext JS 作为基于 Java/Spring/Hibernate 的 Web 应用程序的一部分的最佳方法是什么?

Java Swing : best way to set background image clickable

用于创建图形的 Java 库

java - 哪个线程用于非 GUI 图形?

java - 是否可以在不启动相应应用程序的情况下在android中发送电子邮件?

java - JPA with Hibernate - 如果实现复合键则缺少列

java - 与日志框架相关的特殊死锁

c++ - FillRectangle 出现轻微不合需要的透明度