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



你好,我是这里的新手,

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

线程"main"中的异常 java.lang.IllegalArgumentException: 组件必须在以下位置为非空 javax.swing.GroupLayout$ComponentSpring.(来源不明) javax.swing.GroupLayout$ComponentSpring.(来源不明) 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 英菲普。OceanLife.OceanLifeGUI.initializeGUI(OceanLifeGUI.java:237) at 英菲普。海洋生物.海洋生物.(海洋生物协会.java:50) 在 英菲普。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            
}

}

图形用户界面的完整代码:

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但没有初始化它。因此,当您在 drawHorizontal.addComponent(image); 中使用变量 image 时,变量是null的。

因此,我的建议是将变量初始化image如下所示:

image = new Image();

行前drawHorizontal.addComponent(image);

相关内容

  • 没有找到相关文章

最新更新