我在框架顶部放置三个不同的面板时遇到了真正的麻烦,因为我需要在每个面板上具有不同的布局。我似乎无法让它工作,我已经连续尝试了 4 天。我在这段代码中找不到我哪里出错了。
我这样做是最好的方法吗?任何想法或帮助将不胜感激!!!!!!
我的代码:
public Mem() {
super("3 panels on 1 frame");
JFrame frame = new JFrame();
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
//Adding three different panels with borderlayout
frame.setLayout(new BorderLayout());
//Panel 1 is the Player Panel with labels
JLabel ply1 = new JLabel("Player 1");
JLabel ply2 = new JLabel("Player 2");
JLabel ply3 = new JLabel("Player 3");
JLabel ply4 = new JLabel("Player 4");
p1.add(ply1); p1.add(ply2); p1.add(ply3); p1.add(ply4);
//Panel 2 is the game panel with playingcards on it. (Clickable buttons)
JButton card1 = new JButton("Card");
p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1);
p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1); p2.add(card1);
//Panel 3 is the lower panel with the buttons new game & close on it.
JButton newGame = new JButton("New Game");
newGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
startGame();
}
});
JButton endGame = new JButton("Close");
endGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
closeGame();
}
});
p3.add(newGame);
p3.add(endGame);
frame.add(p1, BorderLayout.EAST);
frame.add(p2, BorderLayout.CENTER);
frame.add(p3, BorderLayout.SOUTH);
setVisible(true);
}