问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

急需一些java小程序代码

发布网友 发布时间:2022-04-24 03:15

我来回答

2个回答

热心网友 时间:2023-10-24 06:34

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame {

private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField;
private String lastCommand;
private double result;
private boolean start;

public Calculator() {

super("Calculator");
container=getContentPane();
layout=new GridBagLayout();
container.setLayout(layout);
constraints=new GridBagConstraints();
start=true;
result=0;
lastCommand = "=";
displayField=new JTextField(20);
displayField.setHorizontalAlignment(JTextField.RIGHT);
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth=4;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.BOTH;
constraints.weightx=100;
constraints.weighty=100;
layout.setConstraints(displayField,constraints);
container.add(displayField);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
addButton("Backspace",0,1,2,1,insert);
addButton("CE",2,1,1,1,insert);
addButton("C",3,1,1,1,insert);
addButton("7",0,2,1,1,insert);
addButton("8",1,2,1,1,insert);
addButton("9",2,2,1,1,insert);
addButton("/",3,2,1,1,command);
addButton("4",0,3,1,1,insert);
addButton("5",1,3,1,1,insert);
addButton("6",2,3,1,1,insert);
addButton("*",3,3,1,1,command);
addButton("1",0,4,1,1,insert);
addButton("2",1,4,1,1,insert);
addButton("3",2,4,1,1,insert);
addButton("-",3,4,1,1,command);
addButton("0",0,5,1,1,insert);
addButton("+/-",1,5,1,1,insert);
addButton(".",2,5,1,1,insert);
addButton("+",3,5,1,1,command);
addButton("=",0,6,4,1,command);
setSize(300,300);
setVisible(true);

}

private void addButton(String label,int row,int column,int with,int height,ActionListener listener) {

JButton button=new JButton(label);
constraints.gridx=row;
constraints.gridy=column;
constraints.gridwidth=with;
constraints.gridheight=height;
constraints.fill=GridBagConstraints.BOTH;
button.addActionListener(listener);
layout.setConstraints(button,constraints);
container.add(button);
}

private class InsertAction implements ActionListener {

public void actionPerformed(ActionEvent event) {

String input=event.getActionCommand();
if (start) {
displayField.setText("");
start=false;
if(input.equals("+/-"))
displayField.setText(displayField.getText()+"-");
}

if(!input.equals("+/-")) {
if(input.equals("Backspace")) {
String str=displayField.getText();
if(str.length()>0)
displayField.setText(str.substring(0,str.length()-1));
}else if(input.equals("CE")||input.equals("C")) {
displayField.setText("0");
start=true;
}else
displayField.setText(displayField.getText()+input);
}
}
}

private class CommandAction implements ActionListener {

public void actionPerformed(ActionEvent evt) {
String command=evt.getActionCommand();
if(start) {
lastCommand=command;
}else {
calculate(Double.parseDouble(displayField.getText()));
lastCommand=command;
start=true;
}
}
}

public void calculate(double x) {
if (lastCommand.equals("+")) result+= x;
else if (lastCommand.equals("-")) result-=x;
else if (lastCommand.equals("*")) result*=x;
else if (lastCommand.equals("/")) result/=x;
else if (lastCommand.equals("=")) result=x;
displayField.setText(""+ result);
}

public static void main(String []args) {
Calculator calculator=new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

热心网友 时间:2023-10-24 06:34

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame {

private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField;
private String lastCommand;
private double result;
private boolean start;

public Calculator() {

super("Calculator");
container=getContentPane();
layout=new GridBagLayout();
container.setLayout(layout);
constraints=new GridBagConstraints();
start=true;
result=0;
lastCommand = "=";
displayField=new JTextField(20);
displayField.setHorizontalAlignment(JTextField.RIGHT);
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth=4;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.BOTH;
constraints.weightx=100;
constraints.weighty=100;
layout.setConstraints(displayField,constraints);
container.add(displayField);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
addButton("Backspace",0,1,2,1,insert);
addButton("CE",2,1,1,1,insert);
addButton("C",3,1,1,1,insert);
addButton("7",0,2,1,1,insert);
addButton("8",1,2,1,1,insert);
addButton("9",2,2,1,1,insert);
addButton("/",3,2,1,1,command);
addButton("4",0,3,1,1,insert);
addButton("5",1,3,1,1,insert);
addButton("6",2,3,1,1,insert);
addButton("*",3,3,1,1,command);
addButton("1",0,4,1,1,insert);
addButton("2",1,4,1,1,insert);
addButton("3",2,4,1,1,insert);
addButton("-",3,4,1,1,command);
addButton("0",0,5,1,1,insert);
addButton("+/-",1,5,1,1,insert);
addButton(".",2,5,1,1,insert);
addButton("+",3,5,1,1,command);
addButton("=",0,6,4,1,command);
setSize(300,300);
setVisible(true);

}

private void addButton(String label,int row,int column,int with,int height,ActionListener listener) {

JButton button=new JButton(label);
constraints.gridx=row;
constraints.gridy=column;
constraints.gridwidth=with;
constraints.gridheight=height;
constraints.fill=GridBagConstraints.BOTH;
button.addActionListener(listener);
layout.setConstraints(button,constraints);
container.add(button);
}

private class InsertAction implements ActionListener {

public void actionPerformed(ActionEvent event) {

String input=event.getActionCommand();
if (start) {
displayField.setText("");
start=false;
if(input.equals("+/-"))
displayField.setText(displayField.getText()+"-");
}

if(!input.equals("+/-")) {
if(input.equals("Backspace")) {
String str=displayField.getText();
if(str.length()>0)
displayField.setText(str.substring(0,str.length()-1));
}else if(input.equals("CE")||input.equals("C")) {
displayField.setText("0");
start=true;
}else
displayField.setText(displayField.getText()+input);
}
}
}

private class CommandAction implements ActionListener {

public void actionPerformed(ActionEvent evt) {
String command=evt.getActionCommand();
if(start) {
lastCommand=command;
}else {
calculate(Double.parseDouble(displayField.getText()));
lastCommand=command;
start=true;
}
}
}

public void calculate(double x) {
if (lastCommand.equals("+")) result+= x;
else if (lastCommand.equals("-")) result-=x;
else if (lastCommand.equals("*")) result*=x;
else if (lastCommand.equals("/")) result/=x;
else if (lastCommand.equals("=")) result=x;
displayField.setText(""+ result);
}

public static void main(String []args) {
Calculator calculator=new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

热心网友 时间:2023-10-24 06:34

21点游戏代码:
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点?!");
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("你已经拥有的牌:");
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 = "合计:" + 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, "是否继续?", "提示",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (i == JOptionPane.OK_OPTION) {
onClick();
} else
return;
}
}

private void clear() {
label_2.setText("合计:");
sum = 0;
number = 0;
label_1.setText("你已经拥有的牌:");
}

/**
* @return
*/
protected JLabel getLabel_2() {
if (label_2 == null) {
label_2 = new JLabel();
label_2.setText("合计:");
label_2.setBounds(313, 35, 66, 18);
}
return label_2;
}

}

都是编译成功的java源代码,希望对你有帮助!

热心网友 时间:2023-10-24 06:34

21点游戏代码:
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点?!");
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("你已经拥有的牌:");
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 = "合计:" + 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, "是否继续?", "提示",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (i == JOptionPane.OK_OPTION) {
onClick();
} else
return;
}
}

private void clear() {
label_2.setText("合计:");
sum = 0;
number = 0;
label_1.setText("你已经拥有的牌:");
}

/**
* @return
*/
protected JLabel getLabel_2() {
if (label_2 == null) {
label_2 = new JLabel();
label_2.setText("合计:");
label_2.setBounds(313, 35, 66, 18);
}
return label_2;
}

}

都是编译成功的java源代码,希望对你有帮助!

热心网友 时间:2023-10-24 06:34

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame {

private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField;
private String lastCommand;
private double result;
private boolean start;

public Calculator() {

super("Calculator");
container=getContentPane();
layout=new GridBagLayout();
container.setLayout(layout);
constraints=new GridBagConstraints();
start=true;
result=0;
lastCommand = "=";
displayField=new JTextField(20);
displayField.setHorizontalAlignment(JTextField.RIGHT);
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth=4;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.BOTH;
constraints.weightx=100;
constraints.weighty=100;
layout.setConstraints(displayField,constraints);
container.add(displayField);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
addButton("Backspace",0,1,2,1,insert);
addButton("CE",2,1,1,1,insert);
addButton("C",3,1,1,1,insert);
addButton("7",0,2,1,1,insert);
addButton("8",1,2,1,1,insert);
addButton("9",2,2,1,1,insert);
addButton("/",3,2,1,1,command);
addButton("4",0,3,1,1,insert);
addButton("5",1,3,1,1,insert);
addButton("6",2,3,1,1,insert);
addButton("*",3,3,1,1,command);
addButton("1",0,4,1,1,insert);
addButton("2",1,4,1,1,insert);
addButton("3",2,4,1,1,insert);
addButton("-",3,4,1,1,command);
addButton("0",0,5,1,1,insert);
addButton("+/-",1,5,1,1,insert);
addButton(".",2,5,1,1,insert);
addButton("+",3,5,1,1,command);
addButton("=",0,6,4,1,command);
setSize(300,300);
setVisible(true);

}

private void addButton(String label,int row,int column,int with,int height,ActionListener listener) {

JButton button=new JButton(label);
constraints.gridx=row;
constraints.gridy=column;
constraints.gridwidth=with;
constraints.gridheight=height;
constraints.fill=GridBagConstraints.BOTH;
button.addActionListener(listener);
layout.setConstraints(button,constraints);
container.add(button);
}

private class InsertAction implements ActionListener {

public void actionPerformed(ActionEvent event) {

String input=event.getActionCommand();
if (start) {
displayField.setText("");
start=false;
if(input.equals("+/-"))
displayField.setText(displayField.getText()+"-");
}

if(!input.equals("+/-")) {
if(input.equals("Backspace")) {
String str=displayField.getText();
if(str.length()>0)
displayField.setText(str.substring(0,str.length()-1));
}else if(input.equals("CE")||input.equals("C")) {
displayField.setText("0");
start=true;
}else
displayField.setText(displayField.getText()+input);
}
}
}

private class CommandAction implements ActionListener {

public void actionPerformed(ActionEvent evt) {
String command=evt.getActionCommand();
if(start) {
lastCommand=command;
}else {
calculate(Double.parseDouble(displayField.getText()));
lastCommand=command;
start=true;
}
}
}

public void calculate(double x) {
if (lastCommand.equals("+")) result+= x;
else if (lastCommand.equals("-")) result-=x;
else if (lastCommand.equals("*")) result*=x;
else if (lastCommand.equals("/")) result/=x;
else if (lastCommand.equals("=")) result=x;
displayField.setText(""+ result);
}

public static void main(String []args) {
Calculator calculator=new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

热心网友 时间:2023-10-24 06:34

21点游戏代码:
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点?!");
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("你已经拥有的牌:");
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 = "合计:" + 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, "是否继续?", "提示",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (i == JOptionPane.OK_OPTION) {
onClick();
} else
return;
}
}

private void clear() {
label_2.setText("合计:");
sum = 0;
number = 0;
label_1.setText("你已经拥有的牌:");
}

/**
* @return
*/
protected JLabel getLabel_2() {
if (label_2 == null) {
label_2 = new JLabel();
label_2.setText("合计:");
label_2.setBounds(313, 35, 66, 18);
}
return label_2;
}

}

都是编译成功的java源代码,希望对你有帮助!
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
切肉机刀片不转了怎么办 切肉机常见故障及维修方法 独自去五莲山风景区旅行,有怎样的感受? 独自去日照五莲山风景区是什么样的体验? SPSS详细教程 | 配对样本的t检验 SPSS | 配对样本t检验的操作方法 ansys非线性分析培训 淄博做期货的朋友,求组队 期货俱乐部是什么 西安桶装水哪个牌子好 西安桶装水哪个好 求大神给我一个Java语言编写的小程序代码 java小程序代码 谁能给个JAVA的小程序代码,越小越好! 求java经典小程序代码 微信里买的电影票,可以退票吗? 我的微信6.2.2版本的,怎么删除不了朋友圈里面好友给我的评论? 微信6.2.2版本为何没办法添加好友 微信6.2.2新从哪儿下载? 微信6.2.2怎么不能同步qq空间说说了 微信6.2.2怎样解除绑定的银行账号 微信6.2.2没有视频通话和语音通话怎么办 微信6.2.2 怎么删掉自己发的漂流瓶 微信版本过低不能抢红包微信6.2.2版本领不了红包怎么办? 苹果手机怎么下载微信6.2.2版本的 如何取消微信6.2.2绑定的手机号? 微信6.2.2版本怎么屏蔽群消息 微信6.2.2登陆不上一直载入数据 微信6.2.2里怎么找不到聊天记录迁移 在微信中怎样撤回消息6.2.2版 微信6.2.2如何取消绑定的手机号? 急求JAVA小程序代码(20分重赏) 谁能提供一个java小程序代码? java小程序代码修改 求一个JAVA小程序的源代码,要求如下 java 小程序源代码 一个JAVA小程序从代码编写到运行的过程理解是否正确? java 小程序太阳码中间的logo如何能换掉成为当前用户的头像,成为这个用户的专属二维码 JAVA小程序代码问题,求高手 求java小程序代码,500行左右。。大作业用。追加50 java小程序源代码 Java中Applet小程序的编写 急求java打字小程序的代码 东莞地铁国庆运营时间 东莞地铁2号线蛤地站首末班车时刻表 东莞地铁时间 东莞地铁2号线开通了,早上几点开车,最晚的一班是几点? 1月27日起,东莞地铁延长行车间隔,高峰期10分钟一班 东莞地铁2号线开通了,早上几点开车,最晚的是几点? 2月26日起,东莞地铁运行间隔缩短,高峰期12分钟一班 东莞寮夏最早到虎门是几点的地铁?