比赛记分板
之前写的,记得好像有个bug
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("排球比赛计分系统"); // 实例化窗体对象
frame.setLayout(null); // 使用绝对定位
Font f = new Font("",Font.BOLD, 50);
JLabel guo = new JLabel("中国队得分"); // 建立标签对象
guo.setFont(f);
guo.setForeground(Color.red);
JLabel ben = new JLabel("日本队得分"); // 建立标签对象
ben.setFont(f);
ben.setForeground(Color.black);
// 日本得分框
JLabel ri = new JLabel("0");
ri.setForeground(Color.black);
ri.setBounds(450, 150, 300, 100);
ri.setFont(new Font("dialog", 100, 100));
frame.add(ri);
// 中国得分框
JLabel z = new JLabel("0");
z.setForeground(Color.red);
z.setBounds(100,150, 300, 100);
z.setFont(new Font("dialog", 100, 100));
frame.add(z);
//按钮计分
Font q = new Font("", Font.PLAIN, 40);
JButton china = new JButton("中国队得分");
china.setForeground(Color.red);
china.setFont(q);
china.addActionListener
(
new ActionListener() {
int count=1;
public void actionPerformed(ActionEvent e) {
z.setText(String.valueOf(count));
if(count>=10) {
z.setText("获胜");
}
count++;
}
}
);
// 建立按钮对象
JButton japan = new JButton("日本队得分");
japan.setFont(q);
japan.setForeground(Color.black);
japan.addActionListener(new ActionListener() {
int a=1;
public void actionPerformed(ActionEvent e) {
ri.setText(String.valueOf(a));
if(a>=25) {
ri.setText("获胜");
}
a++;
}
});
// 设置窗体大小
frame.setSize(800, 430);
frame.setLocation(700, 400);
guo.setBounds(10, 0, 500, 100);
ben.setBounds(450, 0, 500, 100);
china.setBounds(0, 300, 400, 100);
japan.setBounds(400, 300, 400, 100);
frame.add(china);
frame.add(japan);
frame.add(guo);
frame.add(ben);
frame.setVisible(true); // 设置窗体可见
}
}