3

我正在尝试在所附图像中插入以红色圈出的“益智游戏”文本。

我已经将 4 个蓝色按钮和文本字段都放在了 1 个 GridLayout 中。

我尝试将文本与 GridLayout 一起作为不可点击的按钮插入,但它不起作用,因为 GridLayout 中每个单元格的大小始终相同。

也尝试使用 setBounds ,但它甚至没有出现在 JFrame =/ 中。

图片

    // create a new panel - panel1
    JPanel panel1 = new JPanel();   
    // set layout of panel1
    panel1.setLayout(new GridLayout(3,2));
    panel1.setPreferredSize(new Dimension(500,200));

    // create new buttons - button1 to button4
    JButton button1  = new JButton("Start Game");
    JButton button2  = new JButton("Get History");
    JButton button3  = new JButton("Reset Game");
    JButton button4  = new JButton("Exit Game");

    // create label and text field for entering of player's names 
    JLabel label1 = new JLabel("Enter Player's name:",JLabel.CENTER);
    JTextField field1 = new JTextField();

    // add the labels and text field to panel1
    panel1.add(label1);
    panel1.add(field1);

    // adds button1 to button4 to panel1
    panel1.add(button1);
    panel1.add(button2);
    panel1.add(button3);
    panel1.add(button4);

    // create a new general panel to contain all panels containing components placed at the bottom
    JPanel btmGenP = new JPanel();
    this.add(btmGenP,BorderLayout.SOUTH);
    btmGenP.add(panel1,FlowLayout.LEFT);

    // create a new panel - panel2
    JPanel panel2 = new JPanel();
    panel2.setLayout(null);
        panel2.setBounds(50,50,50,50);
        this.add(panel2);     

    // add Jlabel text to panel2
    JLabel puzgame = new JLabel("~~Puzzle Game~~");
    panel2.add(puzgame);
4

1 回答 1

7

试试这个:

panel1.setBorder(BorderFactory.createTitledBorder("~~Puzzle Game~~"));
于 2013-01-13T10:24:10.630 回答