Reputation: 39
I'm trying to create Pair Game. I have created 2 JPanels: first for game map (100 buttons) and second for statistics. The problem is that i have deployment of timer and counter like on left picture, but i want to have it like on right picture (i edited it in paint). What layout should i use? I tryed many solution but nothing worked for me. Also, if i set null manager JPanel dissapear
public class PairGame extends JFrame implements ActionListener{
JLabel counterLabel, timerLabel;
JMenuBar menuBar;
JMenu file, help;
JMenuItem fileNew, fileExit, helpAbout;
JPanel gamePanel, statisticsPanel;
JToggleButton buttons[];
ArrayList <Integer> values;
int temp=0, counter=0;
void createMenuBar()
{
file = new JMenu("File");
fileNew = new JMenuItem("New");
fileNew.addActionListener(this);
fileNew.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
fileExit = new JMenuItem("Exit");
fileExit.addActionListener(this);
fileExit.setAccelerator(KeyStroke.getKeyStroke("ctrl Q"));
file.add(fileNew);
file.add(fileExit);
file.setMnemonic('f');
help = new JMenu("Help");
helpAbout = new JMenuItem("About");
helpAbout.addActionListener(this);
helpAbout.setAccelerator(KeyStroke.getKeyStroke("ctrl H"));
help.add(helpAbout);
help.setMnemonic('h');
menuBar = new JMenuBar();
menuBar.add(file);
menuBar.add(help);
setJMenuBar(menuBar);
}
public void createGameMap()
{
gamePanel = new JPanel(new GridLayout(10,10));
values = new ArrayList<Integer>();
for(int i=1; i<=50; i++)
values.add(i);
for(int i=51; i<=100; i++)
values.add(i-50);
Collections.shuffle(values);
buttons = new JToggleButton[100];
for(int i=0; i<100; i++)
{
buttons[i] = new JToggleButton(""+(i+1));
buttons[i].setName(String.valueOf(values.toArray()[i]));
buttons[i].addActionListener(this);
gamePanel.add(buttons[i]);
}
gamePanel.setBounds(0,0,550,400);
add(gamePanel);
}
public void createStatisticsPanel()
{
statisticsPanel = new JPanel();
counterLabel = new JLabel("Counter: "+counter);
timerLabel = new JLabel("Timer: ");
statisticsPanel.add(counterLabel);
statisticsPanel.add(timerLabel);
statisticsPanel.setBounds(0,410,540,400);
add(statisticsPanel);
}
PairGame()
{
setTitle("Pair Game");
setSize(565,600);
setLocation(400,100);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createMenuBar();
createGameMap();
createStatisticsPanel();
setVisible(true);
}
public static void main(String[] args)
{
new PairGame();
}
Upvotes: 0
Views: 59
Reputation: 18812
The purpose of my answer is not only to demonstrate a possible solution, using layout managers as GhostCat suggested, but also demonstrated the idea of an MCVE to make it easier help and get help.
Please see comment for explanations:
//post imports
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class PairGame extends JFrame {
//removed fields to keep code as short as possible
JLabel counterLabel, timerLabel;
JPanel gamePanel, statisticsPanel;
//menu bar removed to keep the code as short as possible
//void createMenuBar()
public void createGameMap()
{
//simplify as much as possible
gamePanel = new JPanel(new GridLayout(1,1));
JToggleButton button = new JToggleButton("1");
gamePanel.add(button);
//use layout managers is preferred practice over setting bounds
add(gamePanel, BorderLayout.CENTER);
}
public void createStatisticsPanel()
{
counterLabel = new JLabel("Counter: ");
timerLabel = new JLabel("Timer: ");
statisticsPanel = new JPanel();
//use layout managers is preferred practice over setting bounds
//the layout you want can be achieved with various managers
//such as BorderLayout, BoxLayout, Gridlayout
//Implementation of BorderLayout
statisticsPanel.setLayout(new BorderLayout());
statisticsPanel.add(counterLabel, BorderLayout.NORTH);
statisticsPanel.add(timerLabel, BorderLayout.SOUTH);
//to test box layout REPLACE the 3 statements above with
//Implementation of BoxLayout
//BoxLayout boxLayout = new BoxLayout(statisticsPanel,BoxLayout.Y_AXIS);
//statisticsPanel.setLayout(boxLayout);
//statisticsPanel.add(counterLabel);
//statisticsPanel.add(timerLabel);
add(statisticsPanel, BorderLayout.SOUTH);
}
PairGame()
{
setTitle("Pair Game");
setSize(565,600);
setLocation(400,100);
//use layout managers is preferred practice over setting bounds
getContentPane().setLayout(new BorderLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
createGameMap();
createStatisticsPanel();
setVisible(true);
}
public static void main(String[] args)
{
new PairGame();
}
}
Don't hesitate to ask for clarifications as needed.
Upvotes: 2
Reputation: 140633
There is a whole variety of different layout managers that allow you to place your objects "in regards" to each other.
If you have a look at the second example for BoxLayout from Oracle ... surprise: that panel shows "two elements", the upper one being a left aligned label. Probably exactly what you are looking for.
Alternatively; right the first example for GridLayout has aligned labels, too.
Upvotes: 2