2016-11-25 1 views
2

私は3組の座席を作成するタスクを与えられており、各組は3行10列の座席を持っています。まず始めに、3つのセットごとに1つのシートを作成しました。レイアウトマネージャを使用して、スタイル(3x10)のシート(JButtons)を表示しようとしています。JButtonの行と列の作成

public class ConcertPanel extends JPanel 
{ 
    private JPanel Panel1; 
    private JButton goldSeat; 
    private JButton silverSeat; 
    private JButton bronzeSeat; 


    ConcertPanel() 
    { 
     Panel1 = new JPanel(); 
     Panel1.setSize(500,500); 

       setLayout(new GridLayout(rows, cols)); 
       for(int i = 0; i < rows; i++) 
       for(int j = 0; j < cols; j++) 
      { 
       goldSeat = new JButton(); 
       silverSeat = new JButton(); 
       bronzeSeat = new JButton(); 
       add(new JButton()); 
      } 
       this.add(Panel1); 

     } 
} 

彼らは、行または列に含まれていない、私はちょうどのJPanelで30個のボタンを得ている分、時:ここで私が持っているコードですか?ヘルプ:(

+1

あなたはgoldSeat、silverSeat、およびbronzeSeatを作成している、とあなたは彼らと何もしないしている - あなたはそれらを追加していませんGUIに。別のJButtonを作成し、それをGUIに追加します - なぜですか?これは意味をなさない。再度、JPanelを使用して空のJPanelをGridLayoutに追加します。なぜか?説明しようとしていることを説明してください。 –

+0

私は、ゴールドシート、シルバーシート、ブロンズシートのセットがある予約システムを作る必要があります。座席の色ごとに3つの行があり、各行は10席(30ゴールド席、シルバー30席、ブロンズ30席)です。座席はJButtonになります。ここでは座席のレイアウトを行と列にするようにしています。 – May

+1

しかし、JButtonを作成する場合は、 'add(...)'メソッドを呼び出すことによってConcertPanelに追加する必要があります。それ以外の場合は、ホイールを回転させるだけです - 意味がありますか? –

答えて

4

GridLayout tutorialで別の顔を持ってください。

  • あなたは一度コンテナにGridLayoutのを設定したい
  • のGridLayoutに追加するときは、第二パラメータなしであなたのコンポーネント(JButtonの)を追加します。
  • レイアウトが3×10の場合は、このコンテナに30個のコンポーネントを追加することをお勧めします。
  • JPanelを使用して別のJPanel、panel1をGridLayoutに追加しています。あなたはこれをやりたいとは思わない。

    for (int i = 0; i < rows; i++) 
         for (int j = 0; j < cols; j++) { 
          goldSeat = new JButton(); // button created but never added 
          silverSeat = new JButton(); // button created but never added 
          bronzeSeat = new JButton(); // button created but never added 
          add(new JButton()); // only button added 
         } 
        this.add(Panel1); // this throws off the whole GridLayout since it's added to the grid 
    

    は、このコードの問題のためのコメントを参照してくださいEDIT

    Example GUI

    import java.awt.GridLayout; 
    
    import javax.swing.*; 
    
    public class GridLayoutEg extends JPanel { 
        private static final int ROWS = 3; 
        private static final int COLS = 10; 
    
        public GridLayoutEg() { 
         setLayout(new GridLayout(ROWS, COLS)); // set JPanel's layout 
         for (int i = 0; i < ROWS; i++) { 
          for (int j = 0; j < COLS; j++) { 
           String text = String.format("[%d, %d]", j + 1, i + 1); 
           add(new JButton(text)); // add component w/o 2nd parameter 
          } 
         } 
        } 
    
        public static void main(String[] args) { 
         SwingUtilities.invokeLater(() -> createAndShowGui()); 
        } 
    
        private static void createAndShowGui() { 
         GridLayoutEg mainPanel = new GridLayoutEg(); 
         JFrame frame = new JFrame("GridLayoutEg"); 
         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
         frame.add(mainPanel); 
         frame.pack(); 
         frame.setLocationByPlatform(true); 
         frame.setVisible(true); 
        } 
    } 
    

    :たとえば

私たちのネストされたforループ内では、3つのJButtonを作成しますが、GUIに追加されることはないため、目的を果たせず、ガベージコレクションされます。その後、ボタンを作成して追加します。最後に、JPanelを作成しますが、グリッドをオフにするJPanelを使用してGridLayoutに追加します。このコードでより多くの楽しみを持っ


import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 

import javax.swing.*; 
import javax.swing.border.BevelBorder; 
import javax.swing.border.Border; 

public class GridLayoutEg extends JPanel { 
    private static final long serialVersionUID = 1L; 
    private static final int ROWS = 3; 
    private static final int COLS = 10; 

    public GridLayoutEg() { 
     int gap = 1; 
     int gap2 = 2; 
     setBorder(BorderFactory.createEmptyBorder(gap2, gap2, gap2, gap2)); 
     setLayout(new GridLayout(ROWS, COLS, gap, gap)); // set JPanel's layout 
     for (int row = 0; row < ROWS; row++) { 
      for (int col = 0; col < COLS; col++) { 
       JButton button = createSeat(row, col); 
       add(button); // add component w/o a 2nd parameter 
      } 
     } 
    } 

    private JButton createSeat(int row, int col) { 
     SeatColor seatColor = SeatColor.values()[row]; 
     SeatAction seatAction = new SeatAction(seatColor, col); 
     JButton button = new JButton(seatAction); 
     button.setBackground(seatColor.getColor()); 
     int topGap = 8; 
     int sideGap = 25; 
     Border innerBorder = BorderFactory.createEmptyBorder(topGap, sideGap, topGap, sideGap); 
     Border outerBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, seatColor.getColor().brighter(), 
       seatColor.getColor().darker()); 
     Border border = BorderFactory.createCompoundBorder(outerBorder, innerBorder); 
     button.setBorder(border); 
     return button; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 

    private static void createAndShowGui() { 
     GridLayoutEg mainPanel = new GridLayoutEg(); 
     JFrame frame = new JFrame("GridLayoutEg"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 
} 

class SeatAction extends AbstractAction { 
    private static final long serialVersionUID = 1L; 
    private SeatColor seatColor; 
    private int column; 

    public SeatAction(SeatColor seatColor, int column) { 
     super(String.format("[%d]", column)); 
     this.seatColor = seatColor; 
     this.column = column; 
     // putValue(LARGE_ICON_KEY, createIcon(seatColor, column)); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     AbstractButton btn = (AbstractButton) e.getSource(); 
     btn.setBackground(seatColor.getColor().brighter()); 
     setEnabled(false); 

     String text = String.format("Selected Seat Color: %s; Column: %d", seatColor, column); 
     System.out.println(text); 
    } 
} 

enum SeatColor { 
    GOLD("Gold", new Color(255, 215, 0)), 
    SILVER("Silver", new Color(192, 192, 192)), 
    BRONZE("Bronze", new Color(205, 127, 50)); 

    private String text; 
    private Color color; 

    private SeatColor(String text, Color color) { 
     this.text = text; 
     this.color = color; 
    } 

    public String getText() { 
     return text; 
    } 

    public Color getColor() { 
     return color; 
    } 

    @Override 
    public String toString() { 
     return getText(); 
    } 

} 
+0

あなたのコードをguidlineとして使ってみると、私は30個のJButtonを得ることができますが、それは私に行と列を渡すことはありません。これは私が今やったことです。私はコーディングがうまくいかないと言うことができます - setLayout(new GridLayout(rows、cols)); for(int i = 0; i <行; i ++) for(int j = 0; j May

+0

@May:こんにちは!残念ながら、コードはコメントにうまく表示されません。それでも問題が解決しない場合は、元の質問を編集して、新しいコードとその問題を下に表示してください。あなたが(4)のボタンをたくさん作成し、GUIに最後のものを追加するように見えますが、少し混乱しています。投稿を編集して明確にしてください。 –

関連する問題