成都网站建设设计

将想法与焦点和您一起共享

java浪漫的源代码 java程序源代码

java代码解析

一楼的说的够全面了,不过稍有误解.

网站设计制作过程拒绝使用模板建站;使用PHP+MYSQL原生开发可交付网站源代码;符合网站优化排名的后台管理系统;成都网站制作、网站设计收费合理;免费进行网站备案等企业网站建设一条龙服务.我们是一家持续稳定运营了10余年的创新互联网站建设公司。

再来表示抱歉,我对编程语言中的中文名词非常不了解,所以如果以下的回复对你的阅读或者理解造成困难,请见谅.

1.首先,要明白这个问题的答案,需要了解call (pass) by value 和 call (pass) by reference 的区别.简单来说:

call by value通常是复制这个parameter的值去另外一块内存里,然后传给function, 所以在method/function里边对这个变量的所有变更,实际上都是对复制过来的镜像进行操作,不会对原本的variable有任何影响.

call by reference是将parameter的reference传给function,简单点理解就是直接把variable传给function.所以说这个variable的值是可以被function改变的.这个用法在c/c++中非常常见,用法是variable_name.

2.再来,在Java里边,你可以很简单的理解为: Java中只有call by value, 也就是说,所以所有传给function的parameter本身都不会被改变. (这是最简单直白的理解,当然也有另一种常从sun的人那边听到的说法:Java是call by value + call by reference by value)

3.那么现在的问题就是为什么第二个结果是2了. 首先说一下sun官方的解释: 对于reference type在作为parameter/argument的时候,也是call by value, 但是在你拥有足够权限时(比方说那个变量是public的, 不是final的等等各种符合的情况),可以修改这个object中fields的值(也就是属于这个object(严谨点讲是an instance of the object) 内部的变量, 在你的例子中, ko 里边的 a 就是一个field, 所以update(ko)会使ko.a变成2).

4.如果你是一个有过c/c++学习经验的人或者你以上的解释很难理解,以下这种说法或许更适合你 (当然了,这只是大多包括我在内有c经验的人的一种理解方式)

这里可以引入一个新的概念,pointer. 这是一种比较特殊的变量,它内部所储存的东西,其实只是另外一个变量的内存地址. 如果对内存没有概念,你可以把它简单理解为是风筝的线轴,虽然看它本身看不出什么端倪,但是顺着摸过去总会找到风筝,看到它是什么样子. 以pointer方式理解Java的人,通常会说: Type variable = new Type(); 这个过程中,最后生成的这个variable其实就是一个pointer,而不是instance本身.

在Java中, 有c/c++经验的人通常认为Java是call by value.同时,当一个变量用在储存reference type的时候,实际上储存的是它的pointer,这也一样可以解释为什么ko.a会有2这个结果,因为虽然pointer被传到function里边时,本身是call by value,无法被改变.但这并不影响function本身对这个pointer指向的object的内容做任何改变. 当然,再次声明,这只是一种帮助有c/c++经验的人理解的方法. Sun本身严正声明Java里边没有pointer这个东西的存在.

5. 再来解释一下为什么说楼上所说的(或者说楼上引用的)理解略有偏差.

引用"我们上面刚学习了JAVA的数据类型,则有:值类型就是按值传递的,而引用类型是按引用传递的" 这句话很明显的有两点错误. 第一点,如果我上面所说的,Java是没有call by reference的.

第二点,暂且假设Java里边是有call by reference的, 这句话依然不成立.

Java中的变量有两种类型: primitive types 和 reference type.

primitive type包括byte, short, int, long, char, boolean, float和double.

而这8种之外的所有的,都是reference type.

下面是一段对你的贴上来的code的一点延伸,希望可以帮助你更好的理解Java中的argument / parameter到底是如何运作的.

public class Test {

public static void main(String[] args) {

int a = 1;

Koo koo = new Koo();

Object o = new Integer(1);

Koo newKoo = new Koo();

update(a);

update(koo);

update(o);

update(newKoo);

newUpdate(newKoo);

System.out.println(a);

System.out.println(koo.a);

System.out.println(o);

System.out.println(newKoo.a);

}

static void update(int a) {

a++;

}

static void update(Koo koo) {

koo.a++;

}

static void update(Object o) {

o = (int) (Integer.parseInt(o.toString()) + 1);

}

static void newUpdate(Koo koo) {

koo = new Koo();

}

}

class Koo {

int a = 1;

}

/*

o = (int) (Integer.parseInt(o.toString()) + 1); 这一行中的(int)纯粹是多余的,是否有这个casting对code本身没有任何影响. 如果你高兴也可以用

o = new Integer(Integer.parseInt(o.toString()) + 1);

或者干脆

o = Integer.parseInt(o.toString()) + 1;

*/

以上这些code运行之后会得到1 2 1 2的结果. 后面两个结果可以很好的说明, 即使对objects (reference type variables) 来看, Java所应用的也并不是call by reference. 否则的话,以上code运行结果应该是1 2 2 1

希望你可以真正理解这个新的例子中,产生1212这个结果的原因,从而对Java中的arguments有一个系统全面的认识.

图片是相关资料的链接,知道里貌似不能加网址

跪求游戏java源代码

给你一个俄罗斯方块的把!!

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.*;

import javax.swing.Timer;

public class Tetris extends JFrame {

public Tetris() {

Tetrisblok a = new Tetrisblok();

addKeyListener(a);

add(a);

}

public static void main(String[] args) {

Tetris frame = new Tetris();

JMenuBar menu = new JMenuBar();

frame.setJMenuBar(menu);

JMenu game = new JMenu("游戏");

JMenuItem newgame = game.add("新游戏");

JMenuItem pause = game.add("暂停");

JMenuItem goon = game.add("继续");

JMenuItem exit = game.add("退出");

JMenu help = new JMenu("帮助");

JMenuItem about = help.add("关于");

menu.add(game);

menu.add(help);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(220, 275);

frame.setTitle("Tetris内测版");

// frame.setUndecorated(true);

frame.setVisible(true);

frame.setResizable(false);

}

}

// 创建一个俄罗斯方块类

class Tetrisblok extends JPanel implements KeyListener {

// blockType 代表方块类型

// turnState代表方块状态

private int blockType;

private int score = 0;

private int turnState;

private int x;

private int y;

private int i = 0;

int j = 0;

int flag = 0;

// 定义已经放下的方块x=0-11,y=0-21;

int[][] map = new int[13][23];

// 方块的形状 第一组代表方块类型有S、Z、L、J、I、O、T 7种 第二组 代表旋转几次 第三四组为 方块矩阵

private final int shapes[][][] = new int[][][] {

// i

{ { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },

{ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 } },

// s

{ { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },

{ 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 } },

// z

{ { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 } },

// j

{ { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },

{ 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },

// o

{ { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },

// l

{ { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },

{ 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },

{ 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },

// t

{ { 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },

{ 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 } } };

// 生成新方块的方法

public void newblock() {

blockType = (int) (Math.random() * 1000) % 7;

turnState = (int) (Math.random() * 1000) % 4;

x = 4;

y = 0;

if (gameover(x, y) == 1) {

newmap();

drawwall();

score = 0;

JOptionPane.showMessageDialog(null, "GAME OVER");

}

}

// 画围墙

public void drawwall() {

for (i = 0; i 12; i++) {

map[i][21] = 2;

}

for (j = 0; j 22; j++) {

map[11][j] = 2;

map[0][j] = 2;

}

}

// 初始化地图

public void newmap() {

for (i = 0; i 12; i++) {

for (j = 0; j 22; j++) {

map[i][j] = 0;

}

}

}

// 初始化构造方法

Tetrisblok() {

newblock();

newmap();

drawwall();

Timer timer = new Timer(1000, new TimerListener());

timer.start();

}

// 旋转的方法

public void turn() {

int tempturnState = turnState;

turnState = (turnState + 1) % 4;

if (blow(x, y, blockType, turnState) == 1) {

}

if (blow(x, y, blockType, turnState) == 0) {

turnState = tempturnState;

}

repaint();

}

// 左移的方法

public void left() {

if (blow(x - 1, y, blockType, turnState) == 1) {

x = x - 1;

}

;

repaint();

}

// 右移的方法

public void right() {

if (blow(x + 1, y, blockType, turnState) == 1) {

x = x + 1;

}

;

repaint();

}

// 下落的方法

public void down() {

if (blow(x, y + 1, blockType, turnState) == 1) {

y = y + 1;

delline();

}

;

if (blow(x, y + 1, blockType, turnState) == 0) {

add(x, y, blockType, turnState);

newblock();

delline();

}

;

repaint();

}

// 是否合法的方法

public int blow(int x, int y, int blockType, int turnState) {

for (int a = 0; a 4; a++) {

for (int b = 0; b 4; b++) {

if (((shapes[blockType][turnState][a * 4 + b] == 1) (map[x

+ b + 1][y + a] == 1))

|| ((shapes[blockType][turnState][a * 4 + b] == 1) (map[x

+ b + 1][y + a] == 2))) {

return 0;

}

}

}

return 1;

}

// 消行的方法

public void delline() {

int c = 0;

for (int b = 0; b 22; b++) {

for (int a = 0; a 12; a++) {

if (map[a][b] == 1) {

c = c + 1;

if (c == 10) {

score += 10;

for (int d = b; d 0; d--) {

for (int e = 0; e 11; e++) {

map[e][d] = map[e][d - 1];

}

}

}

}

}

c = 0;

}

}

// 判断你挂的方法

public int gameover(int x, int y) {

if (blow(x, y, blockType, turnState) == 0) {

return 1;

}

return 0;

}

// 把当前添加map

public void add(int x, int y, int blockType, int turnState) {

int j = 0;

for (int a = 0; a 4; a++) {

for (int b = 0; b 4; b++) {

if (map[x + b + 1][y + a] == 0) {

map[x + b + 1][y + a] = shapes[blockType][turnState][j];

}

;

j++;

}

}

}

// 画方块的的方法

public void paintComponent(Graphics g) {

super.paintComponent(g);

// 画当前方块

for (j = 0; j 16; j++) {

if (shapes[blockType][turnState][j] == 1) {

g.fillRect((j % 4 + x + 1) * 10, (j / 4 + y) * 10, 10, 10);

}

}

// 画已经固定的方块

for (j = 0; j 22; j++) {

for (i = 0; i 12; i++) {

if (map[i][j] == 1) {

g.fillRect(i * 10, j * 10, 10, 10);

}

if (map[i][j] == 2) {

g.drawRect(i * 10, j * 10, 10, 10);

}

}

}

g.drawString("score=" + score, 125, 10);

g.drawString("抵制不良游戏,", 125, 50);

g.drawString("拒绝盗版游戏。", 125, 70);

g.drawString("注意自我保护,", 125, 90);

g.drawString("谨防受骗上当。", 125, 110);

g.drawString("适度游戏益脑,", 125, 130);

g.drawString("沉迷游戏伤身。", 125, 150);

g.drawString("合理安排时间,", 125, 170);

g.drawString("享受健康生活。", 125, 190);

}

// 键盘监听

public void keyPressed(KeyEvent e) {

switch (e.getKeyCode()) {

case KeyEvent.VK_DOWN:

down();

break;

case KeyEvent.VK_UP:

turn();

break;

case KeyEvent.VK_RIGHT:

right();

break;

case KeyEvent.VK_LEFT:

left();

break;

}

}

// 无用

public void keyReleased(KeyEvent e) {

}

// 无用

public void keyTyped(KeyEvent e) {

}

// 定时器监听

class TimerListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

repaint();

if (blow(x, y + 1, blockType, turnState) == 1) {

y = y + 1;

delline();

}

;

if (blow(x, y + 1, blockType, turnState) == 0) {

if (flag == 1) {

add(x, y, blockType, turnState);

delline();

newblock();

flag = 0;

}

flag = 1;

}

;

}

}

}

求一个用JAVA写的简单的记事本源代码程序

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.awt.datatransfer.*;

class MyMenuBar extends MenuBar{

public MyMenuBar(Frame parent){

parent.setMenuBar(this);

}

public void addMenus(String [] menus){

for(int i=0;imenus.length;i++)

add(new Menu(menus[i]));

}

public void addMenuItems(int menuNumber,String[] items){

for(int i=0;iitems.length;i++){

if(items[i]!=null)

getMenu(menuNumber).add(new MenuItem(items[i]));

else getMenu(menuNumber).addSeparator();

}

}

public void addActionListener(ActionListener al){

for(int i=0;igetMenuCount();i++)

for(int j=0;jgetMenu(i).getItemCount();j++)

getMenu(i).getItem(j).addActionListener(al);

}

}

class MyFile{

private FileDialog fDlg;

public MyFile(Frame parent){

fDlg=new FileDialog(parent,"",FileDialog.LOAD);

}

private String getPath(){

return fDlg.getDirectory()+"\\"+fDlg.getFile();

}

public String getData() throws IOException{

fDlg.setTitle("打开");

fDlg.setMode(FileDialog.LOAD);

fDlg.setVisible(true);

BufferedReader br=new BufferedReader(new FileReader(getPath()));

StringBuffer sb=new StringBuffer();

String aline;

while((aline=br.readLine())!=null)

sb.append(aline+'\n');

br.close();

return sb.toString();

}

public void setData(String data) throws IOException{

fDlg.setTitle("保存");

fDlg.setMode(FileDialog.SAVE);

fDlg.setVisible(true);

BufferedWriter bw=new BufferedWriter(new FileWriter(getPath()));

bw.write(data);

bw.close();

}

}

class MyClipboard{

private Clipboard cb;

public MyClipboard(){

cb=Toolkit.getDefaultToolkit().getSystemClipboard();

}

public void setData(String data){

cb.setContents(new StringSelection(data),null);

}

public String getData(){

Transferable content=cb.getContents(null);

try{

return (String) content.getTransferData(DataFlavor.stringFlavor);

//DataFlavor.stringFlavor会将剪贴板中的字符串转换成Unicode码形式的String对象。

//DataFlavor类是与存储在剪贴板上的数据的形式有关的类。

}catch(Exception ue){}

return null;

}

}

class MyFindDialog extends Dialog implements ActionListener{

private Label lFind=new Label("查找字符串");

private Label lReplace=new Label("替换字符串");

private TextField tFind=new TextField(10);

private TextField tReplace=new TextField(10);

private Button bFind=new Button("查找");

private Button bReplace=new Button("替换");

private TextArea ta;

public MyFindDialog(Frame owner,TextArea ta){

super(owner,"查找",false);

this.ta=ta;

setLayout(null);

lFind.setBounds(10,30,80,20);

lReplace.setBounds(10,70,80,20);

tFind.setBounds(90,30,90,20);

tReplace.setBounds(90,70,90,20);

bFind.setBounds(190,30,80,20);

bReplace.setBounds(190,70,80,20);

add(lFind);

add(tFind);

add(bFind);

add(lReplace);

add(tReplace);

add(bReplace);

setResizable(false);

bFind.addActionListener(this);

bReplace.addActionListener(this);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

MyFindDialog.this.dispose();

}

});

}//构造函数结束

public void showFind(){

setTitle("查找");

setSize(280,60);

setVisible(true);

}

public void showReplace(){

setTitle("查找替换");

setSize(280,110);

setVisible(true);

}

private void find(){

String text=ta.getText();

String str=tFind.getText();

int end=text.length();

int len=str.length();

int start=ta.getSelectionEnd();

if(start==end) start=0;

for(;start=end-len;start++){

if(text.substring(start,start+len).equals(str)){

ta.setSelectionStart(start);

ta.setSelectionEnd(start+len);

return;

}

}

//若找不到待查字符串,则将光标置于末尾

ta.setSelectionStart(end);

ta.setSelectionEnd(end);

}

public Button getBFind() {

return bFind;

}

private void replace(){

String str=tReplace.getText();

if(ta.getSelectedText().equals(tFind.getText()))

ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd());

else find();

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==bFind)

find();

else if(e.getSource()==bReplace)

replace();

}

}

public class MyMemo extends Frame implements ActionListener{

private TextArea editor=new TextArea(); //可编辑的TextArea

private MyFile mf=new MyFile(this);//MyFile对象

private MyClipboard cb=new MyClipboard();

private MyFindDialog findDlg=new MyFindDialog(this,editor);

public MyMemo(String title){ //构造函数

super(title);

MyMenuBar mb=new MyMenuBar(this);

//添加需要的菜单及菜单项

mb.addMenus(new String[]{"文件","编辑","查找","帮助"});

mb.addMenuItems(0,new String[]{"新建","打开","保存",null,"全选"});

mb.addMenuItems(1,new String[]{"剪贴","复制","粘贴","清除",null,"全选"});

mb.addMenuItems(2,new String[]{"查找",null,"查找替换"});

mb.addMenuItems(3,new String[]{"我的记事本信息"});

add(editor); //为菜单项注册动作时间监听器

mb.addActionListener(this);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

MyMemo.this.dispose();

}

}); //分号不能忘了

} //构造函数完

public void actionPerformed(ActionEvent e){

String selected=e.getActionCommand(); //获取菜单项标题

if(selected.equals("新建"))

editor.setText("");

else if(selected.equals("打开")){

try{

editor.setText(mf.getData());

}catch(IOException ie){}

}

else if(selected.equals("保存")){

try{

mf.setData(editor.getText());

}catch(IOException ie){}

}

else if(selected.equals("退出")){

dispose();

}

else if(selected.equals("剪贴")){

//将选中的字符串复制到剪贴板中并清除字符串

cb.setData(editor.getSelectedText());

editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());

}

else if(selected.equals("复制")){

cb.setData(editor.getSelectedText());

}

else if(selected.equals("粘贴")){

String str=cb.getData();

editor.replaceRange(str,editor.getSelectionStart(),editor.getSelectionEnd());

//粘贴在光标位置

}

else if(selected.equals("清除")){

editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());

}

else if(selected.equals("全选")){

editor.setSelectionStart(0);

editor.setSelectionEnd(editor.getText().length());

}

else if(selected.equals("查找")){

findDlg.showFind();

}

else if(selected.equals("查找替换")){

findDlg.showReplace();

}

}

public static void main(String[] args){

MyMemo memo=new MyMemo("记事本");

memo.setSize(650,450);

memo.setVisible(true);

}

}

Java相关问题,求助源代码

第一种

package com.at.java;

class PIThread extends Thread{

public void run(){

double num=1,PI=0;int i=0;

while(num0.001) {

num=(double)4/(2*i+++1);

if(i%2==0)PI-=num;

else PI+=num;

System.out.println("派的数值为"+PI);

if(num=0.001)System.out.println("××××××××××派的数值计算已经结束××××××××××");

}

}

}

class EThread extends Thread{

public void run(){

double num=1,E=0;int i=1;

while(1/num0.00000001) {

num=num*i++;

E+=1/num;

System.out.println("自然对数E的数值为"+E);

if(1/num=0.00000001)System.out.println("××××××××××自然对数的数值计算已经结束××××××××××");

}

}

}

public class JustTest {

public static void main(String args[]) {

PIThread p=new PIThread();

EThread e=new EThread();

p.start();

e.start();

int sum=0;

for(int i=1;i1001;i++)

{

sum+=i;

System.out.println(sum);

}

System.out.println("×××××××××××××自动求和1到1000已经结束××××××××××××××××");

}

}

第二种

package com.at.java;

class PIThread implements Runnable{

public void run(){

double num=1,PI=0;int i=0;

while(num0.001) {

num=(double)4/(2*i+++1);

if(i%2==0)PI-=num;

else PI+=num;

System.out.println("派的数值为"+PI);

if(num=0.001)System.out.println("××××××××××派的数值计算已经结束××××××××××");

}

}

}

class EThread implements Runnable{

public void run(){

double num=1,E=0;int i=1;

while(1/num0.00000001) {

num=num*i++;

E+=1/num;

System.out.println("自然对数E的数值为"+E);

if(1/num=0.00000001)System.out.println("××××××××××自然对数的数值计算已经结束××××××××××");

}

}

}

public class JustTest {

public static void main(String args[]) {

PIThread p=new PIThread();

EThread e=new EThread();

Thread t1=new Thread(p);

Thread t2=new Thread(e);

t1.start();

t2.start();

int sum=0;

for(int i=1;i1001;i++)

{

sum+=i;

System.out.println(sum);

}

System.out.println("×××××××××××××自动求和1到1000已经结束××××××××××××××××");

}

}

都已经验证过,正常运行

java简单记事本源代码 带解释

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

class test implements ActionListener

{

JFrame frame;

JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31;

JTextArea ta;

JPanel p1,p2,p3,p4;

JMenuBar mb;

JMenu m1,m2,m3;

JMenuItem mt1,mt2,mt3,mt4,mt5,mt6,mt7;

JRadioButton rb1,rb2;

ButtonGroup bg;

Double d1=0.0,d2=0.0,d3=0.0,d4=1.0,d5=1.0;

String s1="",s2="",s3="",s4="";

int a=0;

char c1;

int i=0;

public static void main(String[] args)

{

test that=new test();

that.go();

}

public void go()

{

frame=new JFrame("计算器");

Container cp= frame.getContentPane();

cp.setLayout(new FlowLayout());

b1=new JButton("7");b2=new JButton("8");b3=new JButton("9");b4=new JButton("/");b5=new JButton("1/x");b6=new JButton("sin");b7=new JButton("log");

b8=new JButton("4");b9=new JButton("5");b10=new JButton("6");b11=new JButton("*");b12=new JButton("x^y");b13=new JButton("cos");b14=new JButton("ln");

b15=new JButton("1");b16=new JButton("2");b17=new JButton("3");b18=new JButton("-");b19=new JButton(new ImageIcon("lanying.gif"));b20=new JButton("tan");b21=new JButton("x^3");

b22=new JButton("0");b23=new JButton("+/-");b24=new JButton(".");b25=new JButton("+");b26=new JButton("√x");b27=new JButton("cot");b28=new JButton("x^2");

b29=new JButton("Backspace");b30=new JButton("C");b31=new JButton("=");

mb=new JMenuBar();

m1=new JMenu("文件(F)");m2=new JMenu("编辑(E)");m3=new JMenu("帮助(H)");

mt1=new JMenuItem("清零");mt2=new JMenuItem("退出");mt3=new JMenuItem("复制");mt4=new JMenuItem("粘贴");mt5=new JMenuItem("版本");mt6=new JMenuItem("标准型");mt7=new JMenuItem("科学型");

ta=new JTextArea(1,30);

p1=new JPanel();p2=new JPanel();p3=new JPanel();p4=new JPanel();

rb1=new JRadioButton("科学型");rb2=new JRadioButton("标准型");

bg=new ButtonGroup();

b1.setForeground(Color.blue);b1.setBackground(Color.white);b2.setForeground(Color.blue);b2.setBackground(Color.white);

b3.setForeground(Color.blue);b3.setBackground(Color.white);b8.setForeground(Color.blue);b8.setBackground(Color.white);

b9.setForeground(Color.blue);b9.setBackground(Color.white);b10.setForeground(Color.blue);b10.setBackground(Color.white);

b15.setForeground(Color.blue);b15.setBackground(Color.white);b16.setForeground(Color.blue);b16.setBackground(Color.white);

b17.setForeground(Color.blue);b17.setBackground(Color.white);b22.setForeground(Color.blue);b22.setBackground(Color.white);

b23.setForeground(Color.blue);b23.setBackground(Color.white);b24.setForeground(Color.blue);b24.setBackground(Color.white);

b4.setForeground(Color.red);b4.setBackground(Color.white);b11.setForeground(Color.red);b11.setBackground(Color.white);

b18.setForeground(Color.red);b18.setBackground(Color.white);b25.setForeground(Color.red);b25.setBackground(Color.white);

b5.setForeground(Color.blue);b5.setBackground(Color.white);b6.setForeground(Color.blue);b6.setBackground(Color.white);

b7.setForeground(Color.blue);b7.setBackground(Color.white);b12.setForeground(Color.blue);b12.setBackground(Color.white);

b13.setForeground(Color.blue);b13.setBackground(Color.white);b14.setForeground(Color.blue);b14.setBackground(Color.white);

b19.setForeground(Color.blue);b19.setBackground(Color.white);b20.setForeground(Color.blue);b20.setBackground(Color.white);

b21.setForeground(Color.blue);b21.setBackground(Color.white);b26.setForeground(Color.blue);b26.setBackground(Color.white);

b27.setForeground(Color.blue);b27.setBackground(Color.white);b28.setForeground(Color.blue);b28.setBackground(Color.white);

b29.setForeground(Color.red);b29.setBackground(Color.white);b30.setForeground(Color.red);b30.setBackground(Color.white);

b31.setForeground(Color.red);b31.setBackground(Color.white);

bg.add(rb1);bg.add(rb2);

p1.setBackground(Color.yellow);

cp.setBackground(Color.CYAN);

m1.setMnemonic(KeyEvent.VK_F);m2.setMnemonic(KeyEvent.VK_E);m3.setMnemonic(KeyEvent.VK_H);

m1.add(mt6);m1.add(mt7);m1.addSeparator();m1.add(mt1);m1.addSeparator();m1.add(mt2);m2.add(mt3);m2.addSeparator();m2.add(mt4);m3.add(mt5);

mb.add(m1);mb.add(m2);mb.add(m3);

frame.setJMenuBar(mb);

p2.setLayout(new GridLayout(4,7));

p3.setLayout(new GridLayout(1,3));

ta.setEditable(false);

p1.add(ta);

p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);p2.add(b5);p2.add(b6);p2.add(b7);

p2.add(b8);p2.add(b9);p2.add(b10);p2.add(b11);p2.add(b12);p2.add(b13);p2.add(b14);

p2.add(b15);p2.add(b16);p2.add(b17);p2.add(b18);p2.add(b19);p2.add(b20);p2.add(b21);

p2.add(b22);p2.add(b23);p2.add(b24);p2.add(b25);p2.add(b26);p2.add(b27);p2.add(b28);

p3.add(b29);p3.add(b30);p3.add(b31);

Border etched=BorderFactory.createEtchedBorder();

Border border=BorderFactory.createTitledBorder(etched,"计算类型");

p4.add(rb1);p4.add(rb2);

p4.setBorder(border);

b2.setActionCommand("8");

b2.addActionListener(this);

cp.add(p1);cp.add(p4);cp.add(p2);cp.add(p3);

frame.setSize(400,330);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

b1.setActionCommand("7");

b1.addActionListener(this);

b2.setActionCommand("8");

b2.addActionListener(this);

b3.setActionCommand("9");

b3.addActionListener(this);

b4.setActionCommand("/");

b4.addActionListener(this);

b5.setActionCommand("1/x");

b5.addActionListener(this);

b6.setActionCommand("sin");

b6.addActionListener(this);

b7.setActionCommand("log");

b7.addActionListener(this);

b8.setActionCommand("4");

b8.addActionListener(this);

b9.setActionCommand("5");

b9.addActionListener(this);

b10.setActionCommand("6");

b10.addActionListener(this);

b11.setActionCommand("*");

b11.addActionListener(this);

b12.setActionCommand("x^y");

b12.addActionListener(this);

b13.setActionCommand("cos");

b13.addActionListener(this);

b14.setActionCommand("ln");

b14.addActionListener(this);

b15.setActionCommand("1");

b15.addActionListener(this);

b16.setActionCommand("2");

b16.addActionListener(this);

b17.setActionCommand("3");

b17.addActionListener(this);

b18.setActionCommand("-");

b18.addActionListener(this);

b19.setActionCommand("x!");

b19.addActionListener(this);

b20.setActionCommand("tan");

b20.addActionListener(this);

b21.setActionCommand("x^3");

b21.addActionListener(this);

b22.setActionCommand("0");

b22.addActionListener(this);

b23.setActionCommand("+/-");

b23.addActionListener(this);

b24.setActionCommand(".");

b24.addActionListener(this);

b25.setActionCommand("+");

b25.addActionListener(this);

b26.setActionCommand("√x");

b26.addActionListener(this);

b27.setActionCommand("cot");

b27.addActionListener(this);

b28.setActionCommand("x^2");

b28.addActionListener(this);

b29.setActionCommand("Backspace");

b29.addActionListener(this);

b30.setActionCommand("C");

b30.addActionListener(this);

b31.setActionCommand("=");

b31.addActionListener(this);

rb1.setActionCommand("kxx");

rb1.addActionListener(this);

rb2.setActionCommand("bzx");

rb2.addActionListener(this);

}

public void actionPerformed(ActionEvent e) //throws Exception

{

if (e.getActionCommand()=="bzx")

{

b5.setEnabled(false);b6.setEnabled(false);b7.setEnabled(false);

b12.setEnabled(false);b13.setEnabled(false);b14.setEnabled(false);

b19.setEnabled(false);b20.setEnabled(false);b21.setEnabled(false);

b26.setEnabled(false);b27.setEnabled(false);b28.setEnabled(false);

}

if (e.getActionCommand()=="kxx")

{

b5.setEnabled(true);b6.setEnabled(true);b7.setEnabled(true);

b12.setEnabled(true);b13.setEnabled(true);b14.setEnabled(true);

b19.setEnabled(true);b20.setEnabled(true);b21.setEnabled(true);

b26.setEnabled(true);b27.setEnabled(true);b28.setEnabled(true);

}

if (e.getActionCommand()=="1")

{

ta.append("1");

}

if (e.getActionCommand()=="2")

{

ta.append("2");

}

if (e.getActionCommand()=="3")

{

ta.append("3");

}

if (e.getActionCommand()=="4")

{

ta.append("4");

}

if (e.getActionCommand()=="5")

{

ta.append("5");

}

if (e.getActionCommand()=="6")

{

ta.append("6");

}

if (e.getActionCommand()=="7")

{

ta.append("7");

}

if (e.getActionCommand()=="8")

{

ta.append("8");

}

if (e.getActionCommand()=="9")

{

ta.append("9");

}

if (e.getActionCommand()=="0")

{

ta.append("0");

}

if (e.getActionCommand()=="+")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=1;

}

if (e.getActionCommand()=="-")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=2;

}

if (e.getActionCommand()=="*")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=3;

}

if (e.getActionCommand()=="/")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=4;

}

if (e.getActionCommand()=="=")

{

s2=ta.getText();

d2=Double.parseDouble(s2);

if(i==1)

{

d3=d1+d2;

ta.setText( d3.toString());

}

if(i==2)

{

d3=d1-d2;

ta.setText( d3.toString());

}

if(i==3)

{

d3=d1*d2;

ta.setText( d3.toString());

}

if(i==4)

{

if(d2==0.0)

ta.setText("ERROR");

else

{

d3=d1/d2;

ta.setText( d3.toString());

}

}

if (i==5)

{

s2=ta.getText();

d2 = Double.parseDouble(s2);

for (int l=1;l=d2 ; l++)

{

d5=d5*d1;

}

ta.setText( d5.toString());

}

}

if (e.getActionCommand()=="C")

{

ta.setText("");

d4=1.0;

d5=1.0;

}

/*if (e.getActionCommand()=="Backspace")

{

s3=ta.getText();

a=s3.length();

//ta.cut(ta.select(a-1,a));

s4=ta.getText(1,3);

ta.setText(s4);

}

*/

if (e.getActionCommand()=="1/x")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=1/d1;

ta.setText( d2.toString());

}

if (e.getActionCommand()==".")

{

ta.append(".");

}

if (e.getActionCommand()=="+/-")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=0-d1;

ta.setText( d2.toString());

}

if (e.getActionCommand()=="x^2")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=d1*d1;

ta.setText( d2.toString());

}

if (e.getActionCommand()=="x^3")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=d1*d1*d1;

ta.setText( d2.toString());

}

if (e.getActionCommand()=="x^y")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=5;

// d2=d1*d1*d1;

// ta.setText( d2.toString());

}

if (e.getActionCommand()=="√x")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=Math.sqrt(d1);

ta.setText( d2.toString());

}

if (e.getActionCommand()=="x!")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

if (d10)

{

ta.setText( "error");

}

else if (d1==0)

{

ta.setText( "0.0");

}

else {

for (int k=1;k=d1 ;k++ )

d4=d4*k;

ta.setText( d4.toString());

}

}

if (e.getActionCommand()=="sin")

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=Math.sin(3.1415926*d1/180);

ta.setText( d2.toString());

}

}

}

求一个用JAVA写的简单的记事本源代码程序 要有运行结果的截图和源代码,在线等

这个是我以前写着玩的一个例子,可以运行起来看看,有保存,撤销,复制,剪切功能,自己研究研究

package test;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class MyNote {

private JFrame frame = new JFrame("记事本");

private JTextArea text = new JTextArea();

private static boolean flag = false; // 判断是否保存

public MyNote() {

JMenuBar bar = new JMenuBar();

JMenu edit = new JMenu("check");

JMenu check = new JMenu("edit");

JMenu help = new JMenu("help");

JMenuItem term = new JMenuItem("copy");

JMenuItem term1 = new JMenuItem("paste");

JMenuItem term2 = new JMenuItem("cut");

JMenuItem term3 = new JMenuItem("backout");

JMenuItem term4 = new JMenuItem("import");

JMenuItem term5 = new JMenuItem("open");

JMenuItem term6 = new JMenuItem("exit");

JMenuItem term7 = new JMenuItem("save to");

JMenuItem term8 = new JMenuItem("about");

JMenuItem term9 = new JMenuItem("save");

JMenuItem term10 = new JMenuItem("new");

Font font = new Font(null, JFrame.ABORT, 24);

text.setFont(font);

JScrollPane scroll = new JScrollPane(text);

frame.setJMenuBar(bar);

bar.add(edit);

bar.add(check);

bar.add(help);

edit.add(term7);

edit.add(term4);

edit.add(term5);

edit.add(term6);

check.add(term);

check.add(term1);

check.add(term2);

check.add(term3);

help.add(term8);

check.add(term9);

edit.add(term10);

frame.add(scroll);

text.setVisible(false);

frame.setSize(400, 400);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

frame.setLocation(500, 500);

// 事件注册

MenuListener m = new MenuListener();

term5.addActionListener(m);

NewListener n = new NewListener();

term4.addActionListener(n);

SaveListener s = new SaveListener();

term7.addActionListener(s);

CopyListener c = new CopyListener();

term.addActionListener(c);

PasteListener p = new PasteListener();

term1.addActionListener(p);

CutListener c1 = new CutListener();

term2.addActionListener(c1);

HelpListener h = new HelpListener();

term8.addActionListener(h);

ExitListener e = new ExitListener();

term6.addActionListener(e);

CloseListener c2 = new CloseListener();

frame.addWindowListener(c2);

BackOutListener b = new BackOutListener();

term3.addActionListener(b);

SaveActionListener s1 = new SaveActionListener();

term9.addActionListener(s1);

NewFileListener n1 = new NewFileListener();

term10.addActionListener(n1);

}

private class MenuListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

text.setVisible(true);

}

}

// 打开新文件

private class NewListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

text.setText("");

JFileChooser fileChooser = new JFileChooser();

int r = fileChooser.showOpenDialog(frame);

if (r == JFileChooser.APPROVE_OPTION) {

try {

File file = fileChooser.getSelectedFile();

FileReader fr = new FileReader(file);

char[] buf = new char[1024];

int len = 0;

while ((len = fr.read(buf)) != -1) {

text.append(new String(buf, 0, len));

}

fr.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

// 另存为

private class SaveListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

JFileChooser filechoose = new JFileChooser();

int r = filechoose.showSaveDialog(frame);

if (r == JFileChooser.APPROVE_OPTION) {

File file = filechoose.getSelectedFile();

try {

FileWriter writer = new FileWriter(file);

writer.write((String) text.getText());

writer.close();

// 下面方法也可以

/*

* PrintWriter print=new PrintWriter(file);

* print.write(text.getText()); print.close();

*/

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

// 复制

private class CopyListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

text.copy();

}

}

// 粘贴

private class PasteListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

text.paste();

}

}

// 剪切

private class CutListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

text.cut();

}

}

// 关于主题

private class HelpListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(frame, "汪雄辉的无敌记事本");

}

}

private class ExitListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(frame, "谢谢!");

try {

Thread.sleep(2000);

System.exit(0);

} catch (InterruptedException e1) {

e1.printStackTrace();

}

}

}

private class CloseListener implements WindowListener {

public void windowActivated(WindowEvent e) {

}

public void windowClosed(WindowEvent e) {

}

// 关闭窗口

public void windowClosing(WindowEvent e) {

StringBuffer sb = new StringBuffer();

try {

FileReader fr = new FileReader("未命名文件.txt");

char[] buf = new char[1024];

int len = 0;

while ((len = fr.read(buf)) != -1) {

sb.append(new String(buf, 0, len));

}

fr.close();

} catch (Exception e1) {

e1.getStackTrace();

}

String s = sb.toString();

if (flag == false || !(text.getText().equals(s))) {

int r = JOptionPane.showConfirmDialog(frame, "你还没有保存,要保存吗?");

if (r == JOptionPane.OK_OPTION) {

JFileChooser filechoose = new JFileChooser();

int r1 = filechoose.showSaveDialog(frame);

if (r1 == JFileChooser.APPROVE_OPTION) {

File file = filechoose.getSelectedFile();

try {

FileWriter writer = new FileWriter(file);

writer.write((String) text.getText());

writer.close();

System.exit(0);

// 下面方法也可以

/*

* PrintWriter print=new PrintWriter(file);

* print.write(text.getText()); print.close();

*/

} catch (IOException e1) {

e1.printStackTrace();

}

} else {

}

} else if (r == JOptionPane.NO_OPTION) {

System.exit(0);

} else {

}

} else

System.exit(0);

}

public void windowDeactivated(WindowEvent e) {

}

public void windowDeiconified(WindowEvent e) {

}

public void windowIconified(WindowEvent e) {

}

public void windowOpened(WindowEvent e) {

}

}

// 撤销

private class BackOutListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (flag == false)

text.setText("");

else {

try {

FileReader fr = new FileReader("未命名文件.txt");

char[] buf = new char[1024];

int len = 0;

while ((len = fr.read(buf)) != -1) {

text.setText(new String(buf, 0, len));

}

fr.close();

} catch (IOException e1) {

e1.getStackTrace();

}

}

}

}

// 保存文件

private class SaveActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

flag = true;

FileWriter writer;

try {

writer = new FileWriter("未命名文件.txt");

writer.write((String) text.getText());

writer.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

private class NewFileListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

new MyNote();

}

}

public static void main(String[] args) {

new MyNote();

}

}


网站名称:java浪漫的源代码 java程序源代码
文章分享:http://chengdu.cdxwcx.cn/article/doschce.html