2013-01-13 18 views
3

我想插入在附加圖像中以紅色圈起來的「益智遊戲」文本。需要關於如何在面板上插入小文本短語的想法

我已經在1 GridLayout中放置了4個藍色按鈕和textfields。

我試着將文本和GridLayout一起作爲一個不可點擊的按鈕插入,但它不起作用,因爲GridLayout中每個單元格的大小始終相同。

試圖使用setBounds,但它甚至不顯示在JFrame = /中。

image

// 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); 
+0

順便說一句:請不要使用setXXSize(http://stackoverflow.com/a/7229519/203657),曾經 – kleopatra

回答

7

試試這個:

panel1.setBorder(BorderFactory.createTitledBorder("~~Puzzle Game~~")); 
+0

謝謝好主意。 Spot on answer =) – iridescent

+0

不客氣:) –

相關問題