import java.awt.event.*
import javax.swing.*
import java.io.*
class TextEditorFrame extends JFrame{
File file=null
Color color=Color.red
TextEditorFrame(){
initTextPane()
initAboutDialog()
initToolBar()
initMenu()
}
void initTextPane(){
getContentPane().add(new JScrollPane(text))
}
JTextPane text=new JTextPane()//这是用来做文本框的
JFileChooser filechooser=new JFileChooser()//文件选择框
JColorChooser colorchooser=new JColorChooser()//
JDialog about=new JDialog(this)//关于对话框
JMenuBar menubar=new JMenuBar()//菜单
JMenu[] menus=new JMenu[]{
new JMenu("文件"),
new JMenu("编辑"),
new JMenu("帮助")
}
JMenuItem menuitems[][]=new JMenuItem[][]{{
new JMenuItem("新建"),
new JMenuItem("打开"),
new JMenuItem("保存"),
new JMenuItem("退出")
},
{
new JMenuItem("复制"),
new JMenuItem("剪切"),
new JMenuItem("粘贴"),
new JMenuItem("颜色")
},
{
new JMenuItem("关于")
}
}
void initMenu(){
for(int i=0i<menus.lengthi++){
menubar.add(menus[i])
for(int j=0j<menuitems[i].lengthj++){
menus[i].add(menuitems[i][j])
menuitems[i][j].addActionListener( action )
}
}
this.setJMenuBar(menubar)
}
ActionListener action=new ActionListener(){ //when here have not wrong:
public void actionPerformed(ActionEvent e){
JMenuItem mi=(JMenuItem)e.getSource()
String id=mi.getText()
if(id.equals("新建")){
text.setText("")
file=null
}else if(id.equals("打开")){
if(file !=null)filechooser.setSelectedFile(file)
int returnVal=filechooser.showOpenDialog(TextEditorFrame.this)
if(returnVal==JFileChooser.APPROVE_OPTION){
file=filechooser.getSelectedFile()
openFile()
}
}else if(id.equals("保存")){
if(file!=null) filechooser.setSelectedFile(file)
int returnVal=filechooser.showSaveDialog(TextEditorFrame.this)
if(returnVal==JFileChooser.APPROVE_OPTION){
file=filechooser.getSelectedFile()
saveFile()
}
}else if(id.equals("退出")){
TextEditorFrame f=new TextEditorFrame()
int s=JOptionPane.showConfirmDialog(f,"你真的要结束吗","结束程序",JOptionPane.YES_NO_CANCEL_OPTION)
if(s==JOptionPane.YES_OPTION)
System.exit(0)
}else if(id.equals("剪切")){
text.cut()
}else if(id.equals("复制")){
text.copy()
}else if(id.equals("粘贴")){
text.paste()
}else if(id.equals("color")){
color=JColorChooser.showDialog(TextEditorFrame.this,"",color)
text.setForeground(color)
}else if(id.equals("关于")){
about.setSize(200,150)
about.show()
}
}
}
void saveFile(){
try{
FileWriter fw=new FileWriter(file)
fw.write(text.getText())
fw.close()
}
catch(Exception e){e.printStackTrace()}
}
void openFile(){
try{
FileReader fr=new FileReader(file)
int len=(int)file.length()
char []buffer=new char[len]
fr.read(buffer,0,len)
fr.close()
text.setText(new String(buffer))
}catch(Exception e){e.printStackTrace()}
}
void initAboutDialog(){
about.getContentPane().add(new JLabel("作者-覃树新:记事本Q1.0版本"))
about.setModal(true)
about.setSize(200,100)
}
JToolBar toolbar=new JToolBar()//我来加上工具条
JButton [] buttons=new JButton[]{
new JButton("", new ImageIcon("qin.jpg")),
new JButton("", new ImageIcon("shu.jpg")),
new JButton("", new ImageIcon("xin.jpg"))
}
void initToolBar(){
for(int i=0i<buttons.lengthi++)
toolbar.add(buttons[i])
buttons[0].setToolTipText("复制")
buttons[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.copy()
}
})
buttons[1].setToolTipText("剪切")
buttons[1].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.cut()
}
})
buttons[2].setToolTipText("粘贴")
buttons[2].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.paste()
}
})
this.getContentPane().add(toolbar,BorderLayout.NORTH)
}
}
public class F{
public static void main(String args[]){
TextEditorFrame f=new TextEditorFrame()
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
TextEditorFrame f=new TextEditorFrame()
int s=JOptionPane.showConfirmDialog(f,"你真的要结束吗","结束程序",JOptionPane.YES_NO_OPTION)
if(s==JOptionPane.YES_OPTION)
System.exit(0)}
})
f.setTitle("简单的记事本")
f.setSize(800,600)
f.show()
}
}
import java.awt.*import java.awt.event.*
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.FilenameFilter
import java.io.IOException
import java.io.OutputStream
import javax.swing.JFrame
import wyq.study.io.FileLister
public class MenuDemo extends Frame implements ActionListener
{
TextArea tf = new TextArea()
MenuBar bar = new MenuBar()
Menu menu = new Menu("文件")
MenuItem newf = new MenuItem("新建")
MenuItem open = new MenuItem("打开")
MenuItem saveFile = new MenuItem("保存")
MenuItem saveFileAs = new MenuItem("另存为")
MenuItem close = new MenuItem("关闭")
MenuItem quit = new MenuItem("退出")
public MenuDemo()
{
super("MenuDemo")
setMenuBar(bar)
menu.add(newf)
menu.add(open)
menu.add(close)
menu.add(saveFile)
menu.add(saveFileAs)
menu.addSeparator()
menu.add(quit)
bar.add(menu)
newf.addActionListener(this)
open.addActionListener(this)
close.addActionListener(this)
quit.addActionListener(this)
saveFile.addActionListener(this)
saveFileAs.addActionListener(this)
//addWindowListener(new WinLis())
//关闭窗口
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose()
}
})
add(tf)
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == newf)
{
tf.setText("新建")
}
if (e.getSource() == open)
{
tf.setText("打开")
}
if (e.getSource() == close)
{
tf.setText("关闭")
}
if (e.getSource() ==saveFile)
{
tf.setText("保存")
saveFile()
}
if (e.getSource() == saveFileAs)
{
tf.setText("另存为")
saveFileAs()
}
if (e.getSource() == quit)
{
System.exit(0)
}
}
public void saveFile()
{
String directory = System.getProperty("user.dir")
FileDialog f = new FileDialog(this, "Save File", FileDialog.SAVE)
f.setDirectory(directory)
f.show()
String fileName =f.getFile()// 得到保存的文件名
directory=f.getDirectory()//得到文件路径
//System.out.println(directory+" "+file)
String txt=tf.getText()
File file=new File(directory+fileName)
OutputStream out=null
try
{
out=new FileOutputStream(file)
}
catch (FileNotFoundException e)
{
}
try
{
out.write(txt.getBytes())
}
catch (IOException e)
{
e.printStackTrace()
}
try
{
if(out!=null)
out.close()
}
catch (IOException e)
{
e.printStackTrace()
}
}
public void saveFileAs()
{
saveFile()
}
class WinLis extends WindowAdapter
{
public void windowCosing(WindowEvent e)
{
System.exit(0)
}
}
public static void main(String[] args)
{
Frame f = new MenuDemo()
f.setSize(400, 200)
f.setVisible(true)
}
}
呵呵,你得保存成.java的文件,保存好了以后在开始里面点运行,在运行里面输入cmd这样就可以进入提示符窗口了,进去在光标那里输入JAVAC 你的文件名.java这样就行了.当然前提是你环境变量什么的都得设好了JDK也得装好了,祝你早日学成.欢迎分享,转载请注明来源:内存溢出
评论列表(0条)