這是一個(gè)我以前寫的簡(jiǎn)單的記事本,里面有新建,保存,另存,打開(kāi)等功能,但是只是邏輯最簡(jiǎn)單的那種,你看看吧,希望對(duì)你有幫助;
成都創(chuàng)新互聯(lián)公司專業(yè)網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì),集網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)、網(wǎng)站制作于一體,網(wǎng)站seo、網(wǎng)站優(yōu)化、網(wǎng)站營(yíng)銷、軟文發(fā)稿等專業(yè)人才根據(jù)搜索規(guī)律編程設(shè)計(jì),讓網(wǎng)站在運(yùn)行后,在搜索中有好的表現(xiàn),專業(yè)設(shè)計(jì)制作為您帶來(lái)效益的網(wǎng)站!讓網(wǎng)站建設(shè)為您創(chuàng)造效益。
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);
// 關(guān)閉
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("打開(kāi)");
jmb.add(fm1);
jmb.addSeparator();
// 打開(kāi)文件
fm1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == fm1) {
FileDialog d = new FileDialog(f, "打開(kāi)文件",
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("請(qǐng)輸入文件名",
"java");
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = notePadPanel.getTa().getText()
.getBytes();
fout.write(bb);
// 關(guān)閉
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
} else {
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = notePadPanel.getTa().getText()
.getBytes();
fout.write(bb);
// 關(guān)閉
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);
// 關(guān)閉
fout.close();
} catch (Exception e4) {
System.out.println(e4.getMessage());
}
}
}
});
fm4 = new JMenuItem("關(guān)閉");
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("復(fù)制");
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("請(qǐng)輸入文件名",
"java");
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = ta.getText().getBytes();
fout.write(bb);
// 關(guān)閉
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
} else {
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = ta.getText().getBytes();
fout.write(bb);
// 關(guān)閉
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
}
}
} catch (IOException e1) {
System.out.println(e1.getMessage());
}
}
});
jb1 = new JButton("關(guān)閉");
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);
}
}
大概寫了一個(gè)例子,給你看看,你的那個(gè)例子來(lái)搞死鎖比較難搞,主要你是只有一個(gè)鎖,沒(méi)有所謂的請(qǐng)求不釋放的問(wèn)題,一般死鎖都需要有兩個(gè)鎖或以上的。
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());
}
}
}
}
這個(gè)例子首先需要先了解Integer類的機(jī)制,再進(jìn)行Integer類實(shí)例化或轉(zhuǎn)換時(shí),它會(huì)緩存-128-127之間的所有對(duì)象,因此在這里我們,調(diào)用的1,2至始至終都只有兩個(gè)對(duì)象。
下面就是死鎖的分析:
當(dāng)我們執(zhí)行NumThread(1,2)時(shí),鎖的取得沒(méi)問(wèn)題(Integer.valueOf(a)的鎖肯定沒(méi)問(wèn)題),接下來(lái)用NumThread(2,1),如果此時(shí)NumThread(1,2)已經(jīng)取得了兩個(gè)鎖,這里沒(méi)問(wèn)題,執(zhí)行完后可以繼續(xù)取得鎖,但如果NumThread(1,2)只取得a的鎖,而此時(shí)NumThread(2,1)取得了b的鎖,這時(shí)問(wèn)題就來(lái)了。NumThread(1,2)會(huì)等待NumThread(2,1)釋放b鎖,而NumThread(2,1)會(huì)等等NumThread(1,2)釋放a鎖。
我用了一個(gè)循環(huán)啟動(dòng)線程是因?yàn)榘l(fā)生的機(jī)率不大。
可以引伸到你那個(gè)例子,用兩個(gè)相同的對(duì)象作為鎖。
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);
}
}
}
}
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();
}
}
}
按照題目要求編寫的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("圓的周長(zhǎng)為"+getLength());
System.out.println("圓的面積為"+getArea());
}
}
下面是Circle類的測(cè)試類Test(文件名Test.java 要運(yùn)行需要和Circle.java放在同一包內(nèi))
public class Test{
public static void main(String[] args){
Circle c=new Circle(2.5);
c.disp();
}
}