2014-01-14 82 views
-1

我有一個名爲BoardGUI的類從JFrame擴展,在一個構造函數中,我做了一個帶有兩個按鈕的JPanel。我已將此面板添加到我的框架中。每當我運行這個程序時,按鈕都不可見。當我將鼠標光標放在按鈕上時,它們就會顯示出來。代碼如下:如何在JFrame上設置JPanel?

public class BoardGUI extends JFrame { 
Play pieces; 
JButton a=new JButton("Undo"); 
JButton r=new JButton("replay"); 
JPanel jp=new JPanel(); 

public BoardGUI() { 
    pieces = new Play(); 
    setTitle("Checkers Game"); 
    setSize(645, 700); 
    setVisible(true); 

    jp.setLayout(new FlowLayout()); 
    jp.setPreferredSize(new Dimension(645,35)); 
    a.setVisible(true); 
    r.setVisible(true); 
    jp.add(a); 
    jp.add(r); 
    add(jp,BorderLayout.SOUTH); 

我也在我的程序中使用重繪方法。任何人都可以指出我的錯誤,併爲此提出任何解決方案嗎?

+0

你能通過public static void main方法提供一個簡單的測試嗎? – PKopachevsky

+0

1)爲了更快地獲得更好的幫助,請發佈最近嘗試的[MCVE](http://stackoverflow.com/help/mcve)(而不是代碼片段)。 2)提供圖形用戶界面的ASCII藝術(或帶有簡單繪圖的圖像),因爲它應該以最小的尺寸出現並且(如果可調整大小)以額外的寬度/高度出現。 –

+0

至於佈局,你可能會從[這個國際象棋棋盤](http://stackoverflow.com/a/21096455/418556)得到一些想法。此[簡短示例](http://stackoverflow.com/a/16058759/418556)顯示如何將組件與BG圖像組合在一起。 –

回答

5

我有一個名爲BoardGUI的類從JFrame擴展,在構造函數中我做了一個帶有兩個按鈕的JPanel。我已將此面板 添加到我的框架中。每當我運行這個程序時,按鈕都不可見。作爲 我把我的鼠標光標放在他們可見的按鈕上。

  • setVisible(true);應在構造函數中最後一行代碼,因爲您添加JComponents到已經顯現JFrame

  • 或致電revalidate()repaint()的情況下JComponents被添加到可見的Swing GUI

  • 有沒有理由撥打a.setVisible(true);r.setVisible(true);標準JComponents,因爲JComponents默認與Top Level Containers比較有visible(true),有你需要調用JFrame/JDialog/JWindow.setVisible(true);

編輯

i used the very first suggestion you gave. problem remains the same.) - 例如

enter image description here

從代碼

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class MyGridLayout { 

    private JFrame frame = new JFrame("GridLayout, JButtons, etc... "); 
    private JPanel panel = new JPanel(new GridLayout(8, 8)); 

    public MyGridLayout() { 
     for (int row = 0; row < 8; row++) { 
      for (int col = 0; col < 8; col++) { 
       JButton button = new JButton("(" + (row + 1) + "/" + (col + 1) + ")"); 
       button.putClientProperty("column", col); 
       button.putClientProperty("row", row); 
       button.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         JButton btn = (JButton) e.getSource(); 
         System.out.println(
           "clicked column : " 
           + btn.getClientProperty("column") 
           + ", row : " + btn.getClientProperty("row")); 
        } 
       }); 
       panel.add(button); 
      } 
     } 
     frame.add(panel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocation(150, 150); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       MyGridLayout myGridLayout = new MyGridLayout(); 
      } 
     }); 
    } 
} 
+0

我用了你給的第一個建議。問題依然存在。 –

+1

然後還有另外一個***,爲了更快地發佈一個[MCVE](http://stackoverflow.com/help/mcve),更好的幫助,短的,可運行的,可編譯的局部變量的硬編碼值,參見[初始線程]( http://docs.oracle。com/javase/tutorial/uiswing/concurrency/initial.html)也很重要 – mKorbel

+1

*「那麼還有另一個***」*(笑);) –