2016-07-16 5 views
2

目標は、すべての細胞が互いに独立しているセルのテーブルを表示することで、各セルには、いくつかのオプションが表示されます(内線、カックロボード、危険ボードを想像してみてください。)カードレイアウトを含むグリッドレイアウト - それはできますか?

をそれはスイングでの私の最初の試みですがあります。多くの読書の後、私は自分のアプローチは、カードレイアウト(この部分は私が満足している)を使用して独立して各セルを独自に設計し、それをグリッドレイアウトで「ラップ」することになると決めました。 これは、基本的なスイングにご理解の私の不足がその頭を上げるところ、おそらくです:各セルを設計するとき

私は、フレームを作成し、それにパネルを追加します。

import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 

    public class Cell extends Component{ 

    JFrame frame = new JFrame(); 

    /* we'll use a card layout with a panel for each look of the cell */ 
    CardLayout cardLayout = new CardLayout(); 
    JPanel containterPanel = new JPanel(); 
    JPanel firstPanel = new JPanel(); 
    JPanel secondPanel = new JPanel(); 
    JPanel thridpanel = new JPanel(); 

    public Cell(){ 

     /* assign the card layout to the container panel */ 
     containterPanel.setLayout(cardLayout); 

     /* assign objects to the different panels - details not important for the sake of the question */ 
     //.... 

     /* add the different panels to the container panel and show the initial one */ 
     containterPanel.add(firstPanel, "firstPanel"); 
     containterPanel.add(secondPanel, "secondPanel"); 
     containterPanel.add(thridpanel, "thridpanel"); 
     cardLayout.show(containterPanel, "firstPanel"); 

     /* add the container to the frame and display it*/ 
     frame.add(containterPanel); 
     frame.setVisible(true); 
    } 
} 

これはうまく動作します。 しかし、これは非常に不器用であるように、各セルの動作グリッドレイアウトでそれをラップする私の試み:私は何を得る

import java.awt.*; 


public class Board{ 

    private static final int COLUMNS_NUM = 3; 
    private static final int ROWS_NUM = 3; 

    JFrame frame = new JFrame(); 
    JPanel panel = new JPanel(); 
    Cell cells[] = new Cell[COLUMNS_NUM * ROWS_NUM]; 

    public Board(){ 

     panel.setLayout(new GridLayout(ROWS_NUM, COLUMNS_NUM)); 
     for (int i = 0; i <ROWS_NUM * COLUMNS_NUM; i++) 
     { 
      cells[i] = new Cell(); 
      panel.add(cells[i]); 
     } 

     frame.add(panel); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     new Board(); 
    } 

} 

は、メインボードフレームから分離し、無関係なフレームの束です。 明確に私はフレームを正しく処理していません(私は1つしか作成しませんか?)。 私が正しいアプローチに導いてくれれば助かります。

+0

1)カードのレイアウトを含むグリッドレイアウト - それはすることができますされていますか? "*はい。 2)すぐに助けを得るために、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。 –

+0

ありがとう@AndrewThompson、ここでは比較的新しい - 私は小さな編集をした、私は今それが '最小、完全かつ検証可能な'要件を保持していると信じています。そうでない場合は教えてください。 – yonikes

+0

ボードにセルを追加する場合は、JFrameではなくJPanelにしてください。 – c0der

答えて

1

細胞は、取締役会に追加する場合は、それらのJPanelではなく、JFrameの


例作る:

輸入java.awt.CardLayout。 import javax.swing.JLabel; import javax.swing.JPanel;

//make it a sub class of JPanel for easier implementation. 
public class Cell extends JPanel{ 

    public Cell(){ 

     JPanel firstPanel = new JPanel(); 
     //add a lable just so something is displayed 
     firstPanel.add(new JLabel(("Panel 1"))); 
     JPanel secondPanel = new JPanel(); 
     JPanel thridpanel = new JPanel(); 

     CardLayout cardLayout = new CardLayout(); 
     /* assign the card layout */ 
     setLayout(cardLayout); 

     /* add the different panels to the container panel and show the initial one */ 
     add(firstPanel, "firstPanel"); 
     add(secondPanel, "secondPanel"); 
     add(thridpanel, "thridpanel"); 
     cardLayout.show(this, "firstPanel"); 
    } 
} 

とセルを保持するボード:

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

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.WindowConstants; 


//make it a sub class of JFrame for easier implementation. 
public class Board extends JFrame{ 

    private static final int COLUMNS_NUM = 3; 
    private static final int ROWS_NUM = 3; 

    Cell cells[] = new Cell[COLUMNS_NUM * ROWS_NUM]; 

    public Board(){ 

     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     setPreferredSize(new Dimension(800, 800)); 

     JPanel panel = new JPanel(); 
     add(panel); //or better getContentPane().add(panel); 
     panel.setLayout(new GridLayout(ROWS_NUM, COLUMNS_NUM)); 

     for (int i = 0; i <(ROWS_NUM * COLUMNS_NUM); i++) 
     { 
      cells[i] = new Cell(); 
      panel.add(cells[i]); 
     } 

     pack(); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     new Board(); 
    } 
} 

それがどのように見えるかです: "*

enter image description here

+0

ありがとう、それは私を方向に導いた。 カードレイアウトには、親コンテナのない3つのパネルが含まれています。正しく理解していれば、そのようなコンテナパネルを持つことが、カード間のトランジションをサポートする正しい方法です。だから私がやったことはこれです: panel.add(cells [i] .containterPanel);これの代わりに を入力します。 panel.add(cells [i]); これはすべてです。つまり、私のバージョンではJpanelを拡張していないCell自体ではなく、コンテナパネルを追加するだけです。 – yonikes

+0

私はそれが助けてうれしいです。それがうまく構成されているのであなたは質問に投票しました。 – c0der

関連する問題