import javax.swing.*;//新的窗口組件包
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、淶水ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的淶水網(wǎng)站制作公司
import java.awt.*;
import java.awt.event.*;
public class Jisuanqi implements ActionListener
{ JFrame jf=new JFrame();
boolean dotExist, operated, equaled; // 幫助運(yùn)算的布爾變量
double result; // 目前的結(jié)果
char lastOperator; // 表示上一運(yùn)算符
JTextField jtf; // 顯示欄
JButton dot, plus, minus, multi, div, sqrt, equal, change, clear,quyu; // 運(yùn)算符
JButton[] numbers;
Panel p=new Panel();
// 構(gòu)造者
public Jisuanqi()
{
jf.setTitle("陳巳偉牌計(jì)算器");
// 初始化變量
dotExist = false; // 表示當(dāng)前的數(shù)是否有小數(shù)點(diǎn)
operated = false; // 表示任意運(yùn)算符是否被按下
equaled = false; // 表示等號(hào)是否被按下
result = 0;
lastOperator = '?';
// 初始化窗口變量
jtf = new JTextField("0");
jtf.setEditable(false); //設(shè)置文本框的可編輯性
jf.setSize(250,200);
jf.setVisible(true);
numbers = new JButton[10];
for (int i = 0; i 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
change= new JButton("±");
clear = new JButton("NC");
quyu=new JButton("%");
p.setLayout(new GridLayout(5,4));
p.add(numbers[1]);
p.add(numbers[2]);
p.add(numbers[3]);
p.add(plus);
p.add(numbers[4]);
p.add(numbers[5]);
p.add(numbers[6]);
p.add(minus);
p.add(numbers[7]);
p.add(numbers[8]);
p.add(numbers[9]);
p.add(multi);
p.add(dot);
p.add(numbers[0]);
p.add(sqrt);
p.add(div);
p.add(equal);
p.add(change);
p.add(clear);
p.add(quyu);
jf.add(jtf,BorderLayout.NORTH);
jf.add(p,BorderLayout.CENTER);
numbers[0].addActionListener(this);
numbers[1].addActionListener(this);
numbers[2].addActionListener(this);
numbers[3].addActionListener(this);
plus.addActionListener(this);
numbers[4].addActionListener(this);
numbers[5].addActionListener(this);
numbers[6].addActionListener(this);
minus.addActionListener(this);
numbers[7].addActionListener(this);
numbers[8].addActionListener(this);
numbers[9].addActionListener(this);
multi.addActionListener(this);
dot.addActionListener(this);
sqrt.addActionListener(this);
div.addActionListener(this);
equal.addActionListener(this);
change.addActionListener(this);
clear.addActionListener(this);
quyu.addActionListener(this);
}
// 對(duì)按鈕進(jìn)行反應(yīng)的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
if (btn == clear)
{
jtf.setText("0");
dotExist = false;
operated = false;
equaled = false;
result = 0;
lastOperator = '?';
}
else if (btn == equal)
{
operate('=');
equaled = true;
}
else if (btn == plus)
{
operate('+');
equaled = false;
}
else if (btn == minus)
{
operate('-');
equaled = false;
}
else if (btn == multi)
{
operate('*');
equaled = false;
}
else if (btn == div)
{
operate('/');
equaled = false;
}
else if (btn==quyu){
operate('%');
equaled=false;
}
else if (btn == change)
{
operate('p');
operate('=');
equaled = true;
}
else if (btn == sqrt)
{
operate('s');
operate('=');
equaled = true;
}
else
{
if (equaled)
result = 0;
for (int i = 0; i 10; i++)
if (btn == numbers[i])
{
if (jtf.getText().equals("0"))
jtf.setText("" + i);
else if(! operated)
jtf.setText(jtf.getText() + i);
else
{
jtf.setText("" + i);
operated = false;
}
}
if (btn == dot ! dotExist)
{
jtf.setText(jtf.getText() + ".");
}
}
}
// 進(jìn)行運(yùn)算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(jtf.getText()).doubleValue();
if (lastOperator == '?')
result = currentNumber;
else if (lastOperator == '+')
result += currentNumber;
else if (lastOperator == '-')
result -= currentNumber;
else if (lastOperator == '*')
result *= currentNumber;
else if (lastOperator == '/')
result /= currentNumber;
else if(lastOperator=='%')
result %=currentNumber;
else if (lastOperator == 'p')
result *= -1;
else if (lastOperator == 's')
result = Math.sqrt(currentNumber);
else if (lastOperator == '=' equaled)
result = currentNumber;
jtf.setText("" + result);
operated = true;
lastOperator = operator;
}
public static void main(String[] args)
{new Jisuanqi();br br }
}
//都是從新手過(guò)來(lái)的,以下代碼供參考
//1.
public?class?BankAccount?{
private?static?String?acctnum;
private?static?double?money;
private?static?void?showAcct()?{
System.out.println("賬號(hào)為:?"?+?acctnum);
}
private?static?void?showMoney()?{
System.out.println("余額為:?"?+?money);
}
public?BankAccount(String?acc,?double?m)?{
this.acctnum?=?acc;
this.money?=?m;
}
public?static?void?main(String[]?args)?{
BankAccount?ba?=?new?BankAccount("626600018888",?5000.00);
ba.showAcct();
ba.showMoney();
}
}
//2.
public?class?Triangle?{
private?static?float?a;
private?static?float?b;
private?static?float?c;
public?Triangle(float?a,?float?b,?float?c)?{
this.a?=?a;
this.b?=?b;
this.c?=?c;
}
public?static?boolean?judgeTriangle(float?a,?float?b,?float?c)?{
if?((a??Math.abs(b?-?c)??a??b?+?c)
?(b??Math.abs(a?-?c)??b??a?+?c)
?(c??Math.abs(a?-?b)??c??a?+?b))
return?true;
else
return?false;
}
public?float?getCircumference()?{
return?this.a?+?this.b?+?this.c;
}
}
//3.
public?class?TestTriangle?{
public?static?void?main(String[]?args)?{
Triangle?t?=?new?Triangle(5.3f,7.8f,9.3f);
if(t.judgeTriangle(5.3f,7.8f,9.3f)){
System.out.print("能夠成三角形,周長(zhǎng)為:?");
System.out.printf("%9.2f",t.getCircumference());}
else
System.out.println("不能構(gòu)成三角形");
}
}
你說(shuō)的java源代碼是指編譯成的class文件前的java文件。
當(dāng)我們運(yùn)行.java文件時(shí),它會(huì)被系統(tǒng)編譯成.class文件,例如Test.java編譯之后就是Test.class,
源文件就是指Test.java文件,
一般部署項(xiàng)目時(shí),有.class文件就可以發(fā)布運(yùn)行了,但是如果想修改這個(gè)系統(tǒng),.class是不能修改的,要有.java文件才能修改
也可以上網(wǎng)去下反編譯軟件,就是能把.class文件大部分還原成.java文件的工具,但不是100%還原,而且如果不是正版的,小心有毒啊,什么的。
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.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class game21 extends JFrame {
private JLabel label_2;
private int number;
private int sum;
final JLabel label = new JLabel();
final JLabel label_1 = new JLabel();
public static void main(String[] args) {
new game21();
}
public game21() {
super("21點(diǎn)?!");
getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
onClick();
}
});
button.setText("出牌");
button.setBounds(170, 350, 106, 28);
getContentPane().add(button);
label.setBorder(new LineBorder(Color.black, 1, false));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("", Font.BOLD, 26));
label.setText("背面");
label.setBounds(158, 81, 137, 153);
getContentPane().add(label);
label_1.setText("你已經(jīng)擁有的牌:");
label_1.setBounds(109, 22, 270, 45);
getContentPane().add(label_1);
this.setBounds(200, 300, 501, 528);
this.setVisible(true);
getContentPane().add(getLabel_2());
}
public int randNumber() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return (int) (Math.random() * 10 + 1);
}
public void onClick() {
number = this.randNumber();
this.sum += number;
label.setText("" + number);
String strTemp = this.label_1.getText();
strTemp += "" + number + " ";
label_1.setText(strTemp);
String temp = "合計(jì):" + sum;
label_2.setText(temp);
isWin();
}
public void isWin() {
if (sum 21) {
JOptionPane.showMessageDialog(this, "你輸了");
clear();
return;
} else if (sum == 21) {
JOptionPane.showMessageDialog(this, "你贏了");
clear();
return;
} else {
int i = JOptionPane.showOptionDialog(this, "是否繼續(xù)?", "提示",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (i == JOptionPane.OK_OPTION) {
onClick();
} else
return;
}
}
private void clear() {
label_2.setText("合計(jì):");
sum = 0;
number = 0;
label_1.setText("你已經(jīng)擁有的牌:");
}
/**
* @return
*/
protected JLabel getLabel_2() {
if (label_2 == null) {
label_2 = new JLabel();
label_2.setText("合計(jì):");
label_2.setBounds(313, 35, 66, 18);
}
return label_2;
}
}
真好無(wú)聊中。。