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

时间:2015-12-20 11:09:13

标签: java swing

Game

我有以下问题,当我想将2JPanel添加到我的JFrame时,只有一个是可见的,这取决于我最后添加到帧中。我在两个JPanel上都覆盖了JPanels默认的paintComponent()方法。我该如何解决这个问题?

代码段: 边框:

public class BorderDrawer extends JPanel{
    private int _width,_height;

    BorderDrawer(int width,int height)
    {
        setOpaque(false);
        _width = width;
        _height = height;
    }

    @Override
    protected void paintComponent(Graphics g) {
        final int BUTTON_WIDTH = 20,BUTTON_HEIGHT = 20;
        int MINES_HORIZONTALLY = _width;
        int MINES_VERTICALLY = _height;
        super.paintComponent(g);
        try{
            BufferedImage topLeftCorner = ImageIO.read(this.getClass().getResource("topLeftCorner.png"));
            g.drawImage(topLeftCorner, 0, 0, null); 
            ....// drawing other border components
    }
}

时钟:

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);
    }
    //...
}

更简单,可编辑的例子:

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);
        }
    }
}

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

2 个答案:

答案 0 :(得分:2)

简单。在框架中添加面板会将其添加到框架的def open_browser(): return webdriver.Firefox() 。此内容窗格的默认布局为b1 = threading.Thread(target=open_browser, args=()) ,这意味着,每次添加面板时,它都会添加到内容窗格的中心并替换上一个面板。这就是为什么你只看到最后一个的原因。使用Jpanel设置到您选择的布局总是很好,把所有需要显示在该面板的屏幕上。如果您不想这样做,您也可以从框架中调用content pane并使用返回的BorderLayout实例进行播放。

答案 1 :(得分:1)

以下是您的示例代码:

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));
    }
}

运行它,看看......你应该能够看到这样的东西:

Frame with two panels