网格布局和边框布局

时间:2017-09-04 18:50:34

标签: java jpanel grid-layout border-layout

所以我想在边框布局的左上角创建一个开始按钮,以及由9个标签组成的3x3网格布局。必须看起来像The image below

但是,我无法这样做。这是我的代码BTW

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.*;

public class Something extends JFrame{

    public static void main(String[] args)
    {

        new Something();
    }


public Something(){
    super("Whack-a-mole");

    JPanel panel1 = new JPanel();
    panel1.setLayout(new BorderLayout());
    getContentPane().add(panel1);

    JButton start = new JButton("Start");
    start.setPreferredSize(new Dimension(5, 30));
    panel1.add(start,BorderLayout.BEFORE_FIRST_LINE);

    JPanel panel2 = new JPanel();
    panel2.setLayout(new GridLayout(3,3));
    add(panel2.AFTER_LAST_LINE);


    for(int x=1;x<10;x++)
    {
        JLabel labl  = new JLabel("lab"+x);
        panel2.add(labl);
    }



    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);



}

}

0 个答案:

没有答案