【问题标题】:Java Simple SimulatorJava 简单模拟器
【发布时间】:2015-08-26 15:00:41
【问题描述】:

我正在尝试制作一种简单的足球模拟器。这是我在观看教程后创建的代码,我知道它非常糟糕。我要做的就是为团队添加一个值,例如最好的团队为 1,最差的团队为 10,当我单击模拟时,会出现一个弹出窗口,告诉我根据团队的价值,哪个团队会获胜。但我不知道该怎么做。

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);
  }
}

【问题讨论】:

  • 你想如何添加这个值?你的意思是一个随机值?
  • 我想给切尔西加值 1,给阿森纳加值 2,当我选择那些球队时,我点击 sim 按钮,它会显示切尔西会赢,因为他们有更好的价值.

标签: java user-interface radio-button actionlistener buttongroup


【解决方案1】:

首先在 Java 中始终将您的类的名称大写,然后检查此代码:

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("/home/mehdi/Pictures/ICONS/Test/1.png");
        Icon c = new ImageIcon("/home/mehdi/Pictures/ICONS/Test/2.png");


        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);

        final 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);

        button.addActionListener(new HandlerClass(label));
    }

    private class HandlerClass implements ActionListener{

        final JLabel label;
        public HandlerClass(JLabel label){
             this.label = label;
        }
        public void actionPerformed(ActionEvent e)
        {
            label.setText("SSSSSSSSSSSSSsssss");
            JOptionPane.showMessageDialog(null, "Something");
        }
    }

    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);
    }
}

【讨论】:

  • How would i make it so when a team in group 1 and a team in group 2 were chosen then i click the sim button it would show that the team with the high value would win?
  • 首先让按钮默认禁用:button.setEnabled(false);在单击团队时编写 actionListener 并在该侦听器内部检查团队是否已成对选择然后如果是 button.setEnabled(true) 并管理用户单击按钮时应该发生的事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
相关资源
最近更新 更多