我是JAVA編程新手,但是如何將變量/值從一個類A傳遞給seconnd類B,其中B是我的GUI,A是邏輯來顯示玩家?我需要在GUI中將播放器名稱打印到lblPlayer標籤。如何將值從一個類傳遞到另一個類GUI
A類:
package driverPkg;
import game.Card;
import game.CardsUI;
import game.players.CardPlayer;
public class GameConsole implements CardsUI {
public void currentCard(CardPlayer[] player) {
for (CardPlayer p : player) {
// here somehow return p.getName() and pass to the GUI
}
}
....more code not really need it
B類(GUI)
public class CardsGameGUI extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CardsGameGUI frame = new CardsGameGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CardsGameGUI() {
setFont(new Font("Arial", Font.PLAIN, 26));
setResizable(false);
setTitle("Card Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 828, 556);
contentPane = new JPanel();
contentPane.setBackground(new Color(102, 153, 153));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblPlayer = new JLabel("");//here i need to bring the players name
lblPlayer.setFont(new Font("Arial One", Font.PLAIN, 13));
lblPlayer.setBounds(6, 11, 49, 16);
panel.add(lblPlayer);
...more code below not need it now
感謝您的幫助
化妝B到創建一個實例和每個塗料獲得其狀態 – Gustavo
肯定,但任何想法,我該怎麼辦呢:( – medzi