首页 养生问答 疾病百科 养生资讯 女性养生 男性养生

JAVA做桌面程序

发布网友

我来回答

3个回答

热心网友

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package javaapplication42;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/ import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;/**
*
* @author Manco
*/
public class Main extends JFrame{
Container c;
JTextArea T;
public Main(){
c=getContentPane();
T=new JTextArea();
JScrollPane S=new JScrollPane(T);
c.add(S);
JMenuBar b=new JMenuBar();
this.setJMenuBar(b);
b.add(vh());
b.add(vv());
b.setVisible(true);
this.setVisible(true);
this.setSize(800,800);
this.setTitle("记事本");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JMenu vh(){
JMenu menu=new JMenu("操作");
final JMenuItem A3=new JMenuItem("保存为");//ok
JMenuItem A1=new JMenuItem("保存");//待添加事件
JMenuItem A4=new JMenuItem("退出");//ok
final JMenuItem A=new JMenuItem("打开");
A.addActionListener(new ActionListener(){//打开 public void actionPerformed(ActionEvent e) {
JFileChooser F1=new JFileChooser();
int p=F1.showOpenDialog(A);
File X=F1.getSelectedFile();
try {
int u=(int)X.length();
char[] a=new char[u+5];
FileReader Re = new FileReader(X.getPath());
int read = Re.read(a);
String k=new String(a);//构造以a为值的字符串
T.setText(k);
Re.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
A4.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
A3.addActionListener(new ActionListener(){//保存为“*********” public void actionPerformed(ActionEvent e) {
JFileChooser F=new JFileChooser();//*****************************************
int i=F.showSaveDialog(F);
File S1=F.getSelectedFile();//获得设定的文件
try {
FileWriter W= new FileWriter(S1);
W.write(T.getText());//使文本获得内容
W.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} }
});
menu.add(A);
menu.add(A1);
menu.add(A3);
menu.add(A4);
return menu;
}
private JMenu vv(){
final JMenu menu1=new JMenu("帮助");
final JMenuItem y=new JMenuItem("关于");
y.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JDialog D=new JDialog();
JLabel L=new JLabel("记事本_版本1.0_作者:Manco");
D.getContentPane().add(L);
D.setVisible(true);
D.setLocationRelativeTo(menu1);//设置D在组件menu1上(位置)
D.setSize(200, 200);
}
});
menu1.add(y);
return menu1;
} /**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main();
// TODO code application logic here
}
}

热心网友

我发到你邮箱了 总共3个 你试试

热心网友

我电脑白痴 不好采纳 细细

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com