Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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中将JPanel类传递给主类_Java_Swing_Jpanel_Paintcomponent - Fatal编程技术网

在JAVA中将JPanel类传递给主类

在JAVA中将JPanel类传递给主类,java,swing,jpanel,paintcomponent,Java,Swing,Jpanel,Paintcomponent,我需要将一个JPanel扩展类传递给主类 以下是我到目前为止的情况: 主类 import java.awt.Color; import javax.swing.*; public class main { private gamePanel gamepanel = new gamePanel(); public JPanel createContentPane(){ // We create a bottom JPanel to place everythi

我需要将一个JPanel扩展类传递给主类

以下是我到目前为止的情况: 主类

import java.awt.Color;
import javax.swing.*;

public class main {

    private gamePanel gamepanel = new gamePanel();

    public JPanel createContentPane(){
        // We create a bottom JPanel to place everything on.
        JPanel totalGUI = new JPanel();

        //We set the Layout Manager to null so we can manually place
        // the Panels.
        totalGUI.setLayout(null);

        //Now we create a new panel and add it to the bottom JPanel.

        totalGUI.add(gamepanel);



        totalGUI.setOpaque(true);
        return totalGUI;
    }

    private static void createAndShowGUI(){
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] There's a JPanel in here! [=]");

        //Create and set up the content pane.
        main demo = new main();
        frame.setContentPane(demo.createContentPane());

        //The other bits and pieces that make your program a bit more stable.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700,500);
        frame.setVisible(true);

    }
    public static void main(String[] args) {
        //Schedule a jog for the event-dispatching thread:
        //creating and showing this application's GUI.
        System.out.println(gamepanel);
        SwingUtilities.invokeLater(new Runnable() {
            public void run(){
                createAndShowGUI();
            }
        });
    }
}
gamePanel类

public class gamePanel extends JPanel implements Commons {

    private Dimension d;
    private ArrayList snowmens;
    private coreFunctions functions = new coreFunctions();
    private int snowmenX = 150;
    private int snowmenY = 5;
    private final String snowmenpix = "snowman.png";
    private JPanel background;

public gamePanel() {



    add(new JLabel("TEST"));
    setFocusable(true);


    setDoubleBuffered(true);
}


@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, 700, 700);



}
}
我不明白为什么蓝色背景和标签没有显示出来

编辑:

详情如下:

好的,我想做一个2D小游戏。为此,我需要在gamePanel类上创建一些雪人,并通过main类显示它。要启动它,createContentPane会创建一个背景面板,根面板(如果需要)。createandshowgui创建一个JFrame

gamepanel类实际上是一个JPanel,到目前为止,它有一个面板,即后台面板。现在,我只希望它有一个蓝色的背景

我试着这样说,因为我看到了一些例子,它们与我所看到的非常相似,但出于某种原因,我无法让它工作

谢谢,,
Ara

您应该使用
LayoutManager
而不是
setLayout(null)如果只添加了一个组件,则也无需添加(除非添加其他组件,否则该组件将拉伸以填充)

有关
LayoutManager
s的教程,请参见此处:

如果您所要做的只是有一个蓝色背景(您可以在其上添加组件),则只需覆盖
gamePanel
paintComponent()
,并将背景涂成蓝色:

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
然后,您可以添加
JLabel
,就像它是一个普通面板一样,因为背景现在被涂成蓝色,而不是由/设置为组件

如果您有图像,请查看
g.drawImage(图像图像,int x,int y,ImageObserver iO)

编辑

请挂断以下电话:

setLayout(空)


而且它会工作

您应该使用
LayoutManager
而不是
setLayout(null)如果只添加了一个组件,则也无需添加(除非添加其他组件,否则该组件将拉伸以填充)

有关
LayoutManager
s的教程,请参见此处:

如果您所要做的只是有一个蓝色背景(您可以在其上添加组件),则只需覆盖
gamePanel
paintComponent()
,并将背景涂成蓝色:

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
然后,您可以添加
JLabel
,就像它是一个普通面板一样,因为背景现在被涂成蓝色,而不是由/设置为组件

如果您有图像,请查看
g.drawImage(图像图像,int x,int y,ImageObserver iO)

编辑

请挂断以下电话:

setLayout(空)



而且它会起作用

你的问题是,而且缺乏必要的信息。请详细说明技术、意图、预期结果以及解决您的问题的一些尝试。我添加了更多详细信息。希望现在有足够的信息…哇,接受率有了很大的提高,现在100%:)我忘了选择答案了。感谢您让我记住:)1+以上的投票,感谢您通过提供更多信息来改进您的问题。谢谢@DavidK的答案为1+。您的问题是,并且缺少所需信息。请详细说明技术、意图、预期结果以及解决您的问题的一些尝试。我添加了更多详细信息。希望现在有足够的信息…哇,接受率有了很大的提高,现在100%:)我忘了选择答案了。感谢您让我记住:)1+以上的投票,感谢您通过提供更多信息来改进您的问题。谢谢@DavidK的答案为1+。此外,默认情况下启用了双缓冲区;)@我从来不知道帽子+1
JPanel
的默认布局是
FlowLayout
,它应该水平附加元素。所以对我来说,第二项应该在
背景的右侧。我在这里怀疑的是,由于
背景
面板没有首选大小,因此无论大小设置如何,它都以宽度=0的方式呈现…我尝试删除新的Jlabel,但它仍然无法工作。。。我还添加了一组首选大小,但没有任何运气…@user1049769它仍然不起作用,因为我告诉你要做的是覆盖
paintComponent()
你覆盖了paint()?除非您将
JPanel
设置为透明,否则请在主类中删除对
setOpaque
的调用,并删除
setLayout(null)
。请参见编辑的postAlso,默认情况下启用双缓冲区;)@我从来不知道帽子+1
JPanel
的默认布局是
FlowLayout
,它应该水平附加元素。所以对我来说,第二项应该在
背景的右侧。我在这里怀疑的是,由于
背景
面板没有首选大小,因此无论大小设置如何,它都以宽度=0的方式呈现…我尝试删除新的Jlabel,但它仍然无法工作。。。我还添加了一组首选大小,但没有任何运气…@user1049769它仍然不起作用,因为我告诉你要做的是覆盖
paintComponent()
你覆盖了paint()?除非您将
JPanel
设置为透明,否则请在主类中删除对
setOpaque
的调用,并删除
setLayout(null)
。见编辑后的文章