Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.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添加到JFrame中,只有一个是可见的_Java_Swing - Fatal编程技术网

Java 将两个JPanel添加到JFrame中,只有一个是可见的

Java 将两个JPanel添加到JFrame中,只有一个是可见的,java,swing,Java,Swing,我有以下问题,当我想将2JPanel添加到我的JFrame时,只有一个是可见的,这取决于我最后添加到框架的是哪个。我在两个JPanels上重写了JPanels默认的paintComponent()方法。我怎样才能解决这个问题 代码段: 边界: 时钟: public class GraphicTimer extends JPanel{ Timer _aktTimer = null; int seconds; int _width = 0; GraphicTimer(

我有以下问题,当我想将2JPanel添加到我的JFrame时,只有一个是可见的,这取决于我最后添加到框架的是哪个。我在两个JPanels上重写了JPanels默认的paintComponent()方法。我怎样才能解决这个问题

代码段: 边界:

时钟:

public class GraphicTimer extends JPanel{
    Timer _aktTimer = null;
    int seconds;
    int _width = 0;
    GraphicTimer(int width)
    {
        setSize(52, 31);
        setOpaque(false);
        _width = width;
        int delay = 1000; //milliseconds
        _aktTimer = new Timer(delay, taskPerformer);
    }
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        try
        {
            final int BUTTON_WIDTH = 20,BUTTON_HEIGHT = 20;
            int MINES_HORIZONTALLY = _width;
            int HORIZONTAL_ENDING = 15+BUTTON_WIDTH*MINES_HORIZONTALLY;

            BufferedImage clock = ImageIO.read(this.getClass().getResource("clock.png"));
            g.drawImage(clock,HORIZONTAL_ENDING-54,22, null);
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }
     ....
}
JFrame:

public class DrawerField extends JFrame implements Serializable{  
    //...
    public DrawerField() 
    {
        super("MineSweeper");
        _FIELD = new Field();
        constructorInit();
    }
    public void constructorInit()
    {        
        _buttons = new FieldButton[_height][_width];  
        _isMouseEventEnabled = true;
        fieldPanel = new JPanel();
        smilePanel = new JPanel();
        //INITS
        int fieldSizeWidth = (_width)*20;
        int fieldSizeHeight = (_height)*20; // Magic size
        fieldPanel.setSize(fieldSizeWidth,fieldSizeHeight); // 20x20 
        fieldPanel.setLocation(15, 70);
        fieldPanel.setLayout(new GridLayout(_width,_height)); 
        int fullWindowWidth = fieldSizeWidth+36;
        int fullWindowHeight = fieldSizeHeight+142;
        setSize(fullWindowWidth,fullWindowHeight);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        restartButton = new RestartButton(this);
        smilePanel.setSize(34,34);                    
        smilePanel.add(restartButton);
        smilePanel.setLayout(new GridLayout(1,1));  
        smilePanel.setLocation((int)fullWindowWidth/2-(34/2)-1,20);
        ///INITIALS

        for(int i = 0; i < _height; i++)
        {
            for(int j = 0; j < _width ; j++)
            {
                    _buttons[i][j] = new FieldButton(_hidden[i][j],true,i,j,this);
                    fieldPanel.add(_buttons[i][j]);
            }
        }

        add(smilePanel);
        add(fieldPanel);

        borderDrawer = new BorderDrawer(_width,_height);
        _graphicTimer = new GraphicTimer(_width);
        _graphicTimer.start();


        add(_graphicTimer);  // This is the two lines which change the result
        add(borderDrawer);

        MenuBar menuBar = new MenuBar(this);
        setJMenuBar(menuBar);
        setVisible(true);
    }
    //...
}

我的主要目标是在不使用任何布局的情况下向JFrame添加2个圆(正如您在上面的示例中看到的,我的JFrame上已经有很多东西,这就是我不想使用布局的原因)。这个例子中的问题是一样的,当我添加第二个圆圈时,第一个圆圈消失了。

很简单。在框架中添加面板会将其添加到框架的
内容窗格中。此内容窗格的默认布局为
BorderLayout
,这意味着每次添加面板时,它都会被添加到内容窗格的中心,并替换上一个面板。这就是为什么你只看到最后一个。将Jpanel设置为您选择的布局,将需要显示的所有内容都显示在该面板的屏幕上,这总是很好的。如果您不想这样做,您也可以从框架中调用
getContentPane()
,并使用返回的
JPanel

实例,下面是示例代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Frame extends JFrame {
    public Frame() {
        setTitle("Two panels");
        setSize(new Dimension(500,500));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Here goes your code
        JPanel p= (JPanel) getContentPane();
        p.setLayout(new GridLayout(1,2)); //set your own layout
        p.add(new MyPanel(Color.BLUE)); //add panel with blue border
        p.add(new MyPanel(Color.GREEN));//add panel with green border
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Frame f= new Frame();
                f.setVisible(true);
            }
        });
    }
}

class MyPanel extends JPanel {
    public MyPanel(Color color) {
        setBorder(BorderFactory.createLineBorder(color));
    }
}
运行它并查看。。。您应该能够看到如下内容:


如需更快地获得更好的帮助,请发布或。对于面板,有一个红色面板和一个蓝色面板。尝试了
repaint()
revalidate()
?移除所有绘画并演示如何将两个面板添加到框架中。borderDrawer=新的borderDrawer(_宽度,_高度)_graphicTimer=新的graphicTimer(_宽度);添加(_graphicTimer);//这是更改结果的两行add(borderDrawer);Jframe使用默认布局,graphicTimer和borderDrawer是我自己的JPanel;添加(现场面板)顺便说一句-默认情况下,
JFrame
BorderLayout
。添加到没有约束的边框布局中的构件默认为
CENTER
。边框布局的5个区域中的每个区域都可以精确显示1个组件。如果它是..,那么它将“起作用”<代码>添加(smilePanel,BorderLayout.PAGE_START);添加(现场面板,边框布局。第页\结尾)。。以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则提供更宽和更高的宽度。我尝试获取内容窗格,并将其添加到其中,但结果是相同的。你能在这个简单的例子中告诉我你是怎么想的吗?正如我在帖子中已经说过的,我不/不能使用布局来解决这个问题,因为我还有很多其他面板。还有其他建议吗?嘿,不必每次添加面板时都设置布局。只需在某个地方设置一次,然后继续添加或删除面板。在更简单的示例中,在将面板添加到框架之前,只需添加行
((JPanel)getContentPane()).setLayout()
。它会起作用的。
public class Main {

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setSize(500, 500);

        MyPanel panel1 = new MyPanel(30,30);
        frame.add(panel1);
        MyPanel panel2 = new MyPanel(70,30);
        frame.add(panel2);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.repaint();
        frame.setVisible(true);
    }

    public static class MyPanel extends JPanel
    {
        int _x,_y;
        MyPanel(int x, int y)
        {
            _x = x; _y = y;
        }
        @Override
        public void paint(Graphics g) {


            g.drawOval(_x,_y,20,20);
        }
    }
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Frame extends JFrame {
    public Frame() {
        setTitle("Two panels");
        setSize(new Dimension(500,500));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Here goes your code
        JPanel p= (JPanel) getContentPane();
        p.setLayout(new GridLayout(1,2)); //set your own layout
        p.add(new MyPanel(Color.BLUE)); //add panel with blue border
        p.add(new MyPanel(Color.GREEN));//add panel with green border
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Frame f= new Frame();
                f.setVisible(true);
            }
        });
    }
}

class MyPanel extends JPanel {
    public MyPanel(Color color) {
        setBorder(BorderFactory.createLineBorder(color));
    }
}