简体   繁体   English

将JPanel添加到JFrame

[英]Adding a JPanel into JFrame

I have a class BoardGUI that extends JFrame . 我有一个扩展JFrame BoardGUI类。 I have added buttons in a JPanel . 我在JPanel添加了按钮。 When I try to add the panel into the frame with a mouselistoner on the frame, the buttons (undo and replay) become invisible. 当我尝试将面板添加到框架中并在其上使用mouselistoner时,按钮(撤消和重播)变得不可见。 When I mouse over the buttons, they become visible. 当我将鼠标悬停在按钮上时,它们变为可见。

Here is my code: 这是我的代码:

public class BoardGUI extends JFrame {
    JButton a=new JButton("Undo");
    JButton r=new JButton("replay");
    JPanel jp=new JPanel();

    public BoardGUI() {

        // TODO Auto-generated constructor stub
        setTitle("Checkers Game");
        setSize(645, 700);

        jp.setLayout(new FlowLayout());
        jp.setPreferredSize(new Dimension(645,35));
        a.setVisible(true);
        r.setVisible(true);
        jp.add(a);
        jp.add(r);
        add(jp,BorderLayout.SOUTH);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent arg0) {
                // TODO Auto-generated method stub
                repaint();

            }
        });


    }
    public void paint(Graphics g)
    {
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                g.fillRect(i*100, j*100, 100, 100);
            }
        }
    }
}

Can anybody help me to fix this please? 有人可以帮我解决这个问题吗?

  • override getPreferredSize for JPanel , then to call JFrame.pack() instead of any sizing JPanel重写getPreferredSize ,然后调用JFrame.pack()而不是任何大小调整

  • don't to set PreferredSize 不要设置PreferredSize

  • don't to override paint for JFrame , override paintComponent for (another, separate) JPanel , put this JPanel to the JFrames CENTER area 不要为JFrame覆盖paint ,为(另一个,单独的) JPanel覆盖paintComponent ,将此JPanel放在JFrames CENTER area

.

。

.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BoardGUI extends JFrame {

   private JButton a = new JButton("Undo");
   private  JButton r = new JButton("replay");
   private  JPanel jp = new JPanel();

    public BoardGUI() {
        setTitle("Checkers Game");
        jp.setLayout(new FlowLayout());
        jp = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(new Dimension(645, 35));
            }

           /* @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                //
            }*/
        };
        jp.add(a);
        jp.add(r);
        add(jp, BorderLayout.SOUTH);
        pack();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new BoardGUI();
            }
        });
    }
}

You've got a 645 x 700 JFrame onto which you paint an 800 x 800 checkerboard. 您有一个645 x 700 JFrame,在其上绘制了800 x 800棋盘格。 It's probably overwriting the buttons. 可能是覆盖了按钮。

Put the checkerboard in its own JPanel, and draw within that panel only. 将棋盘格放置在其自己的JPanel中,并仅在该面板内绘制。 Put that panel in the center of the JFrame. 将该面板放在JFrame的中心。

Do not use: 不使用:

a.setVisible(true);
r.setVisible(true);

NO need to use this. 无需使用它。 When you make frame visible using setVisible(true), all components will get painted. 使用setVisible(true)使框架可见时,将绘制所有组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM