为JRadioButton增加价值

时间:2015-06-11 10:57:08

标签: java user-interface actionlistener jradiobutton buttongroup

我正在尝试制作一种简单的足球模拟器。这是我在观看教程后创建的代码,我知道它非常糟糕。我想要做的就是为团队增加一个价值,比如最好的团队为1,最差的团队为10,当我选择第1组的团队和第2组的团队时,我会点击模拟按钮弹出一个出现告诉我,鉴于球队的价值,哪支球队会赢。但我无法弄清楚如何做到这一点。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;



public class sim extends JPanel {

  public sim() {
    // JFrame constructor
      super(true);

    JRadioButton chelsea, arsenal, chelsea2, arsenal2;

    this.setLayout(new GridLayout(3,0));

    ButtonGroup group = new ButtonGroup();
    ButtonGroup group2 = new ButtonGroup();

    // takes image and saves it the the variable
    Icon a = new ImageIcon(getClass().getResource("a.PNG"));
    Icon c = new ImageIcon(getClass().getResource("c.JPG"));


    chelsea = new JRadioButton("Chelsea",c);
    chelsea.setHorizontalTextPosition(AbstractButton.CENTER);
    chelsea.setVerticalTextPosition(AbstractButton.BOTTOM);

    arsenal = new JRadioButton("Arsenal",a);
    arsenal.setHorizontalTextPosition(AbstractButton.CENTER);
    arsenal.setVerticalTextPosition(AbstractButton.BOTTOM);

    group.add(chelsea);
    group.add(arsenal);

    JLabel label = new JLabel("");
    TitledBorder titled = new TitledBorder("Team 1");
    label.setBorder(titled);

    chelsea.setBorder(titled);
    arsenal.setBorder(titled);

    JButton button = new JButton("Simulate");
    button.setHorizontalAlignment(JButton.CENTER);
    add(button, BorderLayout.CENTER);

    chelsea2 = new JRadioButton("Chelsea",c);
    chelsea2.setHorizontalTextPosition(AbstractButton.CENTER);
    chelsea2.setVerticalTextPosition(AbstractButton.BOTTOM);

    arsenal2 = new JRadioButton("Arsenal",a);
    arsenal2.setHorizontalTextPosition(AbstractButton.CENTER);
    arsenal2.setVerticalTextPosition(AbstractButton.BOTTOM);

    group2.add(chelsea2);
    group2.add(arsenal2);

    JLabel label2 = new JLabel("");
    TitledBorder titled2 = new TitledBorder("Team 2");
    label2.setBorder(titled2);

    chelsea2.setBorder(titled);
    arsenal2.setBorder(titled);

    add(label);
    add(chelsea);
    add(arsenal);

    add(button);

    add(chelsea2);
    add(arsenal2);

    HandlerClass handler = new HandlerClass();
    chelsea.addActionListener(handler);
  }

  private class HandlerClass implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
      {

          //JOptionPane.showMessageDialog(null, String.format("%s",     e.getActionCommand()));

      }
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Final");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1920, 1080);
    frame.setContentPane(new sim());
    frame.setVisible(true);
  }
}

2 个答案:

答案 0 :(得分:0)

您可以创建与JLabel

相关联的JRadioButton

JLabel的可见性为假,以便它们不可见。

然后,您可以点击JLabel s

来比较相应JRadioButton的值

答案 1 :(得分:0)

如果您想将其他值与每个JRadioButton相关联,则可以创建HashMap

将值与JRadioButton关联:

HashMap<JRadioButton,Integer> = new HashMap<>();
hm.put(chelsea2, new Integer(10));
hm.put(arsenal2, new Integer(1));

要获取特定JRadioButton的值(例如chelsea2):

int i = (Integer) hm.get(chelsea2);