2016-03-27 30 views
0

こんにちはこれは私の具体的な問題です。 forループを使用して1つのパネルに1つのボタンを追加しようとしました。JButtonがJPanel上にある必要があります。

これはJButtonを作成するためのループです。

nizButtona=new JButton[22]; 
for(int i=0;i<nizButtona.length;i++){ 

    nizButtona[i] = new JButton(); 
    if(i==0){ 
    nizButtona[i].setText("Započni kviz"); //Start quiz 
    nizButtona[i].addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e){ 
      cl.next(nizPanela[1]); 
     } 
    }); 
    }else if(i==1){ 
     nizButtona[i].setText("Izlaz"); //Quit 
     nizButtona[i].addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e){ 
       System.exit(0); 
      } 
     }); 
     }else if(i<12){ 
     nizButtona[i].setText("Sledeće pitanje"); //Next question, on next panel 
     nizButtona[i].addActionListener(new ActionListener(){ 
       @Override 
       public void actionPerformed(ActionEvent e){ 
        cl.next(nizPanela[1]); 
       } 
      }); 
    } 

これはパネルにボタンを追加するための新しいループです。ここで、nizButtona [i-1]はi-1です。なぜなら、次の質問の最初のボタンは、JPanelよりも1つの引数が必要なためです。そして、すべてのコンポーネントのGridBagLayoutを使用するので、すべてのパネルを同じ場所に配置します。それがなければ問題は同じです。

 for(int i=3;i<=11;i++){ 
     nizPanela[i].add(nizButtona[i-1]); 
    } 

ここで私はJPanelの配列を作成しました。

nizPanela = new JPanel [13];

for (int i=0;i<nizPanela.length;i++){ 

     nizPanela[i] = new JPanel(); 

     if(i<=1){ 
     okvir.getContentPane().add(nizPanela[i]);//Does i real need this getContentPane? 
     }else{ 
      nizPanela[i].setLayout(new GridBagLayout()); 
      nizPanela[1].add(nizPanela[i], String.valueOf(i)); 
     } 

    } 
    cl=new CardLayout(); 

    nizPanela[1].setLayout(cl); 

    cl.show(nizPanela[1],"2"); 

これは、プログラムがSledećeがこのパネルに表示さpitanjeが、それはする必要がありませんphotoボタンを見てどのようです。これは、マウスポインタをこのボタンの場所に移動すると表示されます。

+0

ちょうどあなたのコードを貼り付けないでください。これをデバッグするためにどのような手順を取っていますか?コードのどの部分が機能していないかを正確に特定し、関連するスニペットをポストするだけですか? –

+0

私は構文エラーがないので問題は論理的なので、私のコードからすべての行を投稿する方が良いと思います。問題は、私はフレーム上のJButtonを見ることができません。 – GlacialMan

+0

しかし、関連性があると思われるコードを投稿するのがベストプラクティスです。どのコードが重要なのかわからなくても、コードを別のブロックに分割し、コードブロック間でそれぞれの目的を説明する必要があります。 –

答えて

2

setLayout(null)の代わりに、layoutsを使用する方法を学んでください。以下の例では、一連のnested layoutsを使用して別のグリッド内に1つのグリッドを追加しています。

image

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 

/** 
* @see https://stackoverflow.com/a/36243395/230513 
*/ 
public class Test { 

    private static final int ROW = 2; 
    private static final int COL = 5; 

    private void display() { 
     JFrame f = new JFrame("Test"); 
     f.setLayout(new GridLayout(0, 1)); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel top = new JPanel(new GridBagLayout()); 
     top.setBackground(Color.darkGray); 
     JLabel label = new JLabel("Post no bills."); 
     label.setForeground(Color.yellow); 
     top.add(label); 
     f.add(top); 
     f.add(createGridPanel()); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private JPanel createGridPanel() { 
     JPanel p = new JPanel(new GridLayout(ROW, COL, 5, 5)); 
     p.setBorder(BorderFactory.createLineBorder(Color.yellow,5)); 
     p.setBackground(Color.yellow); 
     for (int r = 0; r < ROW; r++) { 
      for (int c = 0; c < COL; c++) { 
       p.add(createSubPanel()); 
      } 
     } 
     return p; 
    } 

    private JPanel createSubPanel() { 
     JPanel p = new JPanel(new GridLayout(0, 1)); 
     JPanel top = new JPanel(); 
     top.add(new JButton("One")); 
     top.add(new JButton("Two")); 
     JPanel bot = new JPanel(); 
     bot.add(new JRadioButton("A")); 
     bot.add(new JRadioButton("B")); 
     bot.add(new JRadioButton("C")); 
     bot.add(new JRadioButton("D")); 
     p.add(top); 
     p.add(bot); 
     return p; 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Test()::display); 
    } 
} 

補遺: "私はしたい...次回、バックには2つのJButton S"

CardLayoutをボタンを使用して別のパネルに移動できるようにするには、hereと表示されています。

image

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 

/** 
* @see https://stackoverflow.com/a/36243395/230513 
*/ 
public class CardPanel extends JPanel { 

    private static final JPanel cards = new JPanel(new CardLayout()); 
    private final String name; 

    public CardPanel(String name) { 
     super(new GridLayout(0, 1)); 
     this.name = name; 
     JPanel top = new JPanel(new GridBagLayout()); 
     top.setBackground(Color.darkGray); 
     JLabel label = new JLabel(name); 
     label.setForeground(Color.yellow); 
     top.add(label); 
     JPanel bot = new JPanel(); 
     bot.setBorder(BorderFactory.createLineBorder(Color.yellow, 5)); 
     bot.add(new JRadioButton("A")); 
     bot.add(new JRadioButton("B")); 
     bot.add(new JRadioButton("C")); 
     bot.add(new JRadioButton("D")); 
     this.add(top); 
     this.add(bot); 
    } 

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

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       create(); 
      } 
     }); 
    } 

    private static void create() { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     for (int i = 1; i < 9; i++) { 
      CardPanel p = new CardPanel("Panel " + String.valueOf(i)); 
      cards.add(p, p.toString()); 
     } 
     JPanel control = new JPanel(); 
     control.add(new JButton(new AbstractAction("\u22b2Prev") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       CardLayout cl = (CardLayout) cards.getLayout(); 
       cl.previous(cards); 
      } 
     })); 
     control.add(new JButton(new AbstractAction("Next\u22b3") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       CardLayout cl = (CardLayout) cards.getLayout(); 
       cl.next(cards); 
      } 
     })); 
     f.add(cards, BorderLayout.CENTER); 
     f.add(control, BorderLayout.SOUTH); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 
} 
+0

['GridButtonPanel'](http://stackoverflow.com/a/7706684/230513)も参照してください。 – trashgod

+0

ああ、あなたは私を理解していませんでした。私は1つのパネルと最後(南のパネル)を表示したいので、exp 1つのラベル - 質問のため、4ラジオボタンは答えを、2つはJButtonを次に返します。だから誰かが次のボタンをクリックすると、質問用の別のラベル、別の回答、次と戻るボタンを持つ別のパネルが表示されます。私は、テキストをクリックしたときに1つずつ表示される10個のパネルを持っていたい。私はあなたが私を理解することを願っています:) – GlacialMan

+0

おかげで多分それは私を助けるでしょう:) – GlacialMan

関連する問題