且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JFrame上设置JPanel?

更新时间:2023-12-05 09:00:16

我在构造函数中有一个从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();
                  }
              });
          }
      }