在 jFrame 中加载带有图像的面板:组件必须为非空

Posted

技术标签:

【中文标题】在 jFrame 中加载带有图像的面板:组件必须为非空【英文标题】:Loading Panel with Image in jFrame: Component must be non-null 【发布时间】:2015-08-28 09:04:04 【问题描述】:

大家好,我是新手,

我想在我的 JFrame 中添加图片。我使用了 Grouplayout 并尝试在 this JPanel-Class 的帮助下加载我的图像。 但是当我使用“.addComponent(Image)”时,它给了我以下错误:

线程“主”java.lang.IllegalArgumentException 中的异常: 组件必须在 javax.swing.GroupLayout$ComponentSpring.(Unknown Source) at javax.swing.GroupLayout$ComponentSpring.(Unknown Source) at javax.swing.GroupLayout$Group.addComponent(Unknown Source) at javax.swing.GroupLayout$ParallelGroup.addComponent(Unknown Source) at javax.swing.GroupLayout$ParallelGroup.addComponent(Unknown Source) at javax.swing.GroupLayout$Group.addComponent(Unknown Source) at javax.swing.GroupLayout$ParallelGroup.addComponent(Unknown Source) at infpp.OceanLife.OceanLifeGUI.initializeGUI(OceanLifeGUI.java:237) 在 infpp.OceanLife.OceanLifeGUI.(OceanLifeGUI.java:50) 在 infpp.OceanLife.OceanLifeController.start(OceanLifeController.java:39) 在 infpp.OceanLife.OceanLifeMain.main(OceanLifeMain.java:22)

我的代码:

private Images image;


        GroupLayout.ParallelGroup drawHorizontal = drawLayout.createParallelGroup(Alignment.LEADING);

        drawHorizontal.addComponent(image);


        GroupLayout.ParallelGroup drawVertical = drawLayout.createParallelGroup(Alignment.LEADING);

        drawVertical.addComponent(image);

编辑:

图像类

    public class Image extends JPanel
OceanInterface ocean;

/**
 * All Images that display the ocean
 */

public static BufferedImage oceanImage, 
            fishImage1, fishImage2, fishImage3,
            plantImage1,plantImage2, bubbleImage1, 
            bubbleImage2, bubbleImage3, 
            stoneImage1, stoneImage2, sharkImage;

public Image() 
   try                 
       oceanImage = ImageIO.read(new File("media/ocean.png"));
    catch (IOException ex) 
       // If there is an error:
       final JOptionPane optionPane
                       = new JOptionPane();
       JOptionPane.showMessageDialog(
                       optionPane,
                       "Fail.",
                       "ERROR",
                       JOptionPane.ERROR_MESSAGE);
       System.exit(0);
   


@Override
protected void paintComponent(Graphics g) 
    super.paintComponent(g);
    g.drawImage(oceanImage, 0, 0, null); // see javadoc for more info on the parameters            

GUI的完整代码:

package infpp.OceanLife;


    import java.awt.Color;
    import java.awt.Dimension;
    import java.util.LinkedList;

    import javax.swing.BorderFactory;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;



    public class OceanLifeGUI extends JFrame

/**
 * Ocean the you want to control and get displayed
 */
public OceanInterface ocean;

private Image image;



/**
 * OceanGraphic object which contains the oceans drawing
 */
private OceanLifeGraphic oceanGraphic;


/**
 * Method to create a new GUI with a given Ocean
 *
 * @param oc - OceanInterface you want to control and get displayed
 */
public OceanLifeGUI(OceanInterface oc) 

    // Set the ocean
    this.ocean = oc;

    // Initialize the objects of the GUI
    initializeGUI();


private void initializeGUI()

    // Create new JFrame
    JFrame OceanLife = new JFrame();

    // Default exit
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    // Create Panels (layout, oceanDraw, oceanControl)
    JPanel oceanLayout = new JPanel();
    JPanel oceanDraw = new JPanel();
    JPanel oceanControl = new JPanel();

    // Layoutmanager
    GroupLayout layout = new GroupLayout(oceanLayout);
    GroupLayout drawLayout = new GroupLayout(oceanDraw);
    GroupLayout controlLayout = new GroupLayout(oceanControl);

    // Set Layout
    oceanDraw.setLayout(drawLayout);
    oceanControl.setLayout(controlLayout);

    // Creates Buttons
    JButton loadButton = new JButton("Load");
    JButton saveButton = new JButton("Save");
    JButton quitButton = new JButton(" Quit ");
    JButton startButton = new JButton("Start");
    JButton stepButton = new JButton("Step ");
    JButton stopButton = new JButton("Stop ");

    JButton addButton = new JButton("ADD");
    addButton.setPreferredSize(new Dimension (200,20));
    JButton deselectButton = new JButton("DESELECT");
    JButton removeButton = new JButton("REMOVE");
    JButton clearButton = new JButton("CLEAR");

    // Create Labels
    JLabel gameControlLabel = new JLabel("Gamecontrol");
    JLabel objectControlLabel = new JLabel("Objectcontrol");
    JLabel xLabel = new JLabel("x");
    JLabel yLabel = new JLabel("y");

    // Create Textfields
    JTextField xInput = new JTextField("0");
    JTextField yInput = new JTextField("0");

    // Create ComboBox
    JComboBox objectSelectBox = new JComboBox(new String[]  "Fish", "Plant", "Stone", "Bubble" );

    // Create List
    JList objectSelectList = new JList(new String[]  "Fish", "Plant", "Stone", "Bubble" );

    JScrollPane objectSelectScroll = new JScrollPane(objectSelectList);




    // Control Panel
    GroupLayout.ParallelGroup controlHorizontal = controlLayout.createParallelGroup();

        GroupLayout.ParallelGroup controlPanel = controlLayout.createParallelGroup(Alignment.CENTER);

        // GamecontrolLabel (1)

        // GamecontrolButtons (2)
        GroupLayout.SequentialGroup gameControlButtons = controlLayout.createSequentialGroup();

            GroupLayout.ParallelGroup columnLeft = controlLayout.createParallelGroup();
                columnLeft.addComponent(loadButton);
                columnLeft.addComponent(startButton);

            GroupLayout.ParallelGroup columnMiddle = controlLayout.createParallelGroup();
                columnMiddle.addComponent(saveButton);
                columnMiddle.addComponent(stepButton);

            GroupLayout.ParallelGroup columnRight = controlLayout.createParallelGroup();
                columnRight.addComponent(quitButton);
                columnRight.addComponent(stopButton);

            gameControlButtons.addGroup(columnLeft);
            gameControlButtons.addGap(5,5,5);
            gameControlButtons.addGroup(columnMiddle);
            gameControlButtons.addGap(5,5,5);
            gameControlButtons.addGroup(columnRight);

        // ObjectcontrolLabel (3)

        // ObjectSelect and ADD (4)
        GroupLayout.SequentialGroup objectSelect = controlLayout.createSequentialGroup();
            objectSelect.addComponent(objectSelectBox);
            objectSelect.addGap(5,5,5);
            objectSelect.addComponent(xLabel);
            objectSelect.addGap(5,5,5);
            objectSelect.addComponent(xInput);
            objectSelect.addGap(5,5,5);
            objectSelect.addComponent(yLabel);
            objectSelect.addGap(5,5,5);
            objectSelect.addComponent(yInput);

        // ADD-Button (5)

        // ObjectSelectList with ScrollPanel (6)

        // DESELECT-Button (7)

        // REMOVE-Button (8)

        // CLEAR-Button (9)

        // Call controlPanel-Components
        controlPanel.addComponent(gameControlLabel);
        controlPanel.addGroup(gameControlButtons);
        controlPanel.addComponent(objectControlLabel);
        controlPanel.addGroup(objectSelect);
        controlPanel.addComponent(addButton);
        controlPanel.addComponent(objectSelectScroll);
        controlPanel.addComponent(deselectButton);
        controlPanel.addComponent(removeButton);
        controlPanel.addComponent(clearButton);


  // Call Horizontal-Groups
   controlHorizontal.addGroup(controlPanel);



   GroupLayout.SequentialGroup controlVertical = controlLayout.createSequentialGroup();

        GroupLayout.ParallelGroup firstRow = controlLayout.createParallelGroup();
            firstRow.addComponent(loadButton);
            firstRow.addComponent(saveButton);
            firstRow.addComponent(quitButton);

        GroupLayout.ParallelGroup secondRow = controlLayout.createParallelGroup();
            secondRow.addComponent(startButton);
            secondRow.addComponent(stepButton);
            secondRow.addComponent(stopButton);


        GroupLayout.ParallelGroup thirdRow = controlLayout.createParallelGroup(Alignment.BASELINE);
            thirdRow.addComponent(objectSelectBox);
            thirdRow.addComponent(xLabel);
            thirdRow.addComponent(xInput);
            thirdRow.addComponent(yLabel);
            thirdRow.addComponent(yInput);

        GroupLayout.ParallelGroup fourthRow = controlLayout.createParallelGroup(Alignment.BASELINE);
            fourthRow.addComponent(addButton, 200,200,200);




        // Call Vertical-Groups
        controlVertical.addComponent(gameControlLabel);
        controlVertical.addGap(5,5,5);
        controlVertical.addGroup(firstRow);
        controlVertical.addGap(5,5,5);
        controlVertical.addGroup(secondRow);
        controlVertical.addGap(200,200,200);
        controlVertical.addComponent(objectControlLabel);
        controlVertical.addGap(5,5,5);
        controlVertical.addGroup(thirdRow);
        controlVertical.addGap(5,5,5);
        controlVertical.addComponent(addButton);
        controlVertical.addGap(5,5,5);
        controlVertical.addComponent(objectSelectScroll);
        controlVertical.addGap(5,5,5);
        controlVertical.addComponent(deselectButton);
        controlVertical.addGap(5,5,5);
        controlVertical.addComponent(removeButton);
        controlVertical.addGap(5,5,5);
        controlVertical.addComponent(clearButton);
        controlVertical.addGap(5,5,5);






    // OceanDraw

    GroupLayout.ParallelGroup drawHorizontal = drawLayout.createParallelGroup(Alignment.LEADING);

        drawHorizontal.addComponent(image);


        GroupLayout.ParallelGroup drawVertical = drawLayout.createParallelGroup(Alignment.LEADING);

        drawVertical.addComponent(image);





    // Layout
    GroupLayout.ParallelGroup layoutHorizontal = layout.createParallelGroup();

        layoutHorizontal.addComponent(oceanDraw);
        layoutHorizontal.addComponent(oceanControl);

    GroupLayout.SequentialGroup layoutVertical = layout.createSequentialGroup();

        layoutVertical.addComponent(oceanControl);
        layoutVertical.addComponent(oceanDraw);






    controlLayout.setHorizontalGroup(controlHorizontal);
    controlLayout.setVerticalGroup(controlVertical);

    drawLayout.setHorizontalGroup(drawHorizontal);
    drawLayout.setVerticalGroup(drawVertical);

    layout.setHorizontalGroup(layoutHorizontal);
    layout.setVerticalGroup(layoutVertical);



    // Add Panels (OceanDraw, OceanControl) to jFrame (OceanLife)
    OceanLife.add(oceanLayout);
    //OceanLife.add(oceanDraw);

    oceanDraw.setBackground(Color.black);
    oceanDraw.setPreferredSize(new Dimension (1000,600));


    // Pack?
    OceanLife.pack();
    OceanLife.setVisible(true);
    OceanLife.setResizable(false);
    OceanLife.setSize(1220, 650);
    

【问题讨论】:

变量image的类型是什么 是***.com/questions/299495/…的ImagePanel 您能否发布关于您在何处以及如何初始化image 变量的帖子? 【参考方案1】:

你的问题是你已经声明了变量image,但没有初始化它。因此,当您在drawHorizontal.addComponent(image); 中使用变量image 时,变量为null

所以,我的建议是你初始化变量image 像:

image = new Image();

drawHorizontal.addComponent(image);行前

【讨论】:

以上是关于在 jFrame 中加载带有图像的面板:组件必须为非空的主要内容,如果未能解决你的问题,请参考以下文章

如何在面板-EXTJS内的列表/表中加载动态数据

背景图像不想在css中加载

等待图像在 JavaScript 中加载

在 React Native Image 组件中加载本地图像

带有laravel的DOMPDF - 无法在html文件中加载图像

在 React 组件和 browserify 包中加载带有插件的 jQuery