代码之家  ›  专栏  ›  技术社区  ›  Sergei Podlipaev

当我试图在框架上添加多个组件时,显示白色框

  •  0
  • Sergei Podlipaev  · 技术社区  · 6 年前

    我正在开发JAVA Swing应用程序。我试图在主jframe上添加2个jpanel。

    我的主要JPanel课程:

    public class DrawingPanel extends JPanel {
        //mouse variables here
        //Point mPt = new Point(0,0);
    
        public DrawingPanel() {
            setBackground(COLOURBACK);
            MyMouseListener ml = new MyMouseListener();
            addMouseListener(ml);
        }
    
        public Dimension getPreferredSize() {
        return new Dimension(width, height);
        }
    ....
    ....
    ....
    }
    

    当我在主框架上添加为唯一组件时,工作正常。但当我试图在框架上添加另一个JPanel时,我的绘图面板显示为小的白色框。

    private void createAndShowGUI()
        {
            DrawingPanel panel = new DrawingPanel();
    
    
            JFrame frame = new JFrame("Hex Testing 4");
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            Container content = frame.getContentPane();
            JPanel aggregationFrame = new JPanel(new GridBagLayout());
            aggregationFrame.add(panel);
            aggregationFrame.add(new JLabel("Enter username:"));
            content.add(aggregationFrame);
            //this.add(panel);  -- cannot be done in a static context
            //for hexes in the FLAT orientation, the height of a 10x10 grid is 1.1764 * the width. (from h / (s+t))
            frame.setSize( (int)(SCRSIZE/1.23), SCRSIZE);
            frame.setResizable(false);
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    

    看起来像这样

    result

    我试图添加GETAPYRESSIZE()方法来绘制面板,正如建议的那样。 JPanel appears as a small white box 问题,但效果不好。

    你能帮我解决这个问题吗?

    更新:

    我已经改变了布局,现在我看到了一半的JPanel。我认为问题与JPanel和Jframe的固定大小有关。会调查的。

    update

    2 回复  |  直到 6 年前
        1
  •  1
  •   MadProgrammer    6 年前

    所以,我把你的断章取义的代码,拼凑成一个可运行的变体…没有问题。这表明问题出在你没有告诉我们的代码中。与其发布不完整代码的“片段”(这会引发更多问题),不如发布 Minimal, Complete, and Verifiable example 可以编译和运行,这说明了你所面临的问题。

    一个运行和工作的例子…

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Test {
    
      public static void main(String[] args) {
        new Test();
      }
    
      public Test() {
        EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
            DrawingPanel panel = new DrawingPanel();
    
            JFrame frame = new JFrame("Hex Testing 4");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container content = frame.getContentPane();
            JPanel aggregationFrame = new JPanel(new GridBagLayout());
            aggregationFrame.add(panel);
            aggregationFrame.add(new JLabel("Enter username:"));
            content.add(aggregationFrame);
            //this.add(panel);  -- cannot be done in a static context
            //for hexes in the FLAT orientation, the height of a 10x10 grid is 1.1764 * the width. (from h / (s+t))
            frame.pack();
            //frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
          }
        });
      }
    
      public class DrawingPanel extends JPanel {
        //mouse variables here
        //Point mPt = new Point(0,0);
    
        private int width = 200;
        private int height = 200;
    
        public DrawingPanel() {
          setBackground(Color.BLACK);
    //    MyMouseListener ml = new MyMouseListener();
    //    addMouseListener(ml);
        }
    
        public Dimension getPreferredSize() {
          return new Dimension(width, height);
        }
      }
    
    }
    
        2
  •  0
  •   Sergei Podlipaev    6 年前

    问题是我的组件大小。好像它没有安装在主框架上,被切断了。我设置了动态帧大小,现在它工作得很好。