
正文
简单java小程序代码 java简易小程序
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何用JAVA语言编写计算器小程序?
具体代码如下:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends JFrame implements ActionListener {
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123+456-789*0.=/";
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行
String action=e.getActionCommand();
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
相关问答
Q1: 用JAVA编写一个小程序,不限类型好玩点的。
控制台小游戏 class YZDEL\r\n{\r\n public static void main(String[] args)\r\n {\r\n java.util.Random Shiji = new java.util.Random();\r\n java.util.Scanner Shuru = new java.util.Scanner(System.in);\r\n java.util.Scanner Nandu = new java.util.Scanner(System.in);\r\n int YingxHP = 20;\r\n int YingxMP = 0;\r\n int MogHP = 20; \r\n int MogMP = 0;\r\n int MogDo = 0;\r\n int fangyu = 0;\r\n System.out.println("┏━━━━_┏━━┣┣┓ _ ┓┏━┳━┳┓┓━┓ ");\r\n System.out.println("┏┣━━━┓┏━━┣┣┓ ┃┏ ┃ ┃┓┃ _ ");\r\n System.out.println("┣┣━━━┫_━━┻__ ┃_┓┃ ┃_┏━┣━━┓");\r\n System.out.println("┣┣━━━┫┏━━━━┓_━━┣━┛┗━┻━┻┛┃┃");\r\n System.out.println("┏┣━━━┓┣━━━━┫ ┃┃┃┗__┃┃┣━_");\r\n System.out.println(" ┛┗_┗━━━━_┗___━━┛┛┗━__━┛");\r\n System.out.println();\r\n System.out.println(" 作者:天狐空幻");\r\n System.out.println();\r\n System.out.println("说明:勇者的操作方式为以下所示:");\r\n System.out.println(" 使用攻击需消耗1MP 伤害1HP");\r\n System.out.println(" 使用蓄力可增加1MP 伤害0HP");\r\n System.out.println(" 使用躲闪需消耗0MP 伤害0HP 躲避攻击");\r\n System.out.println(" 使用重击需消耗3MP 伤害2HP 防御无效");\r\n System.out.println(" 恶龙攻击力高, 注意防御是取胜的关键");\r\n System.out.println(" 恶龙MP达到4时可能会放出火焰无法躲避");\r\n System.out.println(" 准备说明完毕,那么让我们来挑战恶龙吧!");\r\n System.out.println("==================================");\r\n //难度选择\r\n System.out.println("请选择难度");\r\n System.out.println("1.娱乐 2.挑战 3.噩梦");\r\n int ND = Nandu.nextInt();\r\n System.out.println("==================================");\r\n while(true) \r\n { \r\n //HP,MP的显示 \r\n System.out.print("勇者: ");\r\n System.out.print(" HP ");\r\n for(int x=YingxHP;x0;x--) System.out.print("*"); \r\n System.out.print(" "+YingxHP);\r\n System.out.println();\r\n System.out.print(" ");\r\n System.out.print(" MP ");\r\n for(int x=YingxMP;x0;x--) System.out.print("*"); \r\n System.out.print(" "+YingxMP);\r\n System.out.println();\r\n System.out.print("恶龙: ");\r\n System.out.print(" HP ");\r\n for(int y=MogHP;y0;y--) System.out.print("*");\r\n System.out.print(" "+MogHP);\r\n System.out.println(); \r\n System.out.print(" ");\r\n System.out.print(" MP ");\r\n for(int y=MogMP;y0;y--) System.out.print("*");\r\n System.out.print(" "+MogMP);\r\n System.out.println();\r\n System.out.println("==================================");\r\n //胜利判定 \r\n if(YingxHP2MogMP2) MogDo = 2;\r\n else if(MogMP0) MogDo=2; \r\n if(Do==2MogMP==0) MogDo=0; \r\n if(Do==3MogMP=4) MogDo=3;}//变态判定。。 \r\n //战斗分析\r\n //防御 \r\n if(Do==3) {fangyu=1;System.out.println("你灵巧的躲避攻击!");}\r\n if(MogDo==1) {fangyu=1;System.out.println("恶龙进行防御!");}\r\n //角色判定 \r\n if(Do==1YingxMP==0) {System.out.println("MP不足!");}\r\n if(Do==1YingxMP0) {\r\n if(fangyu==0) {MogHP=MogHP-1;YingxMP=YingxMP-1;System.out.println("你发动攻击!");}\r\n if(fangyu==1) {YingxMP=YingxMP-1;System.out.println("你的攻击被格挡!");}} \r\n if(Do==2) {YingxMP=YingxMP+1;System.out.println("你进行蓄力!");}\r\n if(Do==4YingxMP2) {MogHP=MogHP-2;YingxMP=YingxMP-3;System.out.println("你发动重击!");}\r\n if(Do 4) System.out.println("你不知所措...");\r\n //敌人判定 \r\n if(MogDo==2MogMP==0) {System.out.println("恶龙在发呆!");}\r\n if(MogDo==2MogMP0) {\r\n if(fangyu==0) {YingxHP=YingxHP-2;MogMP=MogMP-1;System.out.println("恶龙发动攻击!");}\r\n if(fangyu==1) {MogMP=MogMP-1;System.out.println("恶龙的攻击被躲开了!");}} \r\n if(MogDo==0) {MogMP=MogMP+1;System.out.println("恶龙进行蓄力!");} \r\n if(MogDo==3MogMP3) {YingxHP=YingxHP-4;MogMP=MogMP-4;System.out.println("恶龙发动火焰吐吸!躲避不能!");} \r\n //结束 \r\n fangyu = 0;\r\n MogDo = 0;\r\n System.out.println("____________________");\r\n System.out.println();\r\n System.out.println("=================================="); }\r\n }\r\n}
Q2: java一个简单小程序,求高手帮忙编写,万分感谢
class Ball {
public void play() {
System.out.println("玩球儿...");
}
}
class Football extends Ball {
public void play() {
System.out.println("使用足球运动");
}
}
class Basketball extends Ball {
public void play() {
System.out.println("使用篮球运动");
}
}
public class TestMain {
public static void main(String[] args) {
TestMain tm = new TestMain();
tm.testPlay();
}
public void testPlay() {
Ball ball = new Football();
ball.play();
ball = new Basketball();
ball.play();
}
}
/*
D:\javac TestMain.java
D:\java TestMain
使用足球运动
使用篮球运动
*/
关于简单java小程序代码和java简易小程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






