GolfRush是一款关于高尔夫的体育运动游戏。
在游戏中,通过推杆击打高尔夫球,最终打入洞中即为获胜,用杆数少,用时短者更加有优势。
import java.awt.Colorimport java.awt.Point
import java.awt.Toolkit
import java.awt.event.KeyEvent
import java.awt.event.KeyListener
import java.awt.event.MouseEvent
import java.awt.event.MouseListener
import javax.swing.BorderFactory
import javax.swing.ButtonGroup
import javax.swing.JButton
import javax.swing.JCheckBox
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JList
import javax.swing.JRadioButton
import javax.swing.JTextArea
import javax.swing.JTextField
import javax.swing.ListSelectionModel
/**
* 一个简单的Swing窗口,输入内容单击“确定”按钮后,在文本域中显示输入的内容。
* 单击“取消”按钮,清空页面内容。
* @author yzg
*
*/
public class Register extends JFrame {
private static final long serialVersionUID = 1L
private JLabel nameLabel
private JTextArea context
private JTextField name
private JLabel pLabel
JList speciality
JLabel mLabel
String[] data = { "计算机", "英语", "机械", "化工" }
ButtonGroup bg
JRadioButton male
JRadioButton female
JLabel fLabel
JCheckBox faverite1
JCheckBox faverite2
JCheckBox faverite3
JCheckBox faverite4
public Register(String title) {
super(title)
this.getContentPane().setLayout(null)
// 下面两行是取得屏幕的高度和宽度
double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth()
double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight()
this.setLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 200))// 设定窗口出现位置
this.setSize(340, 440)// 设定窗口大小
}
public void showWin() {
// 确保窗体有一个好的外观装饰
// setDefaultLookAndFeelDecorated(true)
this.setDefaultCloseOperation(EXIT_ON_CLOSE)
// 姓名
nameLabel = new JLabel("姓名 :")
nameLabel.setBounds(30, 10, 50, 25)
name = new JTextField()
name.setBounds(80, 10, 120, 20)
name.setBorder(BorderFactory.createLineBorder(Color.BLUE))
name.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
if (name.getText().length() >6) {
name.setText(name.getText().substring(0, 6))
}
}
})
// 专业 组合框
pLabel = new JLabel("专业 :")
pLabel.setBounds(30, 40, 50, 25)
speciality = new JList(data)
speciality.setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
speciality.setBounds(80, 40, 80, 85)
speciality.setBorder(BorderFactory.createLineBorder(Color.GREEN))
mLabel = new JLabel("性别 :")
mLabel.setBounds(30, 130, 50, 25)
// 性别 单选框
bg = new ButtonGroup()
male = new JRadioButton("男")
female = new JRadioButton("女")
bg.add(male)
bg.add(female)
male.setBounds(80, 130, 60, 25)
female.setBounds(140, 130, 60, 25)
fLabel = new JLabel("爱好 :")
fLabel.setBounds(30, 160, 50, 25)
// 爱好 复选框
faverite1 = new JCheckBox("音乐")
faverite2 = new JCheckBox("足球")
faverite3 = new JCheckBox("高尔夫")
faverite4 = new JCheckBox("游戏")
faverite1.setBounds(80, 160, 60, 25)
faverite2.setBounds(140, 160, 60, 25)
faverite3.setBounds(200, 160, 65, 25)
faverite4.setBounds(265, 160, 60, 25)
// 内容 文本区域
JLabel conLabel = new JLabel("输入的内容 :")
conLabel.setBounds(30, 250, 90, 25)
context = new JTextArea()
context.setBounds(30, 270, 260, 100)
context.setBorder(BorderFactory.createLineBorder(Color.black))
// 确定按钮
JButton ok = new JButton("确定")
ok.setBounds(50, 190, 60, 25)
ok.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
StringBuffer sb = new StringBuffer()
sb.append(nameLabel.getText()).append(name.getText())
sb.append("\n")
int index = speciality.getSelectedIndex()
if (index >= 0) {
sb.append(pLabel.getText()).append(data[index])
} else {
sb.append(pLabel.getText())
}
sb.append("\n")
sb.append(mLabel.getText())
if (male.isSelected()) {
sb.append("男")
}
if (female.isSelected()) {
sb.append("女")
}
sb.append("\n")
sb.append(fLabel.getText())
if (faverite1.isSelected()) {
sb.append("音乐 ")
}
if (faverite2.isSelected()) {
sb.append("足球 ")
}
if (faverite3.isSelected()) {
sb.append("高尔夫 ")
}
if (faverite4.isSelected()) {
sb.append("游戏 ")
}
context.setText(sb.toString())
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
})
// 取消按钮
JButton cancel = new JButton("取消")
cancel.setBounds(120, 190, 60, 25)
cancel.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
name.setText("")
speciality.clearSelection()
if (faverite1.isSelected()) {
faverite1.setSelected(false)
}
if (faverite2.isSelected()) {
faverite2.setSelected(false)
}
if (faverite3.isSelected()) {
faverite3.setSelected(false)
}
if (faverite4.isSelected()) {
faverite4.setSelected(false)
}
context.setText("")
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
})
this.getContentPane().add(nameLabel)
this.getContentPane().add(name)
this.getContentPane().add(pLabel)
this.getContentPane().add(speciality)
this.getContentPane().add(mLabel)
this.getContentPane().add(male)
this.getContentPane().add(female)
this.getContentPane().add(fLabel)
this.getContentPane().add(faverite1)
this.getContentPane().add(faverite2)
this.getContentPane().add(faverite3)
this.getContentPane().add(faverite4)
this.getContentPane().add(conLabel)
this.getContentPane().add(context)
this.getContentPane().add(ok)
this.getContentPane().add(cancel)
// this.pack()
this.setVisible(true)
}
/**
* @param args
*/
public static void main(String[] args) {
Register reg = new Register("Register")
reg.showWin()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)