如何让button按钮知道我选择的是哪一个角色

用目前会的java知识写一个个人向的小东西练练手熟悉熟悉,写的可能有点乱。
目前就是想让左下角的”确定“按钮知道我选择的是哪一个角色进入游戏

代码如下:


package gamedemo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class role extends JFrame {
    public void newrole (){


        JFrame role = new JFrame("选择角色");
        role.setSize(700,500);
        role.setLocationRelativeTo(null);
        role.setDefaultCloseOperation(EXIT_ON_CLOSE);
        role.setLayout(null);

        JLabel rolename = new JLabel("");
        rolename.setFont(new Font("宋体",Font.PLAIN,16));
        role.add(rolename);

        //设置角色大图显示位置
        JLabel datu = new JLabel(new ImageIcon(""));
        datu.setBounds(20,50,250,330);
        role.add(datu);

        //战士图片按钮设置
        JButton warriorbt = new JButton(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                datu.setIcon(new ImageIcon("src\\img\\datu\\warrior.jpg"));
                rolename.setBounds(105,370,210,50);
                rolename.setText("不知名战士");
            }
        });
        ImageIcon warrico = new ImageIcon("src\\img\\xiaotu\\warrior_1.jpg");
        warriorbt.setIcon(warrico);
        warriorbt.setBounds(300,280,110,140);
        role.add(warriorbt);
        JLabel warriorlb = new JLabel("战士");
        warriorlb.setBounds(340,410,50,50);
        warriorlb.setFont(new Font("宋体",Font.PLAIN,16));
        role.add(warriorlb);


        //射手图片按钮设置
        JButton shooterbt = new JButton(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                datu.setIcon(new ImageIcon("src\\img\\datu\\shooter.jpg"));
                rolename.setBounds(105,370,210,50);
                rolename.setText("不知名射手");
            }
        });
        ImageIcon shooterico = new ImageIcon("src\\img\\xiaotu\\shooter_1.jpg");
        shooterbt.setIcon(shooterico);
        shooterbt.setBounds(425,280,110,140);
        role.add(shooterbt);
        JLabel shooterlb = new JLabel("射手");
        shooterlb.setBounds(465,410,50,50);
        shooterlb.setFont(new Font("宋体",Font.PLAIN,16));
        role.add(shooterlb);


        //魔法师图片按钮设置
        JButton magicianbt = new JButton(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JLabel name = new JLabel();
                datu.setIcon(new ImageIcon("src\\img\\datu\\magician.jpg"));
                rolename.setBounds(80,380,210,50);
                rolename.setText("<html>&nbsp&nbsp奇异博士<br/>史蒂芬·斯特兰奇</html>");
            }
        });
        ImageIcon magicianico = new ImageIcon("src\\img\\xiaotu\\magician_1.jpg");
        magicianbt.setIcon(magicianico);
        magicianbt.setBounds(550,280,110,140);
        role.add(magicianbt);
        JLabel magicianlb = new JLabel("魔法师");
        magicianlb.setBounds(580,410,50,50);
        magicianlb.setFont(new Font("宋体",Font.PLAIN,16));
        role.add(magicianlb);


        JButton next = new JButton(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if () {
                    /*
                    * 判断使用哪个角色进入游戏
                    * */
                }
            }
        });
        next.setText("确定");
        next.setFont(new Font("宋体",Font.PLAIN,14));
        next.setBounds(110,430,65,20);
        role.add(next);

        role.setVisible(true);
    }

    public static void main(String[] args) {
        role role = new role();
        role.newrole();
    }
}

目前的效果如下,有三个角色,如果实现 确定 按钮获取我所选择的角色信息和属性呢

img

你选择哪个角色,应该是触发右边三个角色其中之一时,做触发事件,在事件中将选择的角色信息给到左边组件中,确定按钮,只是一个选择角色确定的过程,真正选择了某个角色从左边组件中就可以获取。

可以定义一个全局变量默认一个角色,可以用角色名或者一个int型的id代表。
在点击选择角色的事件中为id赋值。点击确定后只需要讲此时选的角色id传给你需要用到的下一步就好了。