2012-07-10 65 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,