2012-07-10 67 views
2

我无法为我的课程生成用于战舰克隆的按钮数组,并且似乎无法弄清楚为什么它不起作用。任何建议将有助于...我有主类创建jFrame,然后网格类,更具体地说,生成器方法构建的按钮数组。使用按钮阵列调试JFrames

import java.awt.*; 

import javax.swing.*; 

public class warship { 

/** 
* @param args 
*/ 

public static void main(String[] args) { 
    JFrame gui = new JFrame(); 
    gui.setSize(700, 350); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    gui.setLayout(new FlowLayout()); 
    grid oceanGrid = new grid(); 
    oceanGrid.Generator(); 
    gui.add(oceanGrid); 
    gui.setVisible(true); 

} 

} 

grid.java

 import java.awt.Dimension; 
    import java.awt.GridLayout; 
    import java.awt.LayoutManager; 

    import javax.swing.ImageIcon; 
    import javax.swing.JButton; 
    import javax.swing.JPanel; 
    import javax.swing.border.TitledBorder; 


    @SuppressWarnings("serial") 
public class grid extends JPanel{ 
private static int rows = 7; 
private static int col = 10; 

public void Generator(){ 

    ImageIcon wIcon = new ImageIcon ("H:\\workspace\\Warship\\src\\images\\water.jpg"); 
    JPanel jPan1 = new JPanel(); 
    jPan1.setLayout((LayoutManager) new GridLayout(rows,col,1,1)); 
    jPan1.setSize(350,350); 

    //Set Border 
    TitledBorder bdr = javax.swing.BorderFactory.createTitledBorder(null, "Targeting Grid", 
      javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
      javax.swing.border.TitledBorder.DEFAULT_POSITION, 
      new java.awt.Font("Arial", 0, 16)); 
    bdr.setTitleColor(java.awt.Color.RED); 
    jPan1.setLayout((LayoutManager) new GridLayout(rows,col,1,1));  
    jPan1.setBorder(bdr); 

    //Creates the array of buttons 
    JButton b[]=new JButton[rows*col]; 
    for (int i = 0, j= rows*col; i < j; i++){ 
     b[i] = new JButton(wIcon); 
     b[i].setSize(20, 20); 
     b[i].setMaximumSize(new Dimension(20,20)); 
     b[i].setPreferredSize(new Dimension(20,20)); 
     System.out.println("loop test " + i); 
     jPan1.add(b[i]); 
    } 
} 
} 
+0

什么具体不工作? – 2012-07-10 18:38:41

+0

它没有显示由oceanGrid调用的jPanel的任何部分。不是按钮或边框。但它正在运行这个类,因为我在按钮的for循环中打印了一个命令行 – 2012-07-10 18:40:55

+2

不要忘记使用正确的Java命名约定:类应以大写字母开头,方法和变量以小写字母开头。当你要求陌生人帮助你处理你的代码时,这变得很重要:不遵守约定的代码很难让我们理解,使它更难以帮助你。 – 2012-07-10 18:59:46

回答

4

我觉得这是你在做的错误:
你的类网格扩展JPanel,但声明并初始化另一JPanel在其中添加按钮。所以你实际上并没有把按钮添加到你的网格中,而是添加到另一个你不使用的面板上。

的解决方法是删除此行

JPanel jPan1 = new JPanel(); 

this

更换的jPan1所有出现的这种方式,您将添加按钮,您的网格。

+1

1+:确切地说。原来的海报是向jPan1添加一些组件,但是随后将jPan1扔掉而不做任何事情。将它添加到'this' JPanel(并且不需要明确声明'this.')或将jPan1添加到某个东西。 – 2012-07-10 18:56:34

+0

非常感谢,我忽略了这一点。 – 2012-07-10 18:56:39

+0

啊我错过了我的概述。 – 2012-07-10 18:57:15

1

我相信你调用setVisible(真)之前对你丢失的包()命令。

+1

不包装()只是自动适合的大小?我已经设置了gui jFrame的大小。 .....但我试图添加包(),它不起作用:/ – 2012-07-10 18:54:14

+1

这是很好的建议,但不会解决原来的问题。 – 2012-07-10 18:58:40

1

1网格中的JPanel不需要。
2网格中的JPanel不会添加到使用.add()方法的任何内容中。
然而,似乎其他人已经得到这个。

如上所述,您应该删除行“JPanel jPan1 = new JPanel();”
并替换单词“jPan1”。用“this”一词。所有较小的情况。

这是您编辑的代码的正确缩进,或者至少是一个更容易阅读的代码。

import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.LayoutManager; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.border.TitledBorder; 


@SuppressWarnings("serial") 
public class grid extends JPanel{ 
    private static int rows = 7; 
    private static int col = 10; 

    public void Generator(){ 

     ImageIcon wIcon = new ImageIcon ("H:\\workspace\\Warship\\src\\images\\water.jpg"); 

     this.setLayout((LayoutManager) new GridLayout(rows,col,1,1)); 
     this.setSize(350,350); 

     //Set Border 
     TitledBorder bdr = javax.swing.BorderFactory.createTitledBorder(null,   "Targeting Grid", 
      javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
      javax.swing.border.TitledBorder.DEFAULT_POSITION, 
      new java.awt.Font("Arial", 0, 16)); 
     bdr.setTitleColor(java.awt.Color.RED); 

     this.setLayout((LayoutManager) new GridLayout(rows,col,1,1));  
     this.setBorder(bdr); 

    //Creates the array of buttons 
     JButton b[]=new JButton[rows*col]; 
     for (int i = 0, j= rows*col; i < j; i++){ 
      b[i] = new JButton(wIcon); 
      b[i].setSize(20, 20); 
      b[i].setMaximumSize(new Dimension(20,20)); 
      b[i].setPreferredSize(new Dimension(20,20)); 
      System.out.println("loop test " + i); 
       this.add(b[i]); 
     } 
    } 
} 

请注意,在此之后的任何事情至少是有帮助的批评风格。

我会在网格中使用一个构造函数,所以你不必调用该方法。就像这样:

public Generator(){ 
    super(); 

    //code in Generator() here. 
} 

,现在你不需要调用该方法“发电机()”

和这两条线

javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, 
javax.swing.border.TitledBorder.DEFAULT_POSITION, 

可能是这样的短。

TitledBorder.DEFAULT_JUSTIFICATION, 
TitledBorder.DEFAULT_POSITION,