我现在想画一个9*9的棋盘,棋盘用JPanel,gridLayout布局。
如果我调整frame大小,怎样让这个棋盘成比例缩放?1:1
或者应该用什么容器?怎样布局?多谢各位~~!!

解决方案 »

  1.   

    可以自动缩放阿,代码如下:
    public class Frame2 extends JFrame {
      JPanel jPanel1 = new JPanel();
      BorderLayout borderLayout1 = new BorderLayout();
      GridLayout gridLayout1 = new GridLayout(3,3);
      JButton jButton1 = new JButton();
      JButton jButton2 = new JButton();
      JButton jButton3 = new JButton();
      JButton jButton4 = new JButton();
      JButton jButton5 = new JButton();
      JButton jButton6 = new JButton();
      JButton jButton7 = new JButton();
      JButton jButton8 = new JButton();
      JButton jButton9 = new JButton();  public Frame2() {
        try {
          jbInit();
        }
        catch(Exception ex) {
          ex.printStackTrace();
        }
      }  void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        jPanel1.setLayout(gridLayout1);
        jButton1.setText("jButton1");
        jButton2.setText("jButton2");
        jButton3.setText("jButton3");
        jButton4.setText("jButton4");
        jButton5.setText("jButton5");
        jButton6.setText("jButton6");
        jButton7.setText("jButton7");
        jButton8.setText("jButton8");
        jButton9.setText("jButton9");
        this.getContentPane().add(jPanel1,  BorderLayout.CENTER);
        jPanel1.add(jButton1, null);
        jPanel1.add(jButton9, null);
        jPanel1.add(jButton8, null);
        jPanel1.add(jButton7, null);
        jPanel1.add(jButton6, null);
        jPanel1.add(jButton5, null);
        jPanel1.add(jButton4, null);
        jPanel1.add(jButton3, null);
        jPanel1.add(jButton2, null);
      }  public static void main(String[] args) {
        Frame2 frame2 = new Frame2();
        frame2.setSize(400,300);
        frame2.setVisible(true);
      }
    }