62,614
社区成员
发帖
与我相关
我的任务
分享
package com.holiday.volleyball.date0120;
/**
* 2.题目:排球比赛计分系统
设计要求:
编写一个能够对排球比赛实时统计比分和显示比分的程序。排球竞赛规则规定:
排球竞赛为五局三胜制,每局比赛中得够25分并且领先对方2分以上的一方将获得一局胜利,先获得三局胜利的一方将获得比赛的胜利。
若比赛中出现24:24的比分,则一方必须领先对方2分方可结束一局。若双方前四局的比分为2:2,则将进行第五局的比赛,
第五局比赛中得够15分并且领先对方2分以上的一方将获得胜利。若比赛中出现14:14的比分,则一方必须领先对方2分方可结束比赛。
设计一个程序,分别用两个按钮来输入比赛一方的得分。
根据比赛进展,用两个固定的按钮输入比赛双方的得分,如果一方得分,则按下对应的按钮一次,GUI界面上的分数将增加1分。
当完成一局比赛时,程序自动将比赛得分清零,并且相应地将获得本局胜利一方的局分增加1分。若一方获得三局比赛的胜利,则程序自动显示获胜一方的名称。
要求程序具备以下功能:可以实时输入和显示比赛双方的比分和局分,可以存储比赛的得分过程,并且可以调用已经存储的比赛得分记录。
输入/输出要求:
输入要求:在程序启动之后输入参加比赛的两只球队的名称和队旗画面图像。按照比赛进展,实时输入比赛得分。
输出要求:程序运行时,可以在GUI界面上显示球队的名称和队旗、双方的当前赛局的比分、双方的局分。
数据管理能力要求:
比赛结束之后,可以将整个比赛的整个得分过程存储到文件中,以供回顾使用
*/
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.GridLayout;
public class Volleyball extends JFrame {
JPanel contentPane;
JPanel panelJP;
JLabel labelJP;
JTextField gameJP_field;
JPanel panelJP_top;
JButton btnJP;
JTextField scoreJP_tf;
JPanel panel_scoreJP;
JPanel panelKR;
JTextField gameKR_field;
JLabel labelKR;
JPanel panelKR_top;
JTextField scoreKR_tf;
JButton btnKR;
JPanel panel_scoreKR;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Volleyball frame = new Volleyball();
ImageIcon icon=new ImageIcon("icon/volleyball.jpg");
frame.setIconImage(icon.getImage());
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
Volleyball volley=new Volleyball();
volley.win();
}
/**
* Create the frame.
*/
public Volleyball() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setForeground(new Color(233, 150, 122));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
panelJP = new JPanel(new BorderLayout());
panelJP.setBackground(Color.PINK);
contentPane.add(panelJP, BorderLayout.WEST);
labelJP = new JLabel(" \u65E5\u672C\u961F ");
labelJP.setFont(new Font("微软雅黑", Font.BOLD, 13));
labelJP.setForeground(Color.RED);
gameJP_field=new JTextField("0");//日本队赢的局数文本框
gameJP_field.setHorizontalAlignment(JTextField.RIGHT);
panelJP_top=new JPanel(new GridLayout(1,2));//放置 labelJP,game_field
panelJP_top.add(labelJP);
panelJP_top.add(gameJP_field);
panelJP.add(panelJP_top,BorderLayout.NORTH);
btnJP=new JButton("+1");//日本队加分按钮
btnJP.setFont(new Font("方正兰亭超细黑简体", Font.BOLD, 13));
btnJP.setForeground(new Color(0, 191, 255));
btnJP.setBackground(new Color(255, 69, 0));
scoreJP_tf=new JTextField("0");//日本队分数文本框
scoreJP_tf.setHorizontalAlignment(JTextField.RIGHT);
scoreJP_tf.setSize(300,100);
panel_scoreJP=new JPanel(new GridLayout(1, 2)); //放置btnJP和scoreJP_tf
panel_scoreJP.add(btnJP);
panel_scoreJP.add(scoreJP_tf);
panelJP.add(panel_scoreJP,BorderLayout.SOUTH);
btnJP.addActionListener(new ActionListener() { //日本队加分按钮监听事件
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int scoreJP=Integer.parseInt(scoreJP_tf.getText());//将分数文本框String转换为int
scoreJP_tf.setText(Integer.toString(scoreJP+1));//基本类型转化为字符串 点击
//scoreJP_tf.setText(String.valueOf(scoreTf+1));
// scoreJP_tf.setText((scoreTf+1)+"");
}
});
panelKR = new JPanel(new BorderLayout());
panelKR.setBackground(Color.CYAN);
contentPane.add(panelKR, BorderLayout.EAST);
gameKR_field=new JTextField("0");//韩国队赢局数文本框
labelKR = new JLabel(" 韩国队");
labelKR.setFont(new Font("微软雅黑", Font.BOLD, 13));
labelKR.setForeground(Color.BLUE);
panelKR_top=new JPanel(new GridLayout(1,2));
panelKR_top.add(gameKR_field);
panelKR_top.add(labelKR);
panelKR.add(panelKR_top,BorderLayout.NORTH);
scoreKR_tf=new JTextField("0");//韩国队分数文本框
btnKR=new JButton("+1");
btnKR.setBackground(new Color(255, 255, 0));
btnKR.setFont(new Font("方正兰亭超细黑简体", Font.BOLD, 13));
btnKR.setForeground(new Color(205, 92, 92));
panel_scoreKR=new JPanel(new GridLayout(1, 2));//放置scoreKR_tf,btnKR
panel_scoreKR.add(scoreKR_tf);
panel_scoreKR.add(btnKR);
panelKR.add(panel_scoreKR,BorderLayout.SOUTH);
btnKR.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int scoreKR=Integer.parseInt(scoreKR_tf.getText());
scoreKR_tf.setText((scoreKR+1)+"");
// scoreKR_tf.setText(Integer.toString(scoreKR));
// scoreKR_tf.setText(String.valueOf(scoreKR));
}
});
}
public void win() {
int scoreKR=Integer.parseInt(scoreKR_tf.getText());//韩国队得分文本框(下面)
int scoreJP=Integer.parseInt(scoreJP_tf.getText());//日本队得分文本框(下面)
int gameJP=Integer.parseInt(gameJP_field.getText());//日本队赢得局数文本框(上面)
int gameKR=Integer.parseInt(gameKR_field.getText());//韩国队赢得局数文本框(上面)
if (((scoreKR-scoreJP)>=2)&&(scoreKR>=25)) { //韩国队比日本队高两分以上
gameKR++; //韩国队赢一局
gameKR_field.setText(Integer.toString(gameKR)); //将韩国队赢得的局数转成String 填在韩国队赢得局数文本框
}
if (((scoreJP-scoreKR)>=2)&&(scoreJP>=25)) {
gameJP++; //日本队赢一局
gameJP_field.setText(Integer.toString(gameJP));
}
}
}