Java教程

Java-Work11-图形界面设计1(2)

本文主要是介绍Java-Work11-图形界面设计1(2),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

package work11;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class Work11_2 extends JFrame implements ActionListener
{
int win, lose, score, draw;
JLabel label;
JTextArea textArea;
JButton scissors, stone, cloth, clear;
JPanel panel1, panel2;
JScrollPane jsp;

public Work11_2()
{
	setTitle("Small Game");
	setLocation(250, 250);
	setSize(350, 350);
	
	win = lose = score = draw = 0;
	
	panel1 = new JPanel();
	label = new 
			JLabel("赢: "+Integer.toString(win)+
			"  平: "+Integer.toString(draw)+
			"  输: "+Integer.toString(lose)+
			"  得分: "+Integer.toString(score));
	panel1.add(label);
	
	
	textArea = new JTextArea();
	textArea.setEditable(false);

// textArea.setLineWrap(true);
textArea.setBackground(Color.orange);
textArea.setFont(new Font("隶书", Font.BOLD, 21));
textArea.setForeground(Color.BLACK);
textArea.append("第"+Integer.toString(win+lose+draw+1)+"回合\n");
jsp = new JScrollPane(textArea);

	panel2 = new JPanel();
	scissors = new JButton("剪刀");
	stone = new JButton("石头");
	cloth = new JButton("布");
	clear = new JButton("清楚分数");
	scissors.addActionListener(this);
	stone.addActionListener(this);
	cloth.addActionListener(this);
	clear.addActionListener(this);
	panel2.add(scissors);
	panel2.add(stone);
	panel2.add(cloth);
	panel2.add(clear);
	
	add(panel1, BorderLayout.NORTH);
	add(jsp, BorderLayout.CENTER);
	add(panel2, BorderLayout.SOUTH);
}

@Override
public void actionPerformed(ActionEvent e)
{
	if(e.getSource() == scissors)
	{
		textArea.append("你出: 剪刀\n");
		int i = (int)(Math.random()*10%3);
		switch(i)
		{
			case 0:
				textArea.append("电脑出: 剪刀\n");
				textArea.append("结果: 平\n");
				draw += 1;
				break;
			case 1:
				textArea.append("电脑出: 石头\n");
				textArea.append("结果: 输\n");
				lose += 1;
				score -= 1;
				break;
			case 2:
				textArea.append("电脑出: 布\n");
				textArea.append("结果: 赢\n");
				win += 1;
				score += 2;
				break;
		}
	}
	else if(e.getSource() == stone)
	{
		textArea.append("你出: 石头\n");
		int i = (int)(Math.random()*10%3);
		switch(i)
		{
			case 0:
				textArea.append("电脑出: 剪刀\n");
				textArea.append("结果: 赢\n");
				win += 1;
				score += 2;
				break;
			case 1:
				textArea.append("电脑出: 石头\n");
				textArea.append("结果: 平\n");
				draw += 1;
				break;
			case 2:
				textArea.append("电脑出: 布\n");
				textArea.append("结果: 输\n");
				lose += 1;
				score -= 1;
				break;
		}
	}
	else if(e.getSource() == cloth)
	{
		textArea.append("你出: 布\n");
		int i = (int)(Math.random()*10%3);
		switch(i)
		{
			case 0:
				textArea.append("电脑出: 剪刀\n");
				textArea.append("结果: 输\n");
				lose += 1;
				score -= 1;
				break;
			case 1:
				textArea.append("电脑出: 石头\n");
				textArea.append("结果: 赢\n");
				win += 1;
				score += 2;
				break;
			case 2:
				textArea.append("电脑出: 布\n");
				textArea.append("结果: 平\n");
				draw += 1;
				break;
		}
	}
	else if(e.getSource() == clear)
	{
		win = 0;
		lose = 0;
		draw = 0;
		score = 0;
		textArea.setText("");
	}
	textArea.append("第"+Integer.toString(win+lose+draw+1)+"回合\n");
	label.setText("赢: "+Integer.toString(win)+
				"  平: "+Integer.toString(draw)+
				"  输: "+Integer.toString(lose)+
				"  得分: "+Integer.toString(score));
}

public static void main(String[] args)
{
	Work11_2 frame = new Work11_2();
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
}

}

这篇关于Java-Work11-图形界面设计1(2)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!