Hi this is my concrete problem. I was tried to add one button to one panel with for loop.
This is for loop for creating JButtons.
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]);
}
});
}
This is new loop for adding buttons on panels. Here nizButtona[i-1] is i-1 because first button for next question have for 1 argument than JPanel where it's need to be add, and you GridBagLayout for all components so i will put all on the same location for each panel. Without it the problem is the same.
for(int i=3;i<=11;i++){
nizPanela[i].add(nizButtona[i-1]);
}
Here is how i was create array for JPanels.
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");
This is how program look photo Button Sledeće pitanje visible on this panel but it don't should be. It's only visible if i move mouse pointer through place of this button.
Instead of
setLayout(null)
, learn to use layouts to your advantage. The example below uses a series of nested layouts to add a one grid inside another.Addendum: "I want … two
JButton
s for next and back."To permit navigation by button from one panel to another, use
CardLayout
, shown here and revised below.