| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 757 人关注过本帖
标题:问一个GUI的初学者问题
只看楼主 加入收藏
韩剧鼻祖
Rank: 1
等 级:新手上路
帖 子:168
专家分:0
注 册:2006-10-1
收藏
 问题点数:0 回复次数:7 
问一个GUI的初学者问题
I这个东西我想在文件中的内置记事本按钮处,点击后出现一个类似于记事本一样的东西,记事本我会做出来了,但是怎么让他跟这个有关系,还是直接点内置记事本后去new一个application做一个记事本,请高手们教教我,谢谢了,另外,我怎么才能让我的记事本有保存功能呢?
我还没有学习JDBC(是这个拼法么?)
搜索更多相关主题的帖子: GUI 记事本 application JDBC 
2006-12-07 21:16
一二三四五
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:856
专家分:0
注 册:2006-11-13
收藏
得分:0 

你想让你的记本事有保存功能,先学学java.io

你可以把文件保存成文件形式


hey,di va la
2006-12-07 21:56
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
没有明白你说的是什么意思!

2006-12-07 22:27
一二三四五
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:856
专家分:0
注 册:2006-11-13
收藏
得分:0 

没有明白我的意思?


hey,di va la
2006-12-07 22:35
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
恩?是楼主的意思没有明白!

[此贴子已经被作者于2006-12-7 22:37:45编辑过]



2006-12-07 22:37
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 

这是我以前写过的记事本(功能虽不全,但应该有你想要的东西)
package io;

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

import javax.swing.*;

public class TextBook extends JFrame implements ActionListener {

static JTextArea theArea = null; //文本输入的地方

JMenu themenu = null; //菜单

boolean colorchanged = true; // 控制自动换行的,因为不会像windows的那样打个勾

BufferedReader bf = null; //读
BufferedWriter bw = null; //写

String str = null; //好像没什么用

static FileDialog openfile = null, savefile = null; //调用打开,保存对话框

public TextBook() {
super("记事本----しΟν∈→鱈制作");
theArea = new JTextArea();
theArea.setEditable(true);
Container cp = getContentPane();
cp.add(new JScrollPane(theArea));
// cp.setLayout(new BorderLayout());
JMenuBar MBar = new JMenuBar();
MBar.setOpaque(true);

JMenu mfile = buildFileMenu();
mfile.setOpaque(true);
MBar.add(mfile);

JMenu medite = buildEditeMenu();
medite.setOpaque(true);
MBar.add(medite);

JMenu mformat = buildFormatMenu();
mformat.setOpaque(true);
MBar.add(mformat);
setJMenuBar(MBar); //都是菜单

openfile = new FileDialog(this, "打开对话框", FileDialog.LOAD);
openfile.setVisible(false);

savefile = new FileDialog(this,"保存对话框",FileDialog.SAVE);
savefile.setVisible(false);

}

public JMenu buildFileMenu() {
themenu = new JMenu("文件");

JMenuItem newf = new JMenuItem("新建");
newf.addActionListener(this);

JMenuItem open = new JMenuItem("打开");
open.addActionListener(this);

JMenuItem save = new JMenuItem("保存");
save.addActionListener(this);

JMenuItem close = new JMenuItem("关闭");
close.addActionListener(this);

JMenuItem quit = new JMenuItem("退出");
quit.addActionListener(this);

themenu.add(newf);
themenu.add(open);
themenu.add(save);
themenu.add(close);
themenu.addSeparator();
themenu.add(quit);

return themenu;
}

public JMenu buildEditeMenu() {
themenu = new JMenu("编辑");

JMenuItem copy = new JMenuItem("复制");
JMenuItem cut = new JMenuItem("剪切");
JMenuItem paste = new JMenuItem("粘帖");

themenu.add(copy);
themenu.add(cut);
themenu.add(paste);

return themenu;

}
//上面的不要解释吧,就是搞菜单的

public JMenu buildFormatMenu() {
themenu = new JMenu("格式");

JMenuItem format = new JMenuItem("自动换行");
format.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// theArea.setLineWrap(true);
// theArea.setWrapStyleWord(true);
// boolean colorchanged = true;
if (colorchanged) {
((JMenuItem) e.getSource()).setForeground(Color.red);
theArea.setLineWrap(true);
theArea.setWrapStyleWord(true);
} else {
((JMenuItem) e.getSource()).setForeground(Color.black);
theArea.setLineWrap(false);
theArea.setWrapStyleWord(false);
}
colorchanged = !colorchanged;
}
});
JMenuItem fontSize = new JMenuItem("字体");
fontSize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String size = JOptionPane.showInputDialog(new JFrame(),
"输入你要的字体大小", "字体选择", JOptionPane.YES_NO_OPTION); //得到你输入的字体大小

theArea.setFont(new Font(null, Font.BOLD,
Integer.valueOf(size)));
}
});

themenu.add(format);
themenu.add(fontSize);

return themenu;
}

public void actionPerformed(ActionEvent e) {

//if(e.getSource.equals("new")) //可以这个来判断你按的那个菜单,所以说str没用

//新建事件
str = e.getActionCommand();
if(str == "新建") {
if( !theArea.getText().equals("") ) {
int result = JOptionPane.showConfirmDialog(new JFrame(), "Text" +
"的文字已改变,要保存吗?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
// 显示一个对话框,包括确定,否和取消三个按钮,result记录你点的是那个键,根据你的选择判断下一步
if( result == JOptionPane.YES_OPTION ) //{
saveMethod(); //调用保存事件方法
theArea.setText("");
/*}

else if( result == JOptionPane.NO_OPTION )
theArea.setText(""); //消空笔记本*/
}
}

//打开事件
else if(str == "打开") {
openfile.setVisible(true);
try {
File readfile = new File(openfile.getDirectory(), openfile.getFile());
setTitle(readfile.getName() + "----记事本");
theArea.setText("");
bf = new BufferedReader(new FileReader(readfile));
while(bf.readLine() != null) {

theArea.append(bf.readLine() + "\n");
}
bf.close();
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(this,"警告","未找到该文件或文件发生异常",JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
e1.printStackTrace();
}
}

else if(str == "保存")
saveMethod(); //调用保存事件方法
else if(str == "关闭" ||str == "退出")
System.exit(0);
}

//保存事件方法
public void saveMethod() {
savefile.setVisible(true);
try {
File writefile = new File(savefile.getDirectory(), savefile.getFile());

bw = new BufferedWriter( new FileWriter(writefile));
bw.write(theArea.getText(), 0, (theArea.getText().length()));

bw.flush();
bw.close();
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(this,"警告","未找到该文件或文件发生异常",JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
e1.printStackTrace();
}
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根

JFrame jf = new TextBook();
jf.setSize(new Dimension(800, 600));
jf.addWindowListener(new WindowAdapter() { //关闭事件
public void windowClosing(WindowEvent e) {
// System.exit(0);
if (!theArea.getText().equals("")) {
int result = JOptionPane.showConfirmDialog(new JFrame(),
"Text" + "的文字已改变,要保存吗?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION); //一个选择对话框,和前面的一样
if( result == JOptionPane.YES_OPTION )
new TextBook().saveMethod(); //调用保存事件方法
}
System.exit(0);
}
});
jf.setVisible(true);

}

}



下面是人家给我的一段代码:
新建的话用setDocument(new PlainDocument())就可以了!
保存的话用这个代码
JFileChooser fc = new JFileChooser();
int returnVal = fc.showDialog(Notebook.this, "保存");
if (returnVal == JFileChooser.APPROVE_OPTION) {
String savefile = fc.getSelectedFile().toString();
try {
BufferedWriter br = new BufferedWriter(new FileWriter(
savefile));
br.write(jta.getText());
//Print;
br.flush();
br.close();
} catch (Exception ex) {

}


开开心心的过&玩每一天!!!!
2006-12-07 22:41
韩剧鼻祖
Rank: 1
等 级:新手上路
帖 子:168
专家分:0
注 册:2006-10-1
收藏
得分:0 
谢谢了,我就是需要您这里的一些代码给我的提示。太感谢了。

2006-12-08 10:33
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 
不客气  希望对你有用

开开心心的过&玩每一天!!!!
2006-12-08 12:30
快速回复:问一个GUI的初学者问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036161 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved