2014-01-14 84 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

*“那么还有另一个***”*(笑);) –