我真的有麻煩把三個不同的面板放在一個框架上,因爲我需要在每個面板上有不同的佈局。我似乎無法讓它工作,現在我一直在嘗試連續4天。我無法找到我在這個代碼中出錯的地方。3個面板ontop 1個框架
我正在做這個最好的方法?任何想法或幫助將不勝感激!!!!!
我的代碼:
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);
}
@ user1501127您還沒有告訴我們它出了什麼問題。你期待它做什麼以及它對當前代碼做了什麼?使用'setLayout'將佈局放在面板 – WilliamShatner