Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

function - Java Swing Can't Get Value Of Variable

Im trying to make a game with two diffrent frames. (I know its not effective) Anyways my problem is i can't get the value of my variable that i changed under actionListener function.

public void Pick_Char() {
    
    frame2 = new JFrame();
    frame2.setBounds(100, 100, 925, 805);
    frame2.setLocationRelativeTo(null);
    frame2.setResizable(false);
    frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    
    JPanel panel = new JPanel();
    
    btnNewButton = new JButton("G?zlüklü ?irin");
    btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
                        
            Main window = null;
            try {
                 
                window = new Main();
            } catch (IOException e1) {
                
                e1.printStackTrace();
            }

            flag1=true;
            window.frame2.setVisible(false);
            frame2.dispose();
            window.frame.setVisible(true);
            
            
            
        }
    });

public Main() throws IOException {
    Pick_Char();
        initialize();
        if(flag1 == true)
             GamePlay();
      
       
}

the variable i talk about is flag1. I declared that variable globally. What im trying to do is when i press the button, it will close that frame and open other frame (which is already working) and give me the flag1 value. I don't get any mistakes, but i can't get value of flag1. GamePlay function doesn't work. Please help me

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Introduction

I put together an example Swing GUI to show how a multi-player game can be created where each player has their own JPanel. A timer JPanel appears in between showing the player JPanels so you have time to switch seats.

Here's Player 1's JPanel.

Player 1 JPanel

Here's the timer JPanel, counting down.

Timer JPanel

Here's Player 2's JPanel.

Player 2 JPanel

I didn't just change the JLabel text. You're seeing two player JPanels in one JFrame.

Explanation

When I create a Swing GUI, I use the model / view / controller (MVC) pattern. This pattern allows me to separate my concerns and focus on one part of the Swing application at a time.

The MVC pattern using Java Swing works like this:

  1. The view reads information from the model.
  2. The view does not update the model.
  3. The controller updates the model and repaints / revalidates the view.

Usually, a Swing application has multiple controller classes, one for each action. This example has one ActionListener since all we're doing is switching player JPanels.

Model

This application has two model classes, Player and GameState. For now, the Player class holds a player name. All other game information about a player, like score, goes in this class.

The GameState class holds information about the game state. The List of Player instances tells us the number of players and the int playerTurn tells us whose turn it is.

View

This Swing GUI consists of a single JFrame, with a main JPanel using a CardLayout. Each player has their own JPanel. The JButton at the bottom of the player JPanel switches you to the next player.

Each player JPanel contains whatever information from the GameState class that a player needs to see to play the game. There can be other JButtons on the player JPanel that alter the state of the player. When the player finishes his turn, he clicks on the next turn JButton to pass the game to the next player.

Controller

Right now, the only ActionListener is the one that switches player JPanels. There's an internal ActionListener controlled by a Swing Timer that allows players to switch seats. You can adjust the countdown time in the outer ActionListener class. Other JButtons would trigger other ActionListeners.

Code

Here's the complete runnable example.

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class MultiPlayerGame implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MultiPlayerGame());
    }
    
    private CardLayout cardLayout;
    
    private GameState gameState;
    
    private JLabel timerLabel;
    
    private JPanel mainPanel;
    private JPanel timerPanel;
    
    private PlayerPanel[] playerPanels;
    
    public MultiPlayerGame() {
        this.gameState = new GameState();
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("MultiPlayer Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        this.mainPanel = createMainPanel();
        frame.add(mainPanel, BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        cardLayout =  new CardLayout();
        JPanel panel = new JPanel(cardLayout);
        
        List<Player> players = gameState.getPlayers();
        playerPanels = new PlayerPanel[players.size()];
        for (int i = 0; i < players.size(); i++) {
            Player player = players.get(i);
            playerPanels[i] = new PlayerPanel(this, gameState, 
                    player);
            panel.add(playerPanels[i].getPanel(), player.getName());
        }
        
        timerPanel = createTimerPanel();
        panel.add(timerPanel, "timer");
        
        return panel;
    }
    
    private JPanel createTimerPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        
        timerLabel = new JLabel(" ");
        timerLabel.setFont(panel.getFont().deriveFont(16f));
        panel.add(timerLabel);
        
        return panel;
    }
    
    public void updateTimerPanel(Player player, int seconds) {
        String text = "" + seconds + " seconds before " + 
                player.getName() + " may play";
        timerLabel.setText(text);
    }
    
    public CardLayout getCardLayout() {
        return cardLayout;
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public JPanel getTimerPanel() {
        return timerPanel;
    }

    public class PlayerPanel {
        
        private final MultiPlayerGame frame;
        
        private final GameState model;
        
        private final JPanel panel;

        public PlayerPanel(MultiPlayerGame frame, GameState model, Player player) {
            this.frame = frame;
            this.model = model;
            this.panel = createPlayerPanel(player);
        }
        
        private JPanel createPlayerPanel(Player player) {
            JPanel panel = new JPanel(new BorderLayout(5, 5));
            panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            panel.setPreferredSize(new Dimension(300, 200));
            
            JLabel label = new JLabel(player.getName());
            label.setFont(panel.getFont().deriveFont(Font.BOLD, 24f));
            label.setHorizontalAlignment(JLabel.CENTER);
            panel.add(label, BorderLayout.BEFORE_FIRST_LINE);
            
            JButton button = new JButton("Next Player's Turn");
            button.addActionListener(new ButtonListener(frame, model));
            panel.add(button, BorderLayout.AFTER_LAST_LINE);
            
            return panel;
        }

        public JPanel getPanel() {
            return panel;
        }
        
    }
    
    public class ButtonListener implements ActionListener {
        
        private final MultiPlayerGame frame;
        
        private final GameState model;
        
        private Timer timer;

        public ButtonListener(MultiPlayerGame frame, GameState model) {
            this.frame = frame;
            this.model = model;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            int delayPeriod = 30;
            int turn = model.nextPlayerTurn();
            Player player = model.getPlayers().get(turn);
            CardLayout cardLayout = frame.getCardLayout();
            timer = new Timer(1000, new ActionListener() {
                private int delay = delayPeriod;
                
                @Override
                public void actionPerformed(ActionEvent innerEvent) {
                    --delay;
                    frame.updateTimerPanel(player, delay);
                    if (delay < 0) {
                        cardLayout.show(frame.getMainPanel(), 
                                player.getName());
                        timer.stop();
                    }
                }
            });
            timer.start();
            frame.updateTimerPanel(player, delayPeriod);
            cardLayout.show(frame.getMainPanel(), "timer");
        }
        
    }
    
    public class GameState {
        
        private int playerTurn;
        
        private final List<Player> players;
        
        public GameState() {
            this.players = new ArrayList<>();
            this.players.add(new Player("Player 1"));
            this.players.add(new Player("Player 2"));
            this.playerTurn = 0;
        }
        
        public int nextPlayerTurn() {
            playerTurn = ++playerTurn % players.size();
            return playerTurn;
        }

        public int getPlayerTurn() {
            return playerTurn;
        }

        public List<Player> getPlayers() {
            return players;
        }
        
    }
    
    public class Player {
        
        private final String name;

        public Player(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
        
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...