Xin lỗi Im nếu điều này đã được đăng nhưng tôi đã xem xét kỹ lưỡng và tìm thấy các mã khác mà tôi không thể hiểu rõ. Tôi mới vào lập trình java và muốn ai đó chỉ cho tôi đúng hướng về cách tôi có thể thay đổi thành các bảng mong muốn bằng cách nhấn một nút. Bất cứ sự giúp đỡ nào cũng được đánh giá cao. Tôi chỉ đang cố gắng hoàn toàn hiểu được khái niệm này hoạt động như thế nào.Thay đổi Panels bằng cách sử dụng Bố cục thẻ
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; // Packages used in this program imported
public class onlineGame extends JFrame implements ActionListener
{
JPanel cards;
JButton button1, button2, button3;
public onlineGame() //This is the CONSTRUCTOR method
{
//The entry point into your program
setLayout(new FlowLayout()); //Use this for now.
setSize(810, 510); //Set the size of the JFrame
setTitle("Generic Card Game"); //Put Title on top of JFrame
setBackground(Color.yellow);
setResizable(false);
button1 = new JButton("THIS IS BUTTON 1");
button2 = new JButton("THIS IS BUTTON 2");
button3 = new JButton("THIS IS BUTTON 3");
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
//Create the cards
JPanel card1 = new JPanel();
card1.add(button1);
JPanel card2 = new JPanel();
card2.add(button2);
JPanel card3 = new JPanel();
card3.add(button3);
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1);
cards.add(card2);
cards.add(card3);
getContentPane().add(cards);
setVisible(true); //Make JFrame visible
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1){
//What do i put here to change to Panel card2 or card3 and so on.
}
}
public static void main(String args[])
{
new onlineGame(); // This calls the constructor and runs it
}
}
(Dành cho khách truy cập trong tương lai) Tài liệu [Oracle cho CardLayout] (https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) là khá dễ hiểu. – RustyTheBoyRobot
Tuyệt đối không có lý do để đóng này. Không có cách nào là một câu hỏi hẹp. Nó rộng như các câu hỏi đến. – Gary