0

现在我正在做一个石头剪刀布项目。

这很简单,但我是 Java 新手,并且在游戏的一些方面苦苦挣扎。

我的基本游戏一切正常,但我正在尝试添加一个得分系统,它显示在顶角或底角,每次你赢了你的分数都会上升,和电脑一样,我没有不过,真的决定把它们放在哪里。

我使用JLabels 作为数字并且我一直在尝试一些方法,但一些失败的方法仍然存在。

我一直在使用整数到字符串作为标签。我在有人得分时更新数字和定位JLabels 时都遇到了麻烦。

到目前为止,与分数有关的大部分内容都在代码底部附近的 score 方法中,并且大多数评分事物的变量是yourNum, yourScore, compScore, compNum.

对于定位问题,我希望使用绝对定位,但我不完全理解如何将它与我的 一起使用JLabel,所以如果有人能解释如何在我的情况下使用它,那就太好了。这是我的完整代码。

public class main extends JFrame implements ActionListener {

    JButton rock;
    JButton scissors;
    JButton paper;
    JLabel left;
    ImageIcon rock1;
    ImageIcon scissors1;
    ImageIcon scissors2;
    ImageIcon paper2;
    ImageIcon paper1;
    ImageIcon rock2;
    ImageIcon back;
    ImageIcon scissorsb;
    JLabel right;
    String[] RPS = {"Rock", "Paper", "Scissors"};
    Random rand = new Random();
    int num = rand.nextInt(RPS.length);
    int yourChoice = -1;
    JTextField texter;
    JLabel winner;
    Container contentPane = this.getContentPane();
    SpringLayout layout = new SpringLayout();
    String Tie = "Tie";
    String compwins = "Computer wins!";
    String youwin = "You win!";
    int yourNum = 9;
    JLabel yourScore;
    int compNum = 0;
    JLabel compScore;
    Font winnerFont = new Font("Font", Font.PLAIN, 35);

    public main() {
        super("Rock Paper Scissors");
        back = new ImageIcon("image1233450665506.png");
        paper1 = new ImageIcon("paper1.png");
        paper2 = new ImageIcon("paper2.png");
        scissors1 = new ImageIcon("scissors1.png");
        scissors2 = new ImageIcon("scissors2.png");
        rock1 = new ImageIcon("rock1.png");
        rock2 = new ImageIcon("rock2.png");
        JLabel background = new JLabel(back);
        left = new JLabel();
        right = new JLabel();
        winner = new JLabel();
        yourScore = new JLabel();
        compScore = new JLabel();
        right.setVisible(true);
        this.setVisible(true);
        this.setSize(1280, 985);
        winner.setFont(winnerFont);
        winner.setHorizontalAlignment(SwingConstants.CENTER);
        yourScore.setFont(winnerFont);
        compScore.setFont(winnerFont);
        contentPane.setLayout(layout);
        contentPane.add(winner);
        contentPane.add(left);
        contentPane.add(right);
        contentPane.add(yourScore);
        contentPane.add(compScore);
        layout.putConstraint(SpringLayout.NORTH, left, 300, SpringLayout.NORTH, contentPane);
        layout.putConstraint(SpringLayout.NORTH, right, 300, SpringLayout.NORTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, right, 0, SpringLayout.EAST, contentPane);
        rock = new JButton("Rock");
        paper = new JButton("Paper");
        scissors = new JButton("Scissors");
        layout.putConstraint(SpringLayout.WEST, rock, 500, SpringLayout.WEST, contentPane);
        this.add(rock, BorderLayout.NORTH);
        layout.putConstraint(SpringLayout.SOUTH, rock, -30, SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.WEST, rock, 40, SpringLayout.NORTH, contentPane);
        this.add(paper, BorderLayout.NORTH);
        layout.putConstraint(SpringLayout.SOUTH, paper, -30, SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.WEST, paper, 200, SpringLayout.NORTH, contentPane);
        this.add(scissors, BorderLayout.NORTH);
        layout.putConstraint(SpringLayout.SOUTH, scissors, -30, SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.WEST, scissors, 360, SpringLayout.NORTH, contentPane);
        rock.setBackground(Color.GRAY);
        rock.setPreferredSize(new Dimension(140, 50));
        paper.setBackground(Color.WHITE);
        paper.setPreferredSize(new Dimension(140, 50));
        scissors.setBackground(Color.LIGHT_GRAY);
        scissors.setPreferredSize(new Dimension(140, 50));
        rock.repaint();
        scissors.repaint();
        paper.repaint();
        contentPane.add(background);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        rock.doClick();
        paper.doClick();
        scissors.doClick();
        rock.addActionListener(this);
        paper.addActionListener(this);
        scissors.addActionListener(this);

    }

    public static void main(String args[]) {
        main framer = new main();
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        if (arg0.getSource() == rock) {
            left.setIcon(rock1);
            yourChoice = 0;
        } else if (arg0.getSource() == paper) {
            left.setIcon(paper1);
            yourChoice = 1;
        } else if (arg0.getSource() == scissors) {
            left.setIcon(scissors1);
            yourChoice = 2;
        }
        computerMove();
        Score();
    }

    public void Score() {
        layout.putConstraint(SpringLayout.NORTH, winner, 50, SpringLayout.NORTH, contentPane);
        layout.putConstraint(SpringLayout.WEST, winner, 560, SpringLayout.WEST, contentPane);
        layout.putConstraint(SpringLayout.SOUTH, compScore, 100, SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, compScore, 600, SpringLayout.EAST, contentPane);
        yourScore = new JLabel(Integer.toString(yourNum));
        compScore = new JLabel(Integer.toString(compNum));

        if (num == 0 && yourChoice == 0) {
            winner.setText(Tie);
            winner.repaint();
        } else if (num == 1 && yourChoice == 0) {
            yourNum++;
            yourScore.repaint();
            winner.setText(compwins);
            winner.repaint();
        } else if (num == 2 && yourChoice == 0) {
            compNum++;
            compScore.repaint();
            winner.setText(youwin);
            winner.repaint();
        } else if (num == 0 && yourChoice == 1) {
            yourNum++;
            yourScore.repaint();
            winner.setText(youwin);
            winner.repaint();
        } else if (num == 1 && yourChoice == 1) {
            winner.setText(Tie);
            winner.repaint();
        } else if (num == 2 && yourChoice == 1) {
            compNum++;
            compScore.repaint();
            winner.setText(compwins);
            winner.repaint();
        } else if (num == 0 && yourChoice == 2) {
            compNum++;
            compScore.repaint();
            winner.setText(compwins);
            winner.repaint();
        } else if (num == 1 && yourChoice == 2) {
            yourNum++;
            yourScore.repaint();
            winner.setText(youwin);
            winner.repaint();
        } else if (num == 2 && yourChoice == 2) {
            winner.setText(Tie);
            winner.repaint();
        }
        winner.repaint();
        yourScore.repaint();
        compScore.repaint();
        yourScore.setForeground(Color.RED);
        compScore.setForeground(Color.RED);
        winner.setForeground(Color.RED);
    }

    public void computerMove() {
        num = rand.nextInt(RPS.length);
        if (num == 0) {
            right.setIcon(rock2);
        } else if (num == 1) {
            right.setIcon(paper2);
        } else if (num == 2) {
            right.setIcon(scissors2);
        }
    }
}
4

2 回答 2

0

我会怀着热情避免绝对布局。

您不仅需要监视每个组件的更改以查看它何时可能更改大小,还需要考虑这些更改如何影响其他组件并监视对父容器的更改......甚至不要让我开始了解平台、字体、屏幕分辨率之间的差异......

既然 Swing 已经为您完成了所有这些,为什么还要浪费您的时间...

我会使用复合布局来包含各个感兴趣的区域。

例如,我将所有播放器控件放入面板中,将任何计算机输出放入它自己的面板中,并将乐谱放入自己的面板中。

这将责任分离到每个容器,允许您为每个部分使用不同的布局,而无需花时间尝试让它们通过单个布局对齐。

现在这只是我的看法,您可以做很多其他事情并从许多不同的方式解决问题......

在此处输入图像描述

public class RPS {

    public static void main(String[] args) {
        new RPS();
    }

    public RPS() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new RockPaperScissors());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class RockPaperScissors extends JPanel implements ActionListener {

        public static final String[] NAMES = {"Rock", "Paper", "Scissors"};

        public static final int ROCK = 0;
        public static final int PAPER = 1;
        public static final int SCISSORS = 2;

        private JButton rockButton;
        private JButton paperButton;
        private JButton scissorsButton;
        private JLabel playerChoice;
        private JLabel computerChoice;

        private JLabel playerScore;
        private JLabel computerScore;
        private JLabel winner;

        private int playerScoreValue = 0;
        private int computerScoreValue = 0;

        public RockPaperScissors() {

            JPanel playerPane = new JPanel(new GridBagLayout());
            rockButton = new JButton("Rock");
            rockButton.setActionCommand("rock");
            paperButton = new JButton("Paper");
            paperButton.setActionCommand("paper");
            scissorsButton = new JButton("Scissors");
            scissorsButton.setActionCommand("scissors");

            rockButton.addActionListener(this);
            paperButton.addActionListener(this);
            scissorsButton.addActionListener(this);

            playerPane.setBorder(new TitledBorder("Player"));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.CENTER;
            playerPane.add(rockButton);
            gbc.gridx++;
            playerPane.add(paperButton);
            gbc.gridx++;
            playerPane.add(scissorsButton);

            playerChoice = new JLabel(" ");
            gbc.gridx = 0;
            gbc.gridy = 1;
//            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.gridwidth = 3;
            gbc.insets = new Insets(12, 12, 12, 12);
            playerPane.add(playerChoice, gbc);

            computerChoice = new JLabel(" ");
            computerChoice.setHorizontalAlignment(JLabel.CENTER);
            JPanel computerPane = new JPanel(new BorderLayout());
            computerPane.setBorder(new TitledBorder("Computer"));
            computerPane.add(computerChoice);

            JPanel competitorsPane = new JPanel(new GridLayout(1, 2));
            competitorsPane.add(playerPane);
            competitorsPane.add(computerPane);

            JPanel scoresPane = new JPanel(new GridLayout(1, 3));
            scoresPane.setBorder(new TitledBorder("Scores"));
            playerScore = new JLabel("0");
            playerScore.setHorizontalAlignment(JLabel.CENTER);
            computerScore = new JLabel("0");
            computerScore.setHorizontalAlignment(JLabel.CENTER);
            winner = new JLabel("");
            winner.setHorizontalAlignment(JLabel.CENTER);
            scoresPane.add(playerScore);
            scoresPane.add(winner);
            scoresPane.add(computerScore);

            setLayout(new BorderLayout());
            add(competitorsPane);
            add(scoresPane, BorderLayout.SOUTH);

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            JButton choice = (JButton) e.getSource();
            playerChoice.setText(choice.getText());

            int playerValue = -1;
            if (playerChoice.getText().equalsIgnoreCase("Rock")) {
                playerValue = 0;
            } else if (playerChoice.getText().equalsIgnoreCase("Paper")) {
                playerValue = 1;
            } else if (playerChoice.getText().equalsIgnoreCase("Scissors")) {
                playerValue = 2;
            }

            int myChoice = (int)Math.round(Math.random() * 2);
            computerChoice.setText(NAMES[myChoice]);

            String whoWon = "Computer wins";
            if (playerValue == myChoice) {
                whoWon = "Draw";
            } else if ((playerValue == ROCK && myChoice != PAPER) ||
                    (playerValue == PAPER && myChoice != SCISSORS) ||
                    (playerValue == SCISSORS && myChoice != ROCK)) {
                whoWon = "Player Wins";
                playerScoreValue++;
            } else {
                computerScoreValue++;
            }

            winner.setText(whoWon);
            playerScore.setText(Integer.toString(playerScoreValue));
            computerScore.setText(Integer.toString(computerScoreValue));

        }
    }
}
于 2012-10-16T00:46:45.207 回答
0

首先,我讨厌绝对定位。

如果要使用绝对定位,则必须将父容器的布局设置为 null(可以是 JFrame 或任何您想要的)。

然后你可以添加一个新的 JLabel 并使用 setLocation() 方法定位它。

顺便说一句,我认为您的代码存在一些问题,可能是复制粘贴问题。如果您想发布某些内容并有人阅读,请尝试发布SSCCE

但我总是建议使用 BoxLayout 之类的东西。请看一下我对图形感兴趣的这个答案:

组件的定位(如何将几个按钮放置在相同大小的屏幕中心)

Jframe 执行时大小不合适,有什么原因吗?

于 2012-10-15T23:43:20.460 回答