您的位置:首页 > 产品设计 > UI/UE

java GUI实现记事本

2017-11-23 13:20 387 查看
1,GUI+异常处理+文件=》简易记事本(面向对象实现)
面向对象其实就可以把一个函数用类和对象实现,把传给函数的值传给类的构造,并执行函数
操作,实现功能。岂不美哉!

2,做项目首先想好思路,先实现基本功能再扩充再细化再优化,一步一步来不可多想逾越
1,GUI实现形态
2,文件实现功能(存储,写入,读出)
3,GUI实现创建文件
4,优化附加功能
5,用面向对象方式重写
6,变成.exe文件

面向对象其实就可以把一个函数用类和对象实现,把传给函数的值传给类的构造,并执行函数操作实现功能。岂不美哉!

****************************************************************************************************************************************************
GUI
package Try;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Gui extends JFrame implements ActionListener {
public static String s,s1;

JPanel jp=new JPanel();
JButton jb1=new JButton("文件");
JTextPane jtf=new JTextPane();
JMenuBar jm=new JMenuBar();
JMenu file=new JMenu("文件");
JMenu way=new JMenu("格式");
JMenu help=new JMenu("帮助");
JMenuItem open=new JMenuItem("打开");
JMenuItem new1=new JMenuItem("新建");
JMenuItem save=new JMenuItem("保存");
JMenuItem color=new JMenuItem("颜色");
JMenuItem size=new JMenuItem("型号");
JMenuItem word=new JMenuItem("字形");
JMenuItem about=new JMenuItem("关于记事本");
JScrollPane scrollPane;

public Gui()
{this.setTitle("讷铮记事本");
this.setBounds(100,100,600,400);
this.setVisible(true);
this.add(jp);
jp.setLayout(null);

jp.add(jtf);
jtf.setBounds(5,22,575,370);

jtf.setSelectedTextColor(Color.RED);
/*
jtf.setLineWrap(true); //激活自动换行功能

jtf.setWrapStyleWord(true);

*/
jtf.setSelectionColor(Color.red);

jp.add(jm);
jm.setBounds(5,0,120,20);
jm.add(file);
jm.add(way);
jm.add(help);
file.add(open);
file.add(new1);
file.add(save);
way.add(color);
way.add(size);
way.add(word);
help.add(about);
open.addActionListener(this);
save.addActionListener(this);
new1.addActionListener(this);
color.addActionListener(this);
size.addActionListener(this);
word.addActionListener(this);
about.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
// 判断最初发生Event事件的对象
if (e.getSource() == save) {

// 获得容器
JFrame jf=new JFrame();
int i=0;
i=JOptionPane.showConfirmDialog(jf, "确定保存?","这是一个确认对话框",JOptionPane.OK_CANCEL_OPTION);
if(i==0)
{
System.out.println("已成功保存");
JFrame jf1=new JFrame();

s=JOptionPane.showInputDialog(jf1, "重命名","这是一个输入对话框",JOptionPane.INFORMATION_MESSAGE);
System.out.println(s);

s=s+".java";

s1=jtf.getText();
Save save1=new Save(s,s1);
save1.save();

JOptionPane.showMessageDialog(jf1,"保存成功","",JOptionPane.INFORMATION_MESSAGE);

}
if(i==1)
{
System.out.println("用户取消保存");
}
}
if(e.getSource()==open) {

String text="sssssssss";
JFrame jf1=new JFrame();

s=JOptionPane.showInputDialog(jf1, "文件名","这是一个输入对话框",JOptionPane.INFORMATION_MESSAGE);
System.out.println(s);

s=s+".java";
Open open1=new Open(s);
text=open1.open();

if(text==null)
{
JOptionPane.showMessageDialog(jf1,"此文件不存在","",JOptionPane.INFORMATION_MESSAGE);
}
else{
jtf.setText(text);

JOptionPane.showMessageDialog(jf1,"打开成功","",JOptionPane.INFORMATION_MESSAGE);
}
}

if(e.getSource()==new1)
{
String text="sssssssss";
JFrame jf1=new JFrame();

s=JOptionPane.showInputDialog(jf1, "请输入文件名","这是一个输入对话框",JOptionPane.INFORMATION_MESSAGE);
System.out.println(s);
if(text==null)
{
JOptionPane.showMessageDialog(jf1,"文件名不能为空","",JOptionPane.INFORMATION_MESSAGE);
}else
{
s=s+".java";
New1 new1=new New1(s);
new1.new1();
jtf.setText(null);
JOptionPane.showMessageDialog(jf1,"创建成功","",JOptionPane.INFORMATION_MESSAGE);
}

}
if(e.getSource()==about)
{
String text=" 讷铮记事本以该项目作者谢訥铮的名字命名,功能参照于windows自带记事本,由java实现。2017年11月22日的一个中午,正在的复习"
+ "找工作的中国地质大学计算机学院大三学生谢訥铮正好复习到java 文件和GUI部分,用记事本打开文件时突发奇想产生了模拟实现一个新的记事本的"
+ "想法,经过一个下午努力,完成了此工程的雏形。。。,鉴于作者水平有限,该工程尚有众多不足之处,望海涵!";

jtf.setText(text);

}

if(e.getSource()==color)
{
Select s=new Select();

}
if(e.getSource()==size)
{
SelectSize s=new SelectSize();

}
if(e.getSource()==word)
{
SelectWord s=new SelectWord();

}

}

}
总结:其实GUI一开始就是面向对象的方式实现,首先列举成员对象和成员变量,而后public 构造(),然后事件函数。
过程:1,引包,继承,接口
2,列举对象成员
3,构造函数对成员操作,先this,再jp,this的bounds是相对屏幕,组件的bounds是相对于jp
4,事件处理函数,每一个函数用一个类实现,就相当于以前的函数用类代替,传给函数的值传给了类的构造。
5,主函数引用类,各类组合到一起实现功能。

****************************************************************************************************************************************************
package Try;
import java.io.*;
public class Save {
public String name,text;
public Save(String s,String s1)
{
name=s;
text=s1;
}
public String getName()
{
return name;
}
public String getText()
{
return text;
}

public void save()
{

File f=new File("F:/textbook",name);
if(!f.exists())
{
try
{
f.createNewFile();
}catch(IOException e1)
{

}
}
try
{
FileWriter fw=new FileWriter(f,true);

try
{
fw.write(text);

}catch(Exception e5)
{

}
finally
{
fw.close();
}
}catch(Exception e6)
{

}
}
}
总结:文件的操作
1,对象传入值,赋值构造。
2,实现功能函数,即文件的存储
(注意事项,字符流的操作,注意新建文件,创建write,读写操作close()要在异常处理中)
3,引用对象及其功能函数

****************************************************************************************************************************************************
package Try;
import java.io.*;
public class Open {
String name;
String text="";
public Open(String s)
{
name=s;
System.out.println(name);
}
public String open()
{
File f=new File("F:/",name);
try
{
FileReader fr=new FileReader(f);
int s;

try
{
while(fr.ready())
{
s=fr.read();
text=text+(char)s;
System.out.println((char)s);

}

}catch(Exception e)
{

}
finally
{
fr.close();
}
}
catch(Exception e8)
{

}

return text;
}

}
****************************************************************************************************************************************************
package Try;

import java.io.IOException;

public class Main {
public static void main(String args[])throws IOException
{
Gui g=new Gui();
/* try
{
Thread.sleep(20000);

System.out.println(Gui.s);
System.out.println(Gui.s1);
File f=new File("F:/",s);
if(!f.exists())
{
try
{
f.createNewFile();
}catch(IOException e1)
{

}
}
FileWriter fw=new FileWriter(f,true);
try
{

fw.write(s1);

}catch(Exception e2)
{

}
finally
{
fw.close();
}

}catch(Exception e5)
{

}
finally
{

}
*/

}
}
****************************************************************************************************************************************************
新建
package Try;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class New1 {
public String name;
public New1(String s)
{
name=s;

}
public String getName()
{
return name;
}

public void new1()
{

File f=new File("F:/textbook",name);
if(!f.exists())
{
try
{
f.createNewFile();
}catch(IOException e1)
{

}
}

}

}
****************************************************************************************************************************************************
选择颜色
package Try;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class Select extends JFrame implements ActionListener {
JPanel jp=new JPanel();
JButton jb1=new JButton("确认修改");

JMenuBar jm=new JMenuBar();
JMenu file=new JMenu("颜色选择");

JMenuItem red=new JMenuItem("红色");
JMenuItem org=new JMenuItem("橙色");
JMenuItem yel=new JMenuItem("黄色");
JMenuItem gre=new JMenuItem("绿色");
JMenuItem blue=new JMenuItem("蓝色");
JMenuItem bla=new JMenuItem("黑色");
public Select()
{
this.setTitle("颜色选择");
this.setBounds(300,300,200,100);
this.setVisible(true);
this.add(jp);
jp.setLayout(null);

jp.add(jb1);
jb1.setBounds(80,20,100,50);

jp.add(jm);
jm.setBounds(5,0,120,20);
jm.add(file);
jm.add(red);
jm.add(org);
file.add(yel);
file.add(gre);
file.add(blue);
file.add(bla);
file.add(red);
file.add(org);

red.addActionListener(this);
org.addActionListener(this);
yel.addActionListener(this);
gre.addActionListener(this);
blue.addActionListener(this);
bla.addActionListener(this);

}
public void actionPerformed(ActionEvent e) {

}

}
****************************************************************************************************************************************************
选择字号
package Try;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class SelectSize extends JFrame implements ActionListener {
JPanel jp=new JPanel();
JButton jb1=new JButton("确认修改");

JMenuBar jm=new JMenuBar();
JMenu file=new JMenu("字号选择");

JMenuItem one=new JMenuItem("1");
JMenuItem two=new JMenuItem("2");
JMenuItem thr=new JMenuItem("3");
JMenuItem four=new JMenuItem("4");
JMenuItem five=new JMenuItem("5");
JMenuItem six=new JMenuItem("6");
public SelectSize()
{
this.setTitle("字号选择");
this.setBounds(300,300,200,100);
this.setVisible(true);
this.add(jp);
jp.setLayout(null);

jp.add(jb1);
jb1.setBounds(80,20,100,50);

jp.add(jm);
jm.setBounds(5,0,120,20);
jm.add(file);

file.add(one);
file.add(two);
file.add(thr);
file.add(four);
file.add(five);
file.add(six);

one.addActionListener(this);
two.addActionListener(this);
thr.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);

}
public void actionPerformed(ActionEvent e) {

}

}
****************************************************************************************************************************************************
选择字体
package Try;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class SelectWord extends JFrame implements ActionListener {
JPanel jp=new JPanel();
JButton jb1=new JButton("字形修改");

JMenuBar jm=new JMenuBar();
JMenu file=new JMenu("字型选择");

JMenuItem one=new JMenuItem("宋体");
JMenuItem two=new JMenuItem("正楷");
JMenuItem thr=new JMenuItem("微软雅黑");
JMenuItem four=new JMenuItem("行书");
JMenuItem five=new JMenuItem("隶书");
JMenuItem six=new JMenuItem("甲骨文");
public SelectWord()
{
this.setTitle("字号选择");
this.setBounds(300,300,200,100);
this.setVisible(true);
this.add(jp);
jp.setLayout(null);

jp.add(jb1);
jb1.setBounds(80,20,100,50);

jp.add(jm);
jm.setBounds(5,0,120,20);
jm.add(file);

file.add(one);
file.add(two);
file.add(thr);
file.add(four);
file.add(five);
file.add(six);

one.addActionListener(this);
two.addActionListener(this);
thr.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);

}
public void actionPerformed(ActionEvent e) {

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: