package Deom;
import java.awt.*;import java.awt.event.*;import javax.swing.*;
public class PopGame extends JFrame { private final JTextField textField; private final JTextField textField1; private final JTextField textField2; //scoreArr数组,用来存放5局两队的单轮成绩; static int[][] scoreArr = new int[5][2];
//用来统计两队胜利的次数; static int ni1 = 0; static int ni2 = 0;
//result数组,用来存放每一轮的成绩,方便查询; int i=0; static String[] result = new String[5]; //输出第几盘的比分; //查询按钮; String str2; String []str={"1","2","3","4","5"}; private final JButton[] queryButton =new JButton[5]; public static void main(String[] args) {
try { PopGame popGame = new PopGame(); popGame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
public PopGame() { super(); setTitle("排球计分系统"); setBounds(100, 100, 500, 375); final JPanel panel = new JPanel(); //设置面板在北面; getContentPane().add(panel, BorderLayout.NORTH); final JLabel label = new JLabel(); //显示 label.setText("长垣 "); label.setBounds(100, 100, 300, 100); Font fOnt= new Font("黑体", Font.PLAIN, 20); label.setFont(font); panel.add(label); //胜利比; textField1 = new JTextField(); //设置胜利比显示区域大小; textField1.setPreferredSize(new Dimension(100, 40)); //显示位置; textField1.setHorizontalAlignment(SwingConstants.CENTER); //设置字体和字号; textField1.setFont(new Font("黑体", Font.PLAIN, 20)); textField1.setText(ni1 + " : " + ni2); panel.add(textField1); //不可编辑; textField1.setEditable(false); final JLabel label1 = new JLabel(); label1.setText(" 驻马店"); label1.setBounds(100, 100, 300, 100); Font font1 = new Font("", Font.PLAIN, 20); label1.setFont(font); panel.add(label1); //长垣按钮; final JPanel Panel1 = new JPanel(); JButton button1 = new JButton("a"); button1.setBorder(BorderFactory.createRaisedBevelBorder()); button1.setPreferredSize(new Dimension(30, 100)); button1.setText("a"); //添加按钮监听事件; button1.addActionListener(new inputJbuttonActionListener()); //驻马店按钮; final JPanel Panel2 = new JPanel(); JButton button2 = new JButton("b"); button2.setBorder(BorderFactory.createRaisedBevelBorder()); button2.setPreferredSize(new Dimension(30, 100)); //button2按钮显示b; button2.setText("b"); //添加按钮监听事件; button2.addActionListener(new inputJbuttonActionListener()); //添加到面板; Panel1.add(button1); Panel2.add(button2); //将面板Panel1添加到左边; getContentPane().add(Panel1, BorderLayout.WEST); //将面板Panel2添加到右边; getContentPane().add(Panel2, BorderLayout.EAST); //显示器; textField = new JTextField(); final JPanel viewPanel = new JPanel(); textField.setPreferredSize(new Dimension(400, 100)); //显示字体样式; textField.setFont(new Font("", Font.PLAIN, 40)); textField.setText(scoreArr[0][0] + " : " + scoreArr[0][1]); textField.setEditable(false); viewPanel.add(textField); //中间显示字; textField.setHorizontalAlignment(SwingConstants.CENTER); //将面板添加到中间; getContentPane().add(viewPanel, BorderLayout.CENTER); //添加查询比分字; final JLabel label2=new JLabel(); label2.setText("查询比分"); textField2=new JTextField(); final JPanel viewPanel1 = new JPanel(); //显示字体样式; textField2.setFont(new Font("", Font.PLAIN, 15)); textField2.setText(""); textField2.setColumns(5); textField2.setEditable(false); //添加查询按钮; for(int k=0;k<5;k++){ queryButton[k]=new JButton(); queryButton[k].setText(str[k]); queryButton[k].addActionListener(new inputJbuttonActionListener()); viewPanel1.add( queryButton[k]); } //添加清除数据按钮; JButton button3 = new JButton("清除"); button3.setText("清除"); button3.addActionListener(new inputJbuttonActionListener()); viewPanel1.add(button3); viewPanel1.add(label2); viewPanel1.add(textField2); getContentPane().add(viewPanel1, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public class inputJbuttonActionListener implements ActionListener { /** * Invoked when an action occurs. * * @param e */ String a="a";String b = "b";String c="清除"; public void actionPerformed1(ActionEvent e) { JButton button=(JButton)e.getSource(); String buttOnName=e.getActionCommand(); //把全部数据清空; if (buttOnName==c){ i=0; scoreArr = new int[5][2]; ni1=0;ni2=0; result= new String[5]; textField.setText(scoreArr[i][0] + " : " + scoreArr[i][1]); textField1.setText(ni1 + " : " + ni2); return; } setText1(buttonName); } public void setText1(String buttonName){ int j; //右下角显示查询第几局比分; for(j=0;j<5;j++){ if(buttOnName==str[j]return; } } if(j==5){ textField2.setText("无"); } //已经分出胜负; if(ni1>ni2 textField1.setText(ni1 + " : " + ni2); return; } else if(ni1=3){ textField.setText("已经分出胜利,驻马店胜利"); textField1.setText(ni1 + " : " + ni2); return; } if(buttOnName==a) { scoreArr[i][0]++; } else if (buttOnName== b) { scoreArr[i][1]++; } //改变比分; textField.setText(scoreArr[i][0] + " : " + scoreArr[i][1]); //比赛中得够25分并且领先对方2分以上的一方将获得一局胜利; if((scoreArr[i][0]-scoreArr[i][1]>=2) result[i]=scoreArr[i][0] + ":" + scoreArr[i][1]; i++; textField.setText(scoreArr[i][0] + " : " + scoreArr[i][1]); } else if((scoreArr[i][1]-scoreArr[i][0]>=2) result[i]=scoreArr[i][0] + ":" + scoreArr[i][1]; i++; textField.setText(scoreArr[i][0] + " : " + scoreArr[i][1]); }
//改变胜利比分; textField1.setText(ni1 + " : " + ni2); //当有一方胜局大于>3在当局比分处显示; if(ni1>=3){ textField.setText("长垣胜利"); } else if (ni2 >= 3){ textField.setText("驻马店胜利"); } }@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stub
}}
}