189 8069 5689

java写代码演示 java编程代码实例

我想用java编写windows记事本的新建功能怎么写?要有代码演示。

这是一个我以前写的简单的记事本,里面有新建,保存,另存,打开等功能,但是只是逻辑最简单的那种,你看看吧,希望对你有帮助;

成都创新互联公司专业网站设计制作、成都网站设计,集网站策划、网站设计、网站制作于一体,网站seo、网站优化、网站营销、软文发稿等专业人才根据搜索规律编程设计,让网站在运行后,在搜索中有好的表现,专业设计制作为您带来效益的网站!让网站建设为您创造效益。

import java.awt.FileDialog;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class NotePad {

public static void main(String[] args) {

NotePadFrame notPadFrame = new NotePadFrame();

notPadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

notPadFrame.setVisible(true);

}

}

class NotePadFrame extends JFrame {

private JMenu jmb, jmb1;

private JMenuBar Jmenu = new JMenuBar();

private JMenuItem fm, fm1, fm2, fm3, fm4, fe1, fe2, fe3, fe4;

String fileName, copy, paste, cut;

NotePadPanel notePadPanel = new NotePadPanel(this);

private NotePadFrame f;

public NotePadFrame() {

jmb = new JMenu("文件");

this.setJMenuBar(Jmenu);

fm = new JMenuItem("新建");

jmb.add(fm);

jmb.addSeparator();

// 新建

fm.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

if (e.getSource() == fm) {

if (!(notePadPanel.getTa().getText()).equals("")) {

Object[] options = { "确定", "取消" };

int response = JOptionPane.showOptionDialog(null,

"你是否保存", "提示", JOptionPane.YES_OPTION,

JOptionPane.QUESTION_MESSAGE, null,

options, options[0]);

if (response == 0) {

FileDialog d = new FileDialog(f, "保存文件",

FileDialog.SAVE);

d.setVisible(true);

fileName = d.getDirectory() + d.getFile();

FileOutputStream fout = new FileOutputStream(

fileName + ".txt");

byte[] bb = notePadPanel.getTa().getText()

.getBytes();

fout.write(bb);

// 关闭

fout.close();

JOptionPane.showMessageDialog(null, "已保存");

notePadPanel.getTa().setText("");

}

if (response == 1) {

JOptionPane.showMessageDialog(null, "你选择了取消");

notePadPanel.getTa().setText("");

}

}

}

} catch (Exception e2) {

System.out.println(e2.getMessage());

}

}

});

fm1 = new JMenuItem("打开");

jmb.add(fm1);

jmb.addSeparator();

// 打开文件

fm1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

if (e.getSource() == fm1) {

FileDialog d = new FileDialog(f, "打开文件",

FileDialog.LOAD);

d.setVisible(true);

File file = new File(d.getDirectory() + d.getFile());

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

char[] ch = new char[1024];

FileReader fr = new FileReader(file);

fr.read(ch);

String str = new String(ch);

notePadPanel.getTa().setText(str);

}

}

} catch (IOException e3) {

System.out.println(e3.getMessage());

}

}

});

fm2 = new JMenuItem("保存");

jmb.add(fm2);

jmb.addSeparator();

// 保存文件

fm2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

if (e.getSource() == fm2) {

if (fileName == null) {

fileName = JOptionPane.showInputDialog("请输入文件名",

"java");

FileOutputStream fout = new FileOutputStream(

fileName + ".txt");

byte[] bb = notePadPanel.getTa().getText()

.getBytes();

fout.write(bb);

// 关闭

fout.close();

JOptionPane.showMessageDialog(null, "已保存");

} else {

FileOutputStream fout = new FileOutputStream(

fileName + ".txt");

byte[] bb = notePadPanel.getTa().getText()

.getBytes();

fout.write(bb);

// 关闭

fout.close();

JOptionPane.showMessageDialog(null, "已保存");

}

}

} catch (IOException e1) {

System.out.println(e1.getMessage());

}

}

});

fm3 = new JMenuItem("另存为");

jmb.add(fm3);

jmb.addSeparator();

// 另存为

fm3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getSource() == fm3) {

try {

FileDialog d = new FileDialog(f, "另存为", FileDialog.SAVE);

d.setVisible(true);

fileName = d.getDirectory() + d.getFile();

FileOutputStream fout = new FileOutputStream(fileName + ".txt");

byte[] bb = notePadPanel.getTa().getText().getBytes();

fout.write(bb);

// 关闭

fout.close();

} catch (Exception e4) {

System.out.println(e4.getMessage());

}

}

}

});

fm4 = new JMenuItem("关闭");

jmb.add(fm4);

fm4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getSource() == fm4) {

System.exit(0);

}

}

});

jmb1 = new JMenu("编辑");

fe1 = new JMenuItem("复制");

jmb1.add(fe1);

jmb1.addSeparator();

fe1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getSource() == fe1) {

copy = notePadPanel.getTa().getSelectedText();

}

}

});

fe2 = new JMenuItem("粘贴");

jmb1.add(fe2);

jmb1.addSeparator();

fe2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getSource() == fe2) {

System.out.println("copy="+copy);

notePadPanel.getTa().setText(copy);

}

}

});

fe3 = new JMenuItem("剪切");

jmb1.add(fe3);

jmb1.addSeparator();

fe3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getSource() == fe3) {

copy = notePadPanel.getTa().getSelectedText();

notePadPanel.getTa().setText("");

}

}

});

fe4 = new JMenuItem("版本");

jmb1.add(fe4);

fe4.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

if(e.getSource()==fe4){

JOptionPane.showMessageDialog(f, "NotePad 1.0");

}

}

});

Jmenu.add(jmb);

Jmenu.add(jmb1);

}

}

class NotePadPanel extends JPanel {

private JButton jb1, jb;

private JTextArea ta;

String fileName;

private JScrollPane jsp;

public JTextArea getTa() {

return ta;

}

public void setTa(JTextArea ta) {

this.ta = ta;

}

public NotePadPanel(NotePadFrame notePadFrame) {

ta = new JTextArea();

ta.setWrapStyleWord(true);

jsp = new JScrollPane(ta);

jb = new JButton("保存");

jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

if (e.getSource() == jb) {

if (fileName == null) {

fileName = JOptionPane.showInputDialog("请输入文件名",

"java");

FileOutputStream fout = new FileOutputStream(

fileName + ".txt");

byte[] bb = ta.getText().getBytes();

fout.write(bb);

// 关闭

fout.close();

JOptionPane.showMessageDialog(null, "已保存");

} else {

FileOutputStream fout = new FileOutputStream(

fileName + ".txt");

byte[] bb = ta.getText().getBytes();

fout.write(bb);

// 关闭

fout.close();

JOptionPane.showMessageDialog(null, "已保存");

}

}

} catch (IOException e1) {

System.out.println(e1.getMessage());

}

}

});

jb1 = new JButton("关闭");

jb1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getSource() == jb1) {

System.exit(0);

}

}

});

this.add(jb);

this.add(jb1);

notePadFrame.add(this, "South");

notePadFrame.setSize(600, 400);

notePadFrame.add(jsp);

notePadFrame.setTitle("记事本");

int W = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();

int H = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();

notePadFrame.setLocation((W - notePadFrame.getWidth()) / 2,

(H - notePadFrame.getHeight()) / 2);

}

}

java -线程 -演示死锁的代码 main函数里面要怎么写 具体如下:

大概写了一个例子,给你看看,你的那个例子来搞死锁比较难搞,主要你是只有一个锁,没有所谓的请求不释放的问题,一般死锁都需要有两个锁或以上的。

public class TestT {

public static void main(String[] args) {

for (int i = 0; i 10; i ++) {

NumThread nt = new NumThread(1,2);

NumThread nt2 = new NumThread(2,1);

nt.start();

nt2.start();

}

}

}

class NumThread extends Thread{

private int a;

private int b;

public NumThread(int a,int b) {

this.a = a;

this.b = b;

}

public void run() {

synchronized(Integer.valueOf(a)) {

System.out.println("xxx" + Thread.currentThread().getName());

synchronized(Integer.valueOf(b)) {

System.out.println("yyy" + Thread.currentThread().getName());

}

}

}

}

这个例子首先需要先了解Integer类的机制,再进行Integer类实例化或转换时,它会缓存-128-127之间的所有对象,因此在这里我们,调用的1,2至始至终都只有两个对象。

下面就是死锁的分析:

当我们执行NumThread(1,2)时,锁的取得没问题(Integer.valueOf(a)的锁肯定没问题),接下来用NumThread(2,1),如果此时NumThread(1,2)已经取得了两个锁,这里没问题,执行完后可以继续取得锁,但如果NumThread(1,2)只取得a的锁,而此时NumThread(2,1)取得了b的锁,这时问题就来了。NumThread(1,2)会等待NumThread(2,1)释放b锁,而NumThread(2,1)会等等NumThread(1,2)释放a锁。

我用了一个循环启动线程是因为发生的机率不大。

可以引伸到你那个例子,用两个相同的对象作为锁。

public class TestT {

public static void main(String[] args) {

S s = new S();

S s2 = new S();

for (int i = 0; i 10; i ++) {

TestSleep ts = new TestSleep(s,s2);

TestSleep ts2 = new TestSleep(s2,s);

ts.start();

ts2.start();

}

}

}

class S{public int i=0;}

class TestSleep extends Thread {

/**

* @param args

*/

private S s=null;

private S s2 = null;

public TestSleep(S s,S s2){

this.s=s;

this.s2=s2;

}

public void run(){

System.out.println("Now is begin Thread-A");

synchronized(s){

System.out.println("Now is begin "+Thread.currentThread().getName());

synchronized(s2) {

System.out.println(s.i);

}

}

}

}

如何用java写这段代码?

import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;public class JEncrytion{

public static void main(String[] argv) {

try{ KeyGenerator keygenerator = KeyGenerator.getInstance("DES"); SecretKey myDesKey = keygenerator.generateKey();

Cipher desCipher; // Create the cipher

desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

// Initialize the cipher for encryption

desCipher.init(Cipher.ENCRYPT_MODE, myDesKey); //sensitive information

byte[] text = "No body can see me".getBytes();

System.out.println("Text [Byte Format] : " + text);

System.out.println("Text : " + new String(text));

// Encrypt the text

byte[] textEncrypted = desCipher.doFinal(text);

System.out.println("Text Encryted : " + textEncrypted);

// Initialize the same cipher for decryption

desCipher.init(Cipher.DECRYPT_MODE, myDesKey); // Decrypt the text

byte[] textDecrypted = desCipher.doFinal(textEncrypted);

System.out.println("Text Decryted : " + new String(textDecrypted));

}catch(NoSuchAlgorithmException e){

e.printStackTrace();

}catch(NoSuchPaddingException e){

e.printStackTrace();

}catch(InvalidKeyException e){

e.printStackTrace();

}catch(IllegalBlockSizeException e){

e.printStackTrace();

}catch(BadPaddingException e){

e.printStackTrace();

}

}

}

怎么用java写下面的代码?

按照题目要求编写的Circle类的Java程序如下(文件名Circle.java)

public class Circle{

private double radius;

Circle(){

radius=0;

}

Circle(double r){

radius=r;

}

double getRadius(){

return radius;

}

double getLength(){

return 2*Math.PI*radius;

}

double getArea(){

return Math.PI*radius*radius;

}

void disp(){

System.out.println("圆的半径为"+getRadius());

System.out.println("圆的周长为"+getLength());

System.out.println("圆的面积为"+getArea());

}

}

下面是Circle类的测试类Test(文件名Test.java 要运行需要和Circle.java放在同一包内)

public class Test{

public static void main(String[] args){

Circle c=new Circle(2.5);

c.disp();

}

}


分享题目:java写代码演示 java编程代码实例
本文路径:http://jkwzsj.com/article/dojcdeg.html

其他资讯