用java编写记事本代码,JAVA编写记事本
如何用JAVA编写简单的记事本程序?
import java.awt.*;
创新互联长期为近1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为鄯善企业提供专业的成都做网站、成都网站设计、成都外贸网站建设,鄯善网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.datatransfer.*;public class NewEdit
{
public static void main(String args[])
{
MyFrame EditFrame=new MyFrame();
EditFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EditFrame.setVisible(true);
}
}class MyFrame extends JFrame
{
public MyFrame()
{ Dimension screenSize=toolKit.getScreenSize();
int screenHeight=screenSize.height;
int screenWidth=screenSize.width;
setSize(screenWidth/2,screenHeight/2);
setLocation(screenWidth/4,screenHeight/4);
Image img=toolKit.getImage("icon.gif");
setIconImage(img);
setTitle("MyEdit");
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu=new JMenu("文件");
JMenu editMenu=new JMenu("编辑");
JMenu helpMenu=new JMenu("帮助");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newItem=new JMenuItem("新建");
newItem.addActionListener(new NewListener());
JMenuItem openItem=new JMenuItem("打开");
openItem.addActionListener(new OpenListener());
JMenuItem saveItem=new JMenuItem("保存");
saveItem.addActionListener(new SaveListener());
JMenuItem escItem=new JMenuItem("退出");
escItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent myE)
{
System.exit(0);
}
});
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(escItem);
JMenuItem allItem=new JMenuItem("全选");
//JMenuItem copyItem=new JMenuItem("复制");
//CopyAction copyAction=new CopyAction();
//copyItem.addActionListener(copyAction);
JMenuItem cutItem=new JMenuItem("剪切");
//JMenuItem pasteItem=new JMenuItem("粘贴");
//Action pasteAction=new PasteAction();
//pasteItem.addActionListener(pasteAction);
editMenu.add(allItem);
allItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent myE)
{
textArea.selectAll();
}
});
CopyAction copyAction=new CopyAction("剪切");
editMenu.add(copyAction);
CutAction cutAction=new CutAction("复制");
editMenu.add(cutAction);
Action pasteAction=new PasteAction("粘贴");
editMenu.add(pasteAction);
popup=new JPopupMenu();
popup.add(copyAction);
popup.add(pasteAction);
popup.add(cutAction);
textArea = new JTextArea();
textArea.add(popup);
textArea.addMouseListener(new
MouseListener(){
public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3)
{
popup.show(textArea, e.getX(), e.getY());
}
} public void mouseReleased(MouseEvent e) {} });//可尝试用MouseAdapter,代码可能更简单
JScrollPane scroller = new JScrollPane(textArea);
add(scroller);
//OR--this.getContentPane().add(scroller)
//scroller.setComponentPopupMenu(popup);
//textArea.setInheritsPopupMenu(true);
//scroller.addMouseListener(new MouseAdapter(){});
}
public void writeFile(String fileName)
{
try
{
File file = new File(fileName);
FileWriter write = new FileWriter(file);
write.write(textArea.getText());
write.close();
}
catch(Exception e){
System.out.println("Error closing file!");
}
}
public void openFile(String fileName)
{
try
{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content,charsRead,size-charsRead);
readIn.close();
textArea.setText(new String(content,0,charsRead));
}
catch(IOException e)
{
System.out.println("Error opening file!");
}
}
private class NewListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
textArea.setText("");
}
}
private class OpenListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName != null)
{
openFile(fileName);
}
}
}
private class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
saveFileDialog.setVisible(true);
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
if(fileName !=null)
{
writeFile(fileName);
}
}
}
private class CutAction extends AbstractAction
{
public CutAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection,null);
textArea.replaceRange("",textArea.getSelectionStart(),
textArea.getSelectionEnd());
}
}
private class CopyAction extends AbstractAction
{
public CopyAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
String text = textArea.getSelectedText();
StringSelection selection= new StringSelection(text);
clipboard.setContents(selection,null);
}
}
private class PasteAction extends AbstractAction
{
public PasteAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
Transferable contents = clipboard.getContents(this);
if(contents==null)
return;
String text;
text="";
try
{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception ex){}
textArea.replaceRange(text,
textArea.getSelectionStart(),textArea.getSelectionEnd());
}
}
private JTextArea textArea;
private JPopupMenu popup;
private String fileName="";
private FileDialog openFileDialog=new FileDialog(this,"OpenFile",FileDialog.LOAD); private FileDialog saveFileDialog=new FileDialog(this,"SaveFile",FileDialog.SAVE);
Toolkit toolKit = Toolkit.getDefaultToolkit();
private Clipboard clipboard = toolKit.getSystemClipboard();}
java记事本源代码
给你个做好了的Java的源程序的记事本,自己看看就行了的,不怎么难的···
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class MyNotepad implements ActionListener{
private JFrame frame=new JFrame("新记事本");
private JTextArea jta=new JTextArea();
private String result="";
private boolean flag=true;
private File f;
private JButton jb=new JButton("开始");
private JTextField jtf=new JTextField(15);
private JTextField jt=new JTextField(15);
private JButton jbt=new JButton("替换为");
private JButton jba=new JButton("全部替换");
private Icon ic=new ImageIcon("D:\\java课堂笔记\\GUI\\11.gif");
private String value;
private int start=0;
private JFrame jf=new JFrame("查找");
private JFrame jfc=new JFrame("替换");
@Override
public void actionPerformed(ActionEvent e) {
String comm=e.getActionCommand();
if("新建".equals(comm)){
if(!(frame.getTitle().equals("新记事本"))){
if(!flag){
write();
newNew();
}else{
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}
}else if(!(jta.getText().isEmpty())){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
newNew();
}
}else{
newNew();
}
}else if("打开".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
frame.setTitle(f.getName());
result=read();
flag=false;
value=result;
jta.setText(result);
}
}else if("保存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
if(flag){
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}else{
write();
}
}else if("另存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"另存");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
write();
}
}else if("退出".equals(comm)){
System.exit(0);
}else if("撤销".equals(comm)){
jta.setText(value);
}else if("剪切".equals(comm)){
value=jta.getText();
jta.cut();
}else if("复制".equals(comm)){
jta.copy();
}else if("粘贴".equals(comm)){
value=jta.getText();
jta.paste();
}else if("删除".equals(comm)){
value=jta.getText();
jta.replaceSelection(null);
}else if("全选".equals(comm)){
jta.selectAll();
}else if("查找".equals(comm)){
value=jta.getText();
jf.add(jtf,BorderLayout.CENTER);
jf.add(jb,BorderLayout.SOUTH);
jf.setLocation(300,300);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("替换".equals(comm)){
value=jta.getText();
GridLayout gl=new GridLayout(3,3);
JLabel jl1=new JLabel("查找内容:");
JLabel jl2=new JLabel("替换为:");
jfc.setLayout(gl);
jfc.add(jl1);
jfc.add(jtf);
jfc.add(jb);
jfc.add(jl2);
jfc.add(jt);
jfc.add(jbt);
JLabel jl3=new JLabel();
JLabel jl4=new JLabel();
jfc.add(jl3);
jfc.add(jl4);
jfc.add(jba);
jfc.setLocation(300,300);
jfc.pack();
jfc.setVisible(true);
jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("版本".equals(comm)){
JDialog jd=new JDialog(frame,"关于对话框");
jd.setSize(200,200);
JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");
jd.add(l,BorderLayout.CENTER);
jd.setLocation(100,200);
jd.setSize(300,300);
jd.setVisible(true);
// jd.pack();
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}else if("开始".equals(comm)||"下一个".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jb.setText("下一个");
// value=value.substring(s+temp.length());//不能截取字串
}else {
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}else if("替换为".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jta.replaceSelection(jt.getText());
}else {
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}else if("全部替换".equals(comm)){
String temp=jta.getText();
temp=temp.replaceAll(jtf.getText(), jt.getText());
jta.setText(temp);
}
}
public String read(){
String temp="";
try {
FileInputStream fis = new FileInputStream(f.getAbsolutePath());
byte[] b=new byte[1024];
while(true){
int num=fis.read(b);
if(num==-1)break;
temp=temp+new String(b,0,num);
}
fis.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return temp;
}
public void write(){
try {
FileOutputStream fos=new FileOutputStream(f);
fos.write(jta.getText().getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void newNew(){
frame.dispose();
new MyNotepad();
flag=true;
}
public MyNotepad(){
JMenuBar jmb=new JMenuBar();
String[] menuLab={"文件","编辑","帮助"};
String[][] menuItemLab={{"新建","打开","保存","另存","退出"},
{"撤销","剪切","复制","粘贴","删除","全选","查找","替换"},
{"版本"}};
for(int i=0;imenuLab.length;i++){
JMenu menu=new JMenu(menuLab[i]);
jmb.add(menu);
for(int j=0;jmenuItemLab[i].length;j++){
JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);
menu.add(jmi);
jmi.addActionListener(this);
}
}
frame.setJMenuBar(jmb);
jta.setLineWrap(true);//自动换行
JScrollPane jsp=new JScrollPane(jta);//滚动窗口面板
frame.add(jsp);
jb.addActionListener(this);
jbt.addActionListener(this);
jba.addActionListener(this);
frame.setLocation(200,50);
frame.setSize(620,660);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyNotepad();
}
}
用java编写记事本程序,可以实现新建、打开、保存、退出、复制、粘贴、剪切、全选。
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
public class MyText extends JFrame implements ActionListener{
private JLabel lb1;
private JMenuBar mb;
private JMenu 文件, 编辑, 格式, 帮助;
private JMenuItem 新建, 打开, 保存, 退出, 复制, 粘贴, 剪切, 全选, 字体, 关于;
private JTextArea editorArea;
private boolean isDirty = false;
private String strFileName = "未命名";
private static final String EDITOR_NAME = "MyText";
public MyText() {
super();
mb = new JMenuBar();
文件 = new JMenu("文件");
编辑 = new JMenu("编辑");
格式 = new JMenu("格式");
帮助 = new JMenu("帮助");
新建 = new JMenuItem("新建");
新建.addActionListener(this);
打开 = new JMenuItem("打开");
打开.addActionListener(this);
保存 = new JMenuItem("保存");
保存.addActionListener(this);
退出 = new JMenuItem("退出");
退出.addActionListener(this);
复制 = new JMenuItem("复制");
复制.addActionListener(this);
粘贴 = new JMenuItem("粘贴");
粘贴.addActionListener(this);
剪切 = new JMenuItem("剪切");
剪切.addActionListener(this);
全选 = new JMenuItem("全选");
全选.addActionListener(this);
字体 = new JMenuItem("字体");
字体.addActionListener(this);
关于 = new JMenuItem("关于");
关于.addActionListener(this);
mb.add(文件);
mb.add(编辑);
mb.add(格式);
mb.add(帮助);
文件.add(新建);
文件.add(打开);
文件.add(保存);
文件.add(退出);
编辑.add(复制);
编辑.add(粘贴);
编辑.add(剪切);
编辑.add(全选);
格式.add(字体);
帮助.add(关于);
setJMenuBar(mb);
Container container = getContentPane();
editorArea = new JTextArea();
editorArea.setLineWrap(true);
editorArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(!isDirty()){
setDirty(true);
}
}
});
JScrollPane scrollPane = new JScrollPane(editorArea);
container.add(scrollPane);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
dispose();
}
});
setTitle(formatEditorTitle());
setSize(600, 400);
setVisible(true);
}
private boolean isDirty() {
return isDirty;
}
private void setDirty(boolean isDirty) {
this.isDirty = isDirty;
setTitle(formatEditorTitle());
}
public static void main(String args[]) {
@SuppressWarnings("unused")
MyText app = new MyText();
}
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem)e.getSource();
if(item.equals(新建)){
if(isDirty()){
int ret = JOptionPane.showConfirmDialog(getContentPane(), "文件内容已经变动,是否保存?", "MyText", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if(ret == JOptionPane.OK_OPTION){
saveFile();
}else if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION){
return;
}
}
clearEditorArea();
setDirty(false);
}else if(item.equals(打开)){
if(isDirty()){
int ret = JOptionPane.showConfirmDialog(getContentPane(), "文件内容已经变动,是否保存?", "MyText", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if(ret == JOptionPane.OK_OPTION){
saveFile();
}else if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION){
return;
}
}
openFile();
}else if(item.equals(保存)){
saveFile();
}else if(item.equals(退出)){
dispose();
}else if(item.equals(复制)){
editorArea.copy();
}else if(item.equals(剪切)){
editorArea.cut();
}else if(item.equals(粘贴)){
editorArea.paste();
}else if(item.equals(全选)){
editorArea.selectAll();
}else if(item.equals(字体)){
FontDialog font = new FontDialog(this, editorArea.getFont());
editorArea.setFont(font.getSelectedFont());
}else if(item.equals(关于)){
AboutDialog about = new AboutDialog(this);
about.setVisible(true);
}
}
private String getFileName() {
return strFileName;
}
private void setFileName(String strFileName) {
this.strFileName = strFileName;
}
public String formatEditorTitle(){
StringBuffer strFileNm = new StringBuffer(getFileName());
strFileNm.append(isDirty()?"*":"");
strFileNm.append(" - ");
strFileNm.append(EDITOR_NAME);
return strFileNm.toString();
}
private void clearEditorArea(){
editorArea.selectAll();
editorArea.replaceSelection("");
}
private void openFile(){
JFileChooser openDialog = new JFileChooser();
openDialog.setFileFilter(new TxtFileFilter());
if(openDialog.showOpenDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){
File file = openDialog.getSelectedFile();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String buff = br.readLine();
clearEditorArea();
while(buff != null){
editorArea.append(buff);
editorArea.append("\n");
buff = br.readLine();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally{
try{
if(br != null)
br.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
}
private void saveFile(){
JFileChooser saveDialog = new JFileChooser();
saveDialog.setFileFilter(new TxtFileFilter());
if(saveDialog.showSaveDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){
File file = saveDialog.getSelectedFile();
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
String buff = editorArea.getText();
bw.write(buff);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally{
try{
if(bw != null)
bw.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
}
class TxtFileFilter extends FileFilter{
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt");
}
@Override
public String getDescription() {
return "*.txt(文本文件)";
}
}
class FontDialog extends JDialog{
private JComboBox cb_FontSize;
private JComboBox cb_FontStyle;
private JComboBox cb_FontNm;
private Font font;
HashtableInteger, String style = new HashtableInteger, String();
public FontDialog(){
this(null, null);
}
public FontDialog(Frame owner, Font font){
super(owner);
this.font = font == null?getFont():font;
setTitle("字体选择框");
setModal(true);
setResizable(false);
setSize(326, 164);
getContentPane().setLayout(null);
final JLabel lb_FontNm = new JLabel();
lb_FontNm.setText("字体名称");
lb_FontNm.setBounds(10, 10, 66, 16);
getContentPane().add(lb_FontNm);
cb_FontNm = new JComboBox();
cb_FontNm.setBounds(10, 28, 133, 25);
getContentPane().add(cb_FontNm);
cb_FontStyle = new JComboBox();
cb_FontStyle.setBounds(169, 28, 66, 25);
getContentPane().add(cb_FontStyle);
cb_FontSize = new JComboBox();
cb_FontSize.setBounds(258, 28, 53, 25);
getContentPane().add(cb_FontSize);
final JButton btn_OK = new JButton();
btn_OK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int styleCode = 0;
for(EnumerationInteger i = style.keys();i.hasMoreElements();){
styleCode = i.nextElement();
if(style.get(styleCode).equals(cb_FontStyle.getSelectedItem()))
break;
}
Font font = new Font(cb_FontNm.getSelectedItem().toString(), styleCode, ((Integer)cb_FontSize.getSelectedItem()).intValue());
setSelectedFont(font);
dispose();
}
});
btn_OK.setText("确定");
btn_OK.setBounds(58, 83, 76, 26);
getContentPane().add(btn_OK);
final JButton btn_Cancel = new JButton();
btn_Cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
btn_Cancel.setText("取消");
btn_Cancel.setBounds(169, 83, 76, 26);
getContentPane().add(btn_Cancel);
final JLabel lb_FontStyle = new JLabel();
lb_FontStyle.setText("字体样式");
lb_FontStyle.setBounds(169, 10, 66, 16);
getContentPane().add(lb_FontStyle);
final JLabel lb_FontSize = new JLabel();
lb_FontSize.setText("字体大小");
lb_FontSize.setBounds(258, 10, 66, 16);
getContentPane().add(lb_FontSize);
init();
setVisible(true);
}
private void init(){
GraphicsEnvironment gg=GraphicsEnvironment.getLocalGraphicsEnvironment();
String ss[]=gg.getAvailableFontFamilyNames();
for(String s : ss)
cb_FontNm.addItem(s);
if(font != null)
cb_FontNm.setSelectedItem(font.getFamily());
style.put(Font.PLAIN, "标准");
style.put(Font.BOLD, "粗体");
style.put(Font.ITALIC, "斜体");
style.put(Font.BOLD+Font.ITALIC, "粗体斜体");
cb_FontStyle.addItem(style.get(Font.PLAIN));
cb_FontStyle.addItem(style.get(Font.BOLD));
cb_FontStyle.addItem(style.get(Font.ITALIC));
cb_FontStyle.addItem(style.get(Font.BOLD+Font.ITALIC));
if(font != null)
cb_FontStyle.setSelectedItem(style.get(font.getStyle()));
for(int i=8;i23;i++)
cb_FontSize.addItem(i);
if(font != null)
cb_FontSize.setSelectedItem(font.getSize());
}
public Font getSelectedFont() {
return font;
}
public void setSelectedFont(Font font) {
this.font = font;
}
}
class AboutDialog extends JDialog{
public AboutDialog(JFrame owner){
super(owner);
setTitle("关于");
setSize(new Dimension(322, 163));
getContentPane().setLayout(null);
final JLabel version = new JLabel();
version.setText("MyText 1.0");
version.setBounds(74, 37, 66, 16);
getContentPane().add(version);
final JLabel copyright = new JLabel();
copyright.setText("Copyright (C) 2010");
copyright.setBounds(74, 59, 188, 16);
getContentPane().add(copyright);
final JSeparator separator = new JSeparator();
separator.setBounds(70, 90, 210, 2);
getContentPane().add(separator);
final JButton okButton = new JButton();
okButton.setBounds(235, 95, 50, 26);
getContentPane().add(okButton);
okButton.setText("Ok");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
}
电脑怎么用记事本写Java程序并运行?
1. 新建一个记事本,后缀名是 .java ;然后在里面写一段java的代码,如图:
2.把写好的java文件丢进D盘,就是第一步给出的那个class文件;
3. 打开dos界面 开始-〉运行-〉cmd-〉 这个是命令行模式,选择D盘,如图:
4.选择D盘之后,在命令提示符中输入“javac 文件名.java”,我的文件名为java,所以输入javac java.java然后回车,等待编译。这时候你就会发现它提示说javac不是内部文件。接下来就是java环境变量设置的问题了;
5.下载一个jdk执行默认安装。例如我下的jdk版本是: jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe;
6.配置环境:右击我的电脑点属性,进去点高级就能看到环境变量。点进去就能设置你的系统变量了,如图:
6-1。变量名:classpath 变量值:(.;C:\Program Files\Java\jdk1.6.0_10\lib;)括号里面都是 (主要我们在编译运行程序的时候通过classpath可以帮助我们找到一些需要的系统类,“.”号是表示当前路径;“;”号是用来隔开多个变量值,如果你的系统里面已经有classpath环 境变量,就不用新建了,直接加“;”号,在后面加新值);如图所示
标题名称:用java编写记事本代码,JAVA编写记事本
链接URL:http://cdiso.cn/article/dsepiig.html