【问题标题】:Java, BorderLayout.CENTER, getting the width and height of the JPanelJava,BorderLayout.CENTER,获取JPanel的宽高
【发布时间】:2011-09-18 05:24:32
【问题描述】:

我正在使用 Swing 和 AWT(针对听众)制作一个小程序。我在获取 JPanel(名为 Chess 的类)的大小时遇到​​问题。 我的布局:

public class Main extends JFrame implements MouseListener, ActionListener{

    Chess chessPanel = new Chess ();
    JButton newGameButton = new JButton ("New Game");
    JButton loadGameButton = new JButton ("Load Game");
    JButton saveGameButton = new JButton ("Save Game");
    JButton exitButton = new JButton ("Exit");

    public static void main (String [] args) {
        new Main();
    }

    Main () {
        super ("Chess");
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(dim);
        setLocation(0,0);
        setUndecorated(true);

        chessPanel.addMouseListener(this);
        add(chessPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());


        newGameButton.addActionListener(this);
        loadGameButton.addActionListener(this);
        saveGameButton.addActionListener(this);
        exitButton.addActionListener(this);

        buttonPanel.add(newGameButton);
        buttonPanel.add(loadGameButton);
        buttonPanel.add(saveGameButton);
        buttonPanel.add(exitButton);

        add(buttonPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    // ... Code ...
}

从代码中可以看出,我在 CENTER 中有一个 JPanel,它几乎占据了整个屏幕。在底部我有另一个 JPanel (SOUTH),它有一排按钮。

我需要的是 CENTER 中的 JPanel 的大小。当我调用从 JPanel 继承的 getWidth()、getHeight() 或 getBounds() 方法时,由于 BorderLayout,它们都返回 0。 知道如何获得真正的价值吗?

PS:屏幕总是占据整个屏幕,并且永远不会调整大小,如果有帮助的话。

【问题讨论】:

    标签: java swing size jpanel


    【解决方案1】:

    您很可能在 JPanel 被渲染之前调用 getWidth,因此它将为 0。解决方案是在渲染之后获取大小 ,例如在 pack() 或 setVisible(true ) 已在包含此 JPanel 的根容器上调用。

    另外,我建议不要在任何事情上调用 setSize(),因为大多数标准布局管理器会观察组件的首选大小,而不是大小,并且当您调用 pack() 告诉布局管理器做他们的事情时,设置大小通常被忽略。如果需要一定大小,您可能希望通过覆盖其 setPreferredSize 方法来使位于中心的 JPanel 设置自己的大小。然后让 JFrame 及其持有的容器在调用 pack 时根据它们的布局管理器设置下注大小。

    例如,

    import java.awt.*;
    import javax.swing.*;
    
    public class Main extends JFrame {
    
       Chess chessPanel = new Chess();
       JButton newGameButton = new JButton("New Game");
       JButton loadGameButton = new JButton("Load Game");
       JButton saveGameButton = new JButton("Save Game");
       JButton exitButton = new JButton("Exit");
    
       public static void main(String[] args) {
          new Main();
       }
    
       Main() {
          super("Chess");
          add(chessPanel, BorderLayout.CENTER);
    
          JPanel buttonPanel = new JPanel();
          buttonPanel.setLayout(new FlowLayout());
    
          buttonPanel.add(newGameButton);
          buttonPanel.add(loadGameButton);
          buttonPanel.add(saveGameButton);
          buttonPanel.add(exitButton);
    
          System.out.printf("chessPanel Size before rendering: %s%n", chessPanel.getSize());
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          add(buttonPanel, BorderLayout.SOUTH);
          pack();
          System.out.printf("chessPanel Size after rendering: %s%n", chessPanel.getSize());
          setLocationRelativeTo(null);
          setVisible(true);
       }
    
       // ... Code ...
    }
    
    @SuppressWarnings("serial")
    class Chess extends JPanel {
       private static final int CHESS_WIDTH = 600;
       private static final int CHESS_HEIGHT = CHESS_WIDTH;
       private static final int MAX_ROW = 8;
       private static final int MAX_COL = 8;
       private static final Color LIGHT_COLOR = new Color(240, 190, 40);
       private static final Color DARK_COLOR = new Color(180, 50, 0);
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(CHESS_WIDTH, CHESS_HEIGHT);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          int panelWidth = getWidth();
          int panelHeight = getHeight();
          int sqrWidth = panelWidth / MAX_ROW;
          int sqrHeight = panelHeight / MAX_COL;
          for (int row = 0; row < MAX_ROW; row++) {
             for (int col = 0; col < MAX_COL; col++) {
                Color c = (row % 2 == col % 2) ? LIGHT_COLOR : DARK_COLOR;
                g.setColor(c);
                int x = (row * panelWidth) / MAX_ROW;
                int y = (col * panelHeight) / MAX_COL;
                g.fillRect(x, y, sqrWidth, sqrHeight);
             }
          }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-03
      • 2019-05-30
      • 2014-05-11
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      相关资源
      最近更新 更多