目標は、すべての細胞が互いに独立しているセルのテーブルを表示することで、各セルには、いくつかのオプションが表示されます(内線、カックロボード、危険ボードを想像してみてください。)カードレイアウトを含むグリッドレイアウト - それはできますか?
をそれはスイングでの私の最初の試みですがあります。多くの読書の後、私は自分のアプローチは、カードレイアウト(この部分は私が満足している)を使用して独立して各セルを独自に設計し、それをグリッドレイアウトで「ラップ」することになると決めました。 これは、基本的なスイングにご理解の私の不足がその頭を上げるところ、おそらくです:各セルを設計するとき
私は、フレームを作成し、それにパネルを追加します。
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つしか作成しませんか?)。 私が正しいアプローチに導いてくれれば助かります。
1)カードのレイアウトを含むグリッドレイアウト - それはすることができますされていますか? "*はい。 2)すぐに助けを得るために、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。 –
ありがとう@AndrewThompson、ここでは比較的新しい - 私は小さな編集をした、私は今それが '最小、完全かつ検証可能な'要件を保持していると信じています。そうでない場合は教えてください。 – yonikes
ボードにセルを追加する場合は、JFrameではなくJPanelにしてください。 – c0der