如何在JFrame上设置JPanel? [英] How to set a JPanel over a JFrame?

查看:256
本文介绍了如何在JFrame上设置JPanel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从JFrame扩展的名为BoardGUI的类,在构造函数中,我制作了一个带有两个按钮的JPanel.我已将此面板添加到框架中.每当我运行该程序时,按钮都将不可见.当我将鼠标光标移到按钮上时,它们就可见了.代码如下:

I have a class named BoardGUI extended from JFrame, in a constructor I have made a JPanel with two buttons in it. I have added this panel into my frame. Whenever I run this program, buttons get invisible. As I bring my mouse cursor over the buttons they get visible. Code is as follows:

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

public BoardGUI() {
    pieces = new Play();
    setTitle("Checkers Game");
    setSize(645, 700);
    setVisible(true);

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

我也在程序中使用重绘方法.有人可以指出我的错误并提出解决方案吗?

I am also using repaint method in my program. Can anybody point out my mistake and suggest any solution for this?

推荐答案

我在构造函数中有一个从JFrame扩展的名为BoardGUI的类 制作了一个带有两个按钮的JPanel.我已经添加了这个面板 进入我的框架.每当我运行此程序时,按钮将不可见.作为 我将鼠标光标移到可见的按钮上.

I have a class named BoardGUI extended from JFrame, in a constructor i have made a JPanel with two buttons in it. I have added this panel into my frame. Whenever i run this program, buttons get invisible. As i bring my mouse cursor over the buttons they get visible.

  • setVisible(true);应该是构造函数中的最后一行代码,因为您已经将JComponents添加到了已经可见的JFrame

    • setVisible(true); should be last code line in constructor, because you added JComponents to the already visible JFrame,

      或在将JComponents添加到可见的Swing GUI中的情况下调用revalidate()repaint()

      or to call revalidate() and repaint() in the case that JComponents are added to visible Swing GUI

      没有必要为标准JComponents调用a.setVisible(true);r.setVisible(true);,因为与Top Level Containers相比,默认情况下JComponentsvisible(true),因此您需要调用JFrame/JDialog/JWindow.setVisible(true);

      there no reason to call a.setVisible(true); or r.setVisible(true); for standard JComponents, because JComponents are visible(true) by default in comparing with Top Level Containers, there you need to call JFrame/JDialog/JWindow.setVisible(true);

      编辑

      (i used the very first suggestion you gave. problem remains the same.)-例如

      来自代码

      import java.awt.GridLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      
      public class MyGridLayout {
      
          private JFrame frame = new JFrame("GridLayout, JButtons, etc... ");
          private JPanel panel = new JPanel(new GridLayout(8, 8));
      
          public MyGridLayout() {
              for (int row = 0; row < 8; row++) {
                  for (int col = 0; col < 8; col++) {
                      JButton button = new JButton("(" + (row + 1) + " / " + (col + 1) + ")");
                      button.putClientProperty("column", col);
                      button.putClientProperty("row", row);
                      button.addActionListener(new ActionListener() {
                          @Override
                          public void actionPerformed(ActionEvent e) {
                              JButton btn = (JButton) e.getSource();
                              System.out.println(
                                      "clicked column : "
                                      + btn.getClientProperty("column")
                                      + ", row : " + btn.getClientProperty("row"));
                          }
                      });
                      panel.add(button);
                  }
              }
              frame.add(panel);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.pack();
              frame.setLocation(150, 150);
              frame.setVisible(true);
          }
      
          public static void main(String[] args) {
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      MyGridLayout myGridLayout = new MyGridLayout();
                  }
              });
          }
      }
      

      这篇关于如何在JFrame上设置JPanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆