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

java 源代码 基础点的 谢谢

发布网友 发布时间:2022-05-11 03:57

我来回答

5个回答

热心网友 时间:2024-02-08 22:54

package com.regex;
import java.io.*;
import java.net.URLDecoder;
import java.util.regex.*;
public class Regex {
private int REMARK=0;
private int LOGIC=0;
private int PHYSIC=0;
boolean start=false;

/**
* @param args
*/
public static void main(String[] args) { //测试方法
// TODO Auto-generated method stub
Regex re=new Regex();
re.regCount("Regex.java");
System.out.println("remark Line: "+re.REMARK);
System.out.println("logic Line: "+re.LOGIC);
System.out.println("physic Line: "+re.PHYSIC);

}/**
* @author BlueDance
* @param s
* @deprecated count
*/
public void regCount(String s){
String url=null;
try {
url=URLDecoder.decode(this.getClass().getResource(s).getPath(),"UTF-8");
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
try {
BufferedReader br=new BufferedReader(new FileReader(new File(url)));
String s1=null;
while((s1=br.readLine())!=null){
PHYSIC++;
if(CheckChar(s1)==1){
REMARK++;
System.out.println("纯注释行:"+s1);
}
if(CheckChar(s1)==2){
LOGIC++;
REMARK++;
System.out.println("非纯注释行:"+s1);
}
if(CheckChar(s1)==3)
LOGIC++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}

}
/**
*
* @param s
* @return int
* @version check s
*/
public int CheckChar(String s){
String s1=null;
if(s!=null)
s1=s.trim();

//System.out.println(regCheck(s1,re));
if(regCheck(s1,"(//.*)")) //判断//开头的为纯注释行
return 1;
if(regCheck(s1,"(.*[;{})] *//.*)")) //判断不是//开头的非纯注释行
return 2;
if(regCheck(s1,"(//*.*)")){ //判断/*开头的纯注释行
start=true;
return 1;
}
if(regCheck(s1,"(.*[;{})]//*.*)")){ //判断不是/*开头的非纯注释行
start=true;
return 2;
}
if(regCheck(s1,"(.* */*/)")){ //判断*/结尾的纯注释行
start=false;
return 1;
}
if(regCheck(s1,"(.* */*/.*)")&&!strCheck(s1)){ //判断不是*/结尾的非纯注释行
if(strCheck(s1)){
start=false;
return 2;
}
}
if(start==true) //状态代码,start即/*开始时start=true*/结束时为false
return 1;
return 3;//ssssllll
}//aeee
/**
*
* @param s
* @param re
* @return boolean
*/
public boolean regCheck(String s,String re){ //正则表达试判断方法
return Pattern.matches(re,s);
}
public boolean strCheck(String s){ //中间有*/的字符判断 此方法最关键
if(s.indexOf("*/")>0){
int count=0;
String y[]=s.split("/*/");
boolean boo[]=new boolean[y.length];
for (int i = 0; i < y.length-1; i++) {
char c[]=y[i].toCharArray();
for (int j = 0; j < c.length; j++) {
if(c[j]=='\\'&&c[j+1]=='"'){
count++;
}
}
if(count%2==0){
if(countNumber("\"",y[i])%2!=0){
boo[i]=true;
}else{
boo[i]=false;
}
}else{
if(countNumber("\"",y[i])%2==0){
boo[i]=true;
}else{
boo[i]=false;
}
}

}
for(int i=0;i<boo.length;i++){
if(!boo[i])
return false;
}
return true;
}
return false;
}
public int countNumber(String s,String y){ //此方法为我前面写的字符串出现次数统计方法,不懂的可以看我前面的文章
int count=0;
String [] k=y.split(s);
if(y.lastIndexOf(s)==(y.length()-s.length()))
count=k.length;
else
count=k.length-1;
if(count==0)
System.out.println ("字符串\""+s+"\"在字符串\""+y+"\"没有出现过");
else
return count;
return -1;
}

}

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

public class GoodLucky extends JFrame implements ActionListener{

JTextField tf = new JTextField(); //实例化一个文本域
//设置两个按钮
JButton b1 = new JButton("开始");
JButton b2 = new JButton("停止");
boolean isGo = false;
//构造函数
public GoodLucky(){
b1.setActionCommand("start");//在开始按钮上设置一个动作监听 start
JPanel p = new JPanel(); //实例化一个可视化容器
//将两个按钮添加到可视化容器上面,用add方法
p.add(b1);
p.add(b2);
//在两个按钮上增加监听的属性,自动调用下面的监听处理方法actionPerformed(ActionEvent e),如果要代码有更好的可读性,可用内部类实现动作
//监听处理。
b1.addActionListener(this);
b2.addActionListener(this);
//将停止按钮设置为不可编辑(即不可按的状态)
b2.setEnabled(false);

this.getContentPane().add(tf,"North"); //将上面的文本域放在面板的北方,也就是上面(上北下南左西右东)
this.getContentPane().add(p,"South"); //将可视化容器pannel放在南边,也就是下面
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置用户在此窗体上发起 "close" 时默认执行的操作,参数EXIT_ON_CLOSE是使用 System exit 方法退出应用程序。仅在应用程序中使用

this.setSize(300,200); //设置面板大小,宽和高
this.setLocation(300,300); //设置面板刚开始的出现的位置
Cursor cu = new Cursor(Cursor.HAND_CURSOR); //用指定名称创建一个新的定制光标对象,参数表示手状光标类型
this.setCursor(cu); //为指定的光标设置光标图像,即设置光标图像为上面所创建的手状光标类型
this.setVisible(true); //将面板可视化设置为true,即可视,如果为false,即程序运行时面板会隐藏
tf.setText("welcome you! "); //设置面板的标题为欢迎
this.go(); //调用go方法

}

public void go(){
while(true){ //这里是死循环,也就是说用户不点击停止按钮的话他一直循环出现随机数,直到用户点击停止按钮循环才能推出,具体流程在actionPerformed方法中控制。
if(isGo == true){ //上面所定义的isGo的初始值为false,所以程序第一次到此会跳过
String s = ""; //设置空字符串
for(int j = 1; j <= 7;j++){ //产生7个随机数
int i = (int)(Math.random() * 36) + 1;//每个随机数产生方式,这里定义灵活,可以自由定义随机数产生的方式
if(i < 10){
s = s + " 0" + i; //如果产生的随机数小于10的话做处理:这里就牵扯到一个重要的概念,简单叙述一下:
/*
当一个字符串与一个整型数项相加的意思是连接,上面的s = s + " 0" + i的意思是字符串s链接0再连接整型i值,而不会导致0和整型的i相加,
产生的效果为s0i,由于s为空字符串(上面定义过的),所以当i小于零时,在个位数前面加上0,比如产生的随机数i为7的话,显示效果为 07.
*/
}else{
s = s + " " + i; //如果产生的随机数比10打的话,那么加上空格显示,即数字和数字之间有个空格
}
//以上循环循环七次,以保证能出现7个随机数
}
tf.setText(s); //将产生的随机数全部显示在文本域上,用文本域对象tf调用它的设置文本的方法setText(String)实现。
}

//以下为线程延迟
try{
Thread.sleep(10); //线程类同步方法sleep,睡眠方法,括号里的单位为ms。
}catch(java.lang.InterruptedException e){
e.printStackTrace(); //异常捕获,不用多说。
}

}

}

//以下是上面设置的事件监听的具体处理办法,即监听时间处理方法,自动调用
public void actionPerformed(ActionEvent e){ //传入一个动作事件的参数e
String s = e.getActionCommand(); //设置字符串s来存储获得动作监听,上面的start
/*
以下这个条件语句块的作用为:用户点击开始后(捕获start,用方法getActionCommand()),将命令触发设置为true,从而执行上面的go方法中的循环体(因为循环体中要求isGo参数为true,而初始为false)。
执行循环快产生随机数,并将开始按钮不可编辑化,而用户只可以使用停止按钮去停止。如果用户按下停止时,也就是没有传入参数“start”的时候,
执行else语句块中的语句,isGo设置为false,将不执行上面go中的循环语句块,从而停止产生随机数,并显示,并且把开始按钮设置为可用,而把
停止按钮设置为不可用,等待用户按下开始再去开始新一轮循环产生随机数。
*/
if(s.equals("start")){ //如果捕获到start,也就是用户触发了动作*,那么下面处理
isGo = true; //设置isGo为true
b1.setEnabled(false); //将开始按钮设置为不可用
b2.setEnabled(true); //将停止按钮设置为可用
}else{
isGo = false; //将isGo设置为false,isGo为循环标志位
b2.setEnabled(false); //设置停止按钮为不可用(注意看是b2,b2是停止按钮)
b1.setEnabled(true); //设置开始按钮为可用
}

}

public static void main(String[] args){
new GoodLucky(); //产生类的实例,执行方法
}
}

热心网友 时间:2024-02-08 22:54

//给你贴个扫雷吧,请勿商用!
/*
This class defines a class that contains some useful
attributions and some methods to set or get these attributions
*/
import javax.swing.JButton;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import java.util.Timer;
class ExButton extends JButton
{
//if the button is a mine,the isMine will be true
private boolean isMine;
//to check if a button has been visited is useful
//when using the recursion in the Game class
private boolean isVisited;
//the row number of the button
int btnRowNumber;
//the column number of the button
int btnColumnNumber;
//the mines around a button
int minesAround=0;
public void setIndex(int btnRowNumber,int btnColumnNumber)
{
this.btnRowNumber=btnRowNumber;
this.btnColumnNumber=btnColumnNumber;

}
public int getRowNumber()
{
return this.btnRowNumber;
}
public int getColumnNumber()
{
return this.btnColumnNumber;
}
public void setVisited(boolean isVisited)
{
this.isVisited=isVisited;
}
public boolean getVisited()
{
return this.isVisited;
}
public void setMine(boolean isMine)
{
this.isMine=isMine;
}
public boolean getMine()
{
return this.isMine;
}
//the attribute of minesAround add one each
//time a mine is put down around the button

public void addMinesAround()
{
this.minesAround++;
}
public int getMinesAround()
{
return this.minesAround;
}
}
//-------------------------------------------------
/*
File Name: Game.java
Author: Tian Wei Student Number: Email: xiangchensuiyue@163.com
Assignment number: #4
Description: In this program ,a frame will be created which contains
ten "mines".When you click a button ,it will present the
number of mines around or a message of losing the game
(if the button is a mine).You can make a right click to
sign a dengerous button as well.When all the mines have
been signed ,a message box of winning the game will jump
to the screen.And the the message of the time you used in
the game.More over,you can click the button on the bottom
to restart the game.
*/

public class Game extends JFrame{
//define some menber variables
private long minute=0,second=0;//take down time used int the game
private ExButton[][] btn;//two-dimension array present the buttons
private JLabel label;
private JButton restart;//restart button
private int minesRemained;//remained mines that you have not signed
private boolean thisTry=true;
private JLabel timeUsed=new JLabel ();
private Random rand=new Random();
private final int ROWS,COLUMNS;
private final int MINES;
// the constuctor
public Game(int rows,int columns,int mines)
{
super("Find mines");
this.ROWS=rows;
this.COLUMNS=columns;
this.MINES=mines;
minesRemained=MINES;
Timer timer=new Timer();//Timer's object to timer the game
timer.schele(new MyTimer(), 0, 1000);//do the function every second
Container container=getContentPane();
container.setLayout(new BorderLayout());
//Jpanel in the Container
JPanel jpanel=new JPanel();
jpanel.setLayout(new GridLayout(ROWS,COLUMNS));
restart=new JButton("click me to restart the game");
JPanel jpanel2=new JPanel();
//Another JPanel in the Container
jpanel2.setLayout(new FlowLayout());
jpanel2.add(timeUsed);
jpanel2.add(restart);
ButtonListener restartHandler=new ButtonListener();
restart.addActionListener(restartHandler);
container.add(jpanel2,BorderLayout.SOUTH);
btn=new ExButton[ROWS+2][COLUMNS+2];
//initialize the buttons
for(int i=0;i<=ROWS+1;i++)
{
for(int j=0;j<=COLUMNS+1;j++)
{
btn[i][j]=new ExButton();
btn[i][j].addMouseListener(new MouseClickHandler());
btn[i][j].setIndex(i,j);
btn[i][j].setVisited(false);
}

}
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
jpanel.add(btn[i][j]);

container.add(jpanel,BorderLayout.CENTER);
JPanel jpanel3=new JPanel ();
label=new JLabel();
label.setText("Mines remaining "+MINES);
jpanel3.add(label);

container.add(jpanel3,BorderLayout.NORTH );
this.addMines();
this.addMinesAround();
}
//randomly put ten mines
private void addMines()
{
for(int i=1;i<=MINES;i++)
{
int raInt1=rand.nextInt(ROWS);
int raInt2=rand.nextInt(COLUMNS);
if((raInt1==0)||(raInt2==0)||btn[raInt1][raInt2].getMine())
i--;
else
btn[raInt1][raInt2].setMine(true);
}
}
//take down the mines around a button
private void addMinesAround()
{
for(int i=1;i<=ROWS;i++)
{
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine())
{
btn[i][j-1].addMinesAround();
btn[i][j+1].addMinesAround();
btn[i-1][j-1].addMinesAround();
btn[i-1][j].addMinesAround();
btn[i-1][j+1].addMinesAround();
btn[i+1][j-1].addMinesAround();
btn[i+1][j].addMinesAround();
btn[i+1][j+1].addMinesAround();

}
}
}
}
//if a button clicked is a empty one,then use a recursion
//to find all the empty buttons around
private void checkEmpty(ExButton button)
{
button.setVisited(true);
int x=button.getRowNumber();
int y=button.getColumnNumber();
button.setBackground(Color.white);
if((button.getMinesAround()==0)&&(x>=1)&&(x<=ROWS)
&&(y>=1)&&(y<=COLUMNS))
{
if(!btn[x][y-1].getVisited())
checkEmpty(btn[x][y-1]);
if(!btn[x][y+1].getVisited())
checkEmpty(btn[x][y+1]);
if(!btn[x-1][y].getVisited())
checkEmpty(btn[x-1][y]);
if(!btn[x+1][y].getVisited())
checkEmpty(btn[x+1][y]);
if(!btn[x-1][y-1].getVisited())
checkEmpty(btn[x-1][y-1]);
if(!btn[x-1][y+1].getVisited())
checkEmpty(btn[x-1][y+1]);
if(!btn[x+1][y-1].getVisited())
checkEmpty(btn[x+1][y-1]);
if(!btn[x+1][y+1].getVisited())
checkEmpty(btn[x+1][y+1]);
}
else if(button.getMinesAround()>0)
button.setText(""+button.getMinesAround());

}
//the main function
public static void main(String args[])
{
String rows,columns,mines;
int rowNumber,columnNumber,mineNumber;
rows=JOptionPane.showInputDialog("Enter the rows of the game");
columns=JOptionPane.showInputDialog("Enter the columns of the game");
mines=JOptionPane.showInputDialog("Enter the mines of the game");
rowNumber=Integer.parseInt(rows);
columnNumber=Integer.parseInt(columns);
mineNumber=Integer.parseInt(mines);
Game frame=new Game(rowNumber,columnNumber,mineNumber);
frame.setTitle("Find Mines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(220, 80);
frame.setSize(600, 600 );
frame.setVisible(true);
}
//there are three inner class below

//The first inner class is used to do the mouse listener's
//function.When you click a button ,it will present the
//number of mines around or a message of losing the game
//(if the button is a mine).You can make a right click to
//sign a dengerous button as well.When ten mines have been
//signed,it will check whether all the signed ones are mines
private class MouseClickHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
//get the button that been clicked
ExButton eventButton=new ExButton();
eventButton=(ExButton)event.getSource();
eventButton.setVisited(true);
//when it is a right click
if(event.isMetaDown())
{
if(eventButton.getText()=="#")
{
minesRemained++;
eventButton.setText("");

}
else
{
if((eventButton.getBackground()==Color.white)||
(eventButton.getText()!=""))
{
//do nothing
}

else
{
minesRemained--;
eventButton.setText("#");

}
}
label.setText("Mines remaining "+minesRemained);
//check if all the signed buttons are mines
if(minesRemained==0)
{
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine()&&btn[i][j].getText()!="#")
thisTry=false;
if(!btn[i][j].getMine()&&btn[i][j].getText()=="#")
thisTry=false;

}
if(thisTry)
{
//win the game
JOptionPane.showMessageDialog(null, "You succeed" +
" in this experience!");
JOptionPane.showMessageDialog(null, "Time used:"+
timeUsed.getText());
}

else//you have wrongly signed one or more mines
JOptionPane.showMessageDialog(null, "You have wrongly " +
"signed one or more mines,please check it and go on!");

}
}
else if(event.isAltDown())
{
//do nothing
}
else
{//normally click(left click)
if(eventButton.getText()=="#")
{
//do nothing
}
else if(eventButton.getMine())
{
//lose the game
JOptionPane.showMessageDialog(null, "What a pity!" +
"You failed!" );
//show all the mines to the loser
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine())
btn[i][j].setBackground(Color.BLACK);
}
JOptionPane.showMessageDialog(null, "Time used: 0"+
minute+":"+second);

}
else
{
if(eventButton.getMinesAround()==0)
{
//call the function to find all the empty buttons around
checkEmpty(eventButton);
}
else
eventButton.setText(""+eventButton.getMinesAround());

}
}

}

}
//The second class is to listen to the button which used to
//restart the game.In this class,it will dispose the old frame
//and create a new one(Of course,the mines's position have
//been changed).
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//what to dispose is the object of Game class
Game.this.dispose();
//the same code as in the main function
Game frame=new Game(ROWS,COLUMNS,MINES);
frame.setTitle("Find Mines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600 );
//make sure the frame is at the center of the screen
frame.setLocation(220, 80);
frame.setVisible(true);
}
}
//The last class is the class that will be used in the
//Timer's object timer.It should inherit the class TimerTask
//It is the task that the Timer will do every second
private class MyTimer extends TimerTask
{
public void run()
{
second+=1;
minute+=second/60;
second=second%60;
//change the text of the time used in the game
timeUsed.setText("Time used 0"+minute+":"+second);
}

}

}
//end of the class Game

热心网友 时间:2024-02-08 22:55

package com.xuzs.test;

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

public class Calculator extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private JTextField txtResult;
private JButton btnZero, btnOne, btnTwo, btnThree, btnFour, btnFive,
btnSix, btnSeven, btnEight, btnNine, btnPlus, btnMinus, btnTimes,
btnDivided, btnEqual, btnPoint, btnC, btnCE, btnSqrt, btnPlusMinus;
int z;
double x, y;
StringBuffer str;

public Calculator() {
super("计算器");
this.setSize(311, 231);
this.setLocation(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLayout(new GridLayout(1, 1));// 网格布局
JPanel panel = new JPanel(new GridLayout(6, 1));// 面板 网格布局6行1列
this.add(panel);

txtResult = new JTextField("0");
Color BackColor = new Color(255, 255, 255);
Color ForeColor = new Color(0, 0, 0);
txtResult.setBackground(BackColor);
txtResult.setForeground(ForeColor);

panel.add(txtResult);
txtResult.setHorizontalAlignment(JTextField.RIGHT);
txtResult.setEnabled(false);
// text.setEnabled(true);

JPanel panel_1 = new JPanel(new GridLayout(1, 4));
panel.add(panel_1);

btnSqrt = new JButton("sqrt");
panel_1.add(btnSqrt);
btnSqrt.addActionListener(this);
btnPlusMinus = new JButton("+/-");
panel_1.add(btnPlusMinus);
btnPlusMinus.addActionListener(this);
btnCE = new JButton("CE");
panel_1.add(btnCE);
btnCE.addActionListener(this);
btnC = new JButton("C");
panel_1.add(btnC);
btnC.addActionListener(this);

JPanel panel_2 = new JPanel(new GridLayout(1, 4));
panel.add(panel_2);

btnSeven = new JButton("7");
panel_2.add(btnSeven);
btnSeven.addActionListener(this);
btnEight = new JButton("8");
panel_2.add(btnEight);
btnEight.addActionListener(this);
btnNine = new JButton("9");
panel_2.add(btnNine);
btnNine.addActionListener(this);
btnDivided = new JButton("/");
panel_2.add(btnDivided);
btnDivided.addActionListener(this);

JPanel panel_3 = new JPanel(new GridLayout(1, 4));
panel.add(panel_3);

btnFour = new JButton("4");
panel_3.add(btnFour);
btnFour.addActionListener(this);
btnFive = new JButton("5");
panel_3.add(btnFive);
btnFive.addActionListener(this);
btnSix = new JButton("6");
panel_3.add(btnSix);
btnSix.addActionListener(this);
btnTimes = new JButton("*");
panel_3.add(btnTimes);
btnTimes.addActionListener(this);

JPanel panel_4 = new JPanel(new GridLayout(1, 4));
panel.add(panel_4);

btnOne = new JButton("1");
panel_4.add(btnOne);
btnOne.addActionListener(this);
btnTwo = new JButton("2");
panel_4.add(btnTwo);
btnTwo.addActionListener(this);
btnThree = new JButton("3");
panel_4.add(btnThree);
btnThree.addActionListener(this);
btnMinus = new JButton("-");
panel_4.add(btnMinus);
btnMinus.addActionListener(this);

JPanel panel_5 = new JPanel(new GridLayout(1, 4));
panel.add(panel_5);

btnZero = new JButton("0");
panel_5.add(btnZero);
btnZero.addActionListener(this);
btnPoint = new JButton(".");
panel_5.add(btnPoint);
btnPoint.addActionListener(this);
btnEqual = new JButton("=");
panel_5.add(btnEqual);
btnEqual.addActionListener(this);
btnPlus = new JButton("+");
panel_5.add(btnPlus);
btnPlus.addActionListener(this);

str = new StringBuffer();

this.setVisible(true);

}

public void windowClosing(WindowEvent a) {
System.exit(0);
}

public void actionPerformed(ActionEvent e) {

try {
if (e.getSource() == btnC) {
txtResult.setText("0");
str.setLength(0);
} else if (e.getSource() == btnCE) {
txtResult.setText("0.");
str.setLength(0);
} else if (e.getSource() == btnPlusMinus) {
x = Double.parseDouble(txtResult.getText().trim());
txtResult.setText("" + (-x));
} else if (e.getSource() == btnPlus) {
x = Double.parseDouble(txtResult.getText().trim());
str.setLength(0);
y = 0d;
z = 1;
} else if (e.getSource() == btnMinus) {
x = Double.parseDouble(txtResult.getText().trim());
str.setLength(0);
y = 0d;
z = 2;
} else if (e.getSource() == btnTimes) {
x = Double.parseDouble(txtResult.getText().trim());
str.setLength(0);
y = 0d;
z = 3;
} else if (e.getSource() == btnDivided) {
x = Double.parseDouble(txtResult.getText().trim());
str.setLength(0);
y = 0d;
z = 4;
} else if (e.getSource() == btnEqual) {
str.setLength(0);
switch (z) {
case 1:
txtResult.setText("" + (x + y));
break;
case 2:
txtResult.setText("" + (x - y));
break;
case 3:
txtResult.setText("" + (x * y));
break;
case 4:
txtResult.setText("" + (x / y));
break;
}
}

else if (e.getSource() == btnPoint) {
if (txtResult.getText().trim().indexOf('.') != -1)// 判断字符串中是否已经包含了小数点
{

} else// 如果没数点有小
{
if (txtResult.getText().trim().equals("0"))// 如果初时显示为0
{
str.setLength(0);
txtResult.setText((str.append("0"
+ e.getActionCommand())).toString());
} else if (txtResult.getText().trim().equals(""))// 如果初时显示为空则不做任何操作
{
} else {
txtResult.setText(str.append(e.getActionCommand())
.toString());
}
}

y = 0d;
}

else if (e.getSource() == btnSqrt)// 求平方根
{
x = Double.parseDouble(txtResult.getText().trim());
txtResult.setText("数字格式异常");
if (x < 0)
txtResult.setText("负数没有平方根");
else
txtResult.setText("" + Math.sqrt(x));
str.setLength(0);
y = 0d;
}

else if (e.getSource() == btnZero)// 如果选择的是"0"这个数字键
{
if (txtResult.getText().trim().equals("0"))// 如果显示屏显示的为零不做操作
{
} else {
txtResult.setText(str.append(e.getActionCommand())
.toString());
y = Double.parseDouble(txtResult.getText().trim());
}
}

else// 其他的数字键
{
txtResult.setText(str.append(e.getActionCommand()).toString());
y = Double.parseDouble(txtResult.getText().trim());
}
} catch (NumberFormatException ae) {
txtResult.setText("数字格式异常");
} catch (StringIndexOutOfBoundsException ae) {
txtResult.setText("字符串索引越界");
}

}

public static void main(String arg[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new Calculator();
}

}

热心网友 时间:2024-02-08 22:55

我也贴一个:
用Java写的记事本,同样的,仅供交流,请勿商用!
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

class Remember extends JFrame implements TreeSelectionListener,ActionListener
{
JTree tree=null;
JTextArea text=new JTextArea(20,20);
int i=0;
DefaultMutableTreeNode root;
JButton b_save=new JButton("保存日记");
JButton b_del=new JButton("删除日记");
DefaultMutableTreeNode month[]=new DefaultMutableTreeNode[13];

Remember()
{
Container con=getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("日历记事本");
for(i=1;i<=12;i++)
{
month=new DefaultMutableTreeNode(""+i+"月");
root.add(month);
}
for(i=1;i<=12;i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{
for(int j=1;j<=31;j++)
month.add(new DefaultMutableTreeNode(j+"日"));
}
else if(i==4||i==6||i==9||i==11)
{
for(int j=1;j<=30;j++)
month.add(new DefaultMutableTreeNode(j+"日"));
}
else
{
for (int j=1;j<=28;j++)
month.add(new DefaultMutableTreeNode(j+"日"));
}
}

b_save.addActionListener(this);
b_del.addActionListener(this);
tree=new JTree(root);
JPanel p=new JPanel();
p.setLayout(new BorderLayout());
JScrollPane scrollpane_1=new JScrollPane(text);
p.add(scrollpane_1,BorderLayout.CENTER);
JPanel p_1=new JPanel();
p_1.add(b_save);
p_1.add(b_del);
p.add(p_1,BorderLayout.NORTH);
JScrollPane scrollpane_2=new JScrollPane(tree);
JSplitPane splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollpane_2,p);
tree.addTreeSelectionListener(this);
con.add(splitpane);
setVisible(true);
setBounds(70,80,460,320);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void valueChanged(TreeSelectionEvent e)
{
text.setText(null);
if(e.getSource()==tree)
{
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(node.isLeaf())
{
String str=node.toString();
for(int i=0;i<=12;i++)
{
if(node.getParent()==month)
{
try{
String temp=null;
File f=new File(node.getParent().toString()+str+".txt");
FileReader file=new FileReader(f);
BufferedReader in=new BufferedReader(file);
while((temp=in.readLine())!=null)
text.append(temp+'\n');
file.close();
in.close();
}
catch(Exception el){}
}
}
}
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b_save)
{
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
String str=node.toString();
if(node.isLeaf())
{
try
{
File f=new File(node.getParent().toString()+str+".txt");
FileWriter tofile=new FileWriter(f);
BufferedWriter out=new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.flush();
tofile.close();
out.close();
}
catch(Exception el){}
}
}
else if(e.getSource()==b_del)
{
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
String str=node.toString();
if(node.isLeaf())
{
File f=new File(node.getParent().toString()+str+".txt");
f.delete();
}
}
}
}

public class Example17_13 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Remember win=new Remember();
win.validate();
}
}

热心网友 时间:2024-02-08 22:56

汗~~~这叫300行。。。
java必背代码入门(java语言必背代码大全)

java入门级编程训练,完整代码?1、D:\Java\jdk0\bin目录下都是jdk的工具,使用java编程主要用到的是javac.exe、java.exe这两个命令行工具。具体使用步骤如下:配置java系统环境变量:新建文本文件,复制下面代码,然后另存为jdk.bat文件。2、Java作为一种常见的编程语言可以做很多事情,通过在北大青鸟...

java必背代码(java常用代码大全)

我需要一段最简单的java代码程序1、最简单的java代码肯定就是这个了,如下:publicclassMyFirstApp{publicstaticvoidmain(String[]args){System.out.print(Helloworld);}}“helloworld”就是应该是所有学java的新手看的第一个代码了。2、publicclassHelloWorld{publicstaticvoidmain(String[]args){System.out....

java入门基础知识

java入门基础知识如下:一、JAVA运行机制。Java程序的运行必须经过编写、编译、运行三个步骤。编写是指在Java开发环境中进行程序代码的输入,最终形成后缀名为.java的Java源文件。编译是指使用Java编译器(JDK中)对源文件进行错误排查的过程,编译后将生成后缀名为.class的字节码文件,这不像C语言那样最终生...

Java入门代码?

一个简单的Java程序代码?1、最简单的java代码肯定就是这个了,如下:publicclassMyFirstApp{publicstaticvoidmain(String[]args){System.out.print(Helloworld);}}“helloworld”就是应该是所有学java的新手看的第一个代码了。2、编写java代码,新建文本文件,编写一个简单的java程序,然后另存为Hello.java。

Java基础有哪些知识点?

第一阶段:java基础 本阶段除了JavaSE中要求大家必会的java基础知识外,重点加强了数据结构思想、集合源码分析、jdk9-11新特性的学习。第二阶段:数据库 本阶段主要学习MySQL数据库知识,通过层层递进,让大家掌握开发使用的数据是如何存储和处理的。并且逐步深入学习到索引和优化、锁机制、存储过程等。第三...

java编程基础,输入1-7显示对应星期,求大神的源码和解体思路

这个貌似不难吧!写个switch(表达式){ case 常量表达式1:语句1;当参数i=1 时 打印周一 以下类同!...case 常量表达式2:语句2;default:语句; 这个意思是如果输入的不是1—7那么你可以打印一个输入了无效数据 }语句就搞定了 望采纳,谢谢!

java初学者代码例子(java初学者经典代码)

各位大神,Java新手,以下代码帮看看,谢谢add(newJLabel(lightIcon));add(newJLabel(roomIcon));这个里面传桐稿的参数不对局返孝,世樱你看看文档 初学者请求java源代码classCar { //车辆属性 privateStringbrand;//品牌 privatedoubleengineDisplacement;//排气量 privatedoublespeed;//当前速度 private...

求Java高手给一简单的源代码(200行以下),最好有注解和一段简单的...

//导入包 import java.util.Scanner;/ 上机阶段4:登录并购买商品 / // 声明类Goods public class Goods { // 声明String类型的数组:goods,并初始化。String[] goods = new String[] { "电风扇", "洗衣机", "电视机", "冰 箱", "空调机" }; // 定义数组是干什么用的 // 声明...

求编写一个超级简单的Java的程序源代码

代码如下:public class complie { int i,j;public complie(int i,int j)//构建一个复数类 { this.i=i;this.j=j;} complie add(complie c)//复数加法 { int l,k;l=c.i+i;k=c.j+j;return (new complie(l,k));} complie cut(complie c)//复数减法 { int l,k;l=i-c.i;k...

什么是java源代码 怎么查看

你说的java源代码是指编译成的class文件前的java文件。当我们运行.java文件时,它会被系统编译成.class文件,例如Test.java编译之后就是Test.class,源文件就是指Test.java文件,一般部署项目时,有.class文件就可以发布运行了,但是如果想修改这个系统,.class是不能修改的,要有.java文件才能修改 也...

java源代码文件的扩展名 javajava基础教程 java源代码怎么用 java简单记事本源代码 javaweb购物车源代码 java图书管理系统源代码 java管理系统源代码 java人资管理系统源代码 java的基础知识
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
聊城火车站候车厅,送人能进去么?急等,谢谢。 聊城火车站有存放电车的地方吗 聊城火车站附近有没有网吧? 甲状腺结节5X4大吗? 甲状腺结节,4mm大,偏低回声,边界欠清晰,血流信号不丰富严重吗 甲状腺结节(5mm*4mm)和桥本,自己能好吗,除了定期检查,就没有治愈的办法... 甲状腺检查发现有个结节4mm*5mm还说有钙化 今天同学生日,我想找一个用标点符号组成的生日蛋糕,谁有啊,能告诉我... 请问宋氏三姐妹和宋子文之间有血缘关系吗? Macromedia Flash MX 2004中的文本工具问题,急!!! 如何在网页中添加时间? 关于图片展示代码 CSS中的基础题目: cursor是什么功能;其属性hand表示什么?相应的它还有... 翻译中华少年 还有什么英语春联,快一点告诉我,听见没,就算我求你,好不好么? 杂样克服在某些网页中不能复制,不能下载?(最好用源代码的形式回答我... 如何制作弹出多个窗口 继承qgraphicsview类怎么重新实现painteven 英语作文翻译!速度!+准确(不用太好,高一水平就好) 如何注销掉自己的支付宝帐户 有没有不参加高考就上大学的吗 最好是徐州的。不高考上大学那个叫做什么方案? 不参加高考怎么上大学 2012考研,想知道苏州大学和华中科技大学哪个在生物方面更强些? 没有参加高考可以上什么大学,我才20岁不想参加成人自考 在苏州想读MBA 苏州园区独墅湖高教区有哪些学校? 有什么大学靠近苏州??、 不参加高考能上什么大学 几所大学研究生院计算机专业的比较 苏州有哪些大学?希望详细点,谢谢! error C2679: binary &#39;+&#39; no operator found which takes a right-hand operand of type &#39;const cha 请教源码修改 以下代码如何改为向右移动,目前是向上的 请问梅干菜怎么?吃 物理速度时间图像 高中物理 速度时间 图像求大神解析 初中物理速度图像 物理,速度时间图像,分别说明abcdef点的含义 高中物理。在速度时间图像中 物理的速度时间图像各点和线段分别代表什么,方向 要怎么判断,为什么说速度为0后会反方向加速 准妈妈在生产前吃什么食物会比较好呢? 孕妇经常吃什么食物对生产有帮助呢? 孕妇生产前吃什么好 产妇生产时吃什么才能有力气生娃?有推荐的吗? 什么是线切割?优点有哪些? 孕妇吃什么有助于生产 简述电火花切割加工的特点 孕妇快生了吃什么好 生孩子吃什么 急!电火花成型加工和电火花线切割加工各有什么特点?它们加工对象上有什么不同? 孕妇生产吃什么补充能量