我正在尝试创建配对游戏。我创建了2个JPanels:首先是游戏地图(100个按钮),第二个是统计数据。问题是我在左侧图片上部署了计时器和计数器,但我希望在右图上(我在油漆中编辑它)。我应该使用什么布局?我尝试了很多解决方案,但没有任何对我有用。另外,如果我设置空管理器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();
}
答案 0 :(得分:2)
有各种不同的布局管理器允许您将对象“关注”到彼此。
如果你看一下来自Oracle的BoxLayout的第二个例子......惊喜:该面板显示“两个元素”,上面一个是左对齐标签。可能正是你想要的。
可替换地;正确GridLayout的第一个示例也有对齐标签。
答案 1 :(得分:2)
我的回答的目的不仅是为了演示一个可能的解决方案,使用布局管理器作为GhostCat建议,而且还展示了MCVE的想法,以便更容易帮助并获得帮助。
请参阅评论以获得解释:
//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();
}
}
根据需要,请不要犹豫要求澄清。