求各位帮帮忙,谢谢了! 问题:创建一个窗口应用程序,界面由一个文本框和两个按钮组成;点击添加按钮将文本框中输入的内容保存起来,点击显示按钮将所有被添加的内容显示出来求实现方法和源程序,有注释的,本人初学!分不够我可以加,谢谢大家了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package csdn;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class Test extends JPanel{ BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); JTextArea textArea = new JTextArea(); JButton saveButton = new JButton(); JButton showButton = new JButton(); public Test() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(borderLayout1); textArea.setText("jTextArea1"); saveButton.setText("Save"); saveButton.addActionListener(new Test_saveButton_actionAdapter(this)); showButton.setText("Show"); showButton.addActionListener(new Test_showButton_actionAdapter(this)); this.add(jPanel1, BorderLayout.SOUTH); jPanel1.add(saveButton, null); jPanel1.add(showButton, null); this.add(textArea, BorderLayout.CENTER); } /** * 保存按钮 * 在这里将信息保存到文件 * @param e */ void saveButton_actionPerformed(ActionEvent e) { String text=textArea.getText(); FileWriter fw = null; try { fw = new FileWriter("e:\\a.txt"); fw.write(text); fw.flush(); } catch (IOException ex) { }finally{ if(fw!=null){ try { fw.close(); } catch (IOException ex1) { } } } } /** * 显示按钮 * 将保存的信息显示出来 * @param e */ void showButton_actionPerformed(ActionEvent e) { FileReader fr = null; StringBuffer sb = null; try { fr = new FileReader("e:\\a.txt"); BufferedReader br = new BufferedReader(fr); String line; sb = new StringBuffer(); while ( (line = br.readLine()) != null) { sb.append(line); sb.append("\r\n"); } textArea.setText(sb.toString()); } catch (IOException ex) { }finally{ if(fr!=null){ try { fr.close(); } catch (IOException ex1) { } } } } public static void main(String[] args) { Test test1 = new Test(); JFrame f=new JFrame(); f.getContentPane().add(test1,BorderLayout.CENTER); f.show(); }}class Test_saveButton_actionAdapter implements java.awt.event.ActionListener { Test adaptee; Test_saveButton_actionAdapter(Test adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.saveButton_actionPerformed(e); }}class Test_showButton_actionAdapter implements java.awt.event.ActionListener { Test adaptee; Test_showButton_actionAdapter(Test adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.showButton_actionPerformed(e); }} 貌似不太符合要求哦!点击显示按钮将所有被添加的内容显示出来 //只保存了一个而不是所有求实现方法和源程序, //什么是实现方法呀有注释的 //有点,:)我不抢这个分了fool_leave(请及时结贴) 改改吧改好了人家再给一百! 别阿,老大,其实本人对JAVA一窍不通的,遇到了问题,就是希望大家能帮一把!谢谢了阿!刚才被人骂了,其实我也是有自尊的! 3. 创建一个窗口应用程序,界面由一个文本框和两个按钮组成;点击添加按钮将文本框中输入的内容保存起来,点击显示按钮将所有被添加的内容显示出来添加按钮存储数据,可以添加多个,然后点显示按钮显示出所有添加的数据!希望在所有的类、函数和变量前写上注释,说明其功能,我也是被逼得没有办法了!谢谢了!我发誓我要学JAVA!太郁闷!!!!!!!! 把那个例子中fw = new FileWriter("e:\\a.txt");改成fw = new FileWriter("e:\\a.txt",true);就可以了解释没办法一个一个的加自己看吧结不结贴随你 帮人帮到底package ocean.accp.demo;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextArea;public class ShowString extends JFrame implements ActionListener { private JTextArea txtInput; /** * Launch the application * @param args */ public static void main(String args[]) { try { //准备窗口对象 ShowString frame = new ShowString(); //显示窗口 frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } /** * Create the frame */ public ShowString() { super(); //设置当前窗口对象排布方式为无(任意放置) getContentPane().setLayout(null); //设置当前窗口对象的大小 setBounds(100, 100, 500, 375); //设置当前窗口对象的关闭钮处理方式 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //添加输入文本框 txtInput = new JTextArea(); txtInput.setBounds(10, 10, 472, 239); getContentPane().add(txtInput); //添加保存钮 final JButton cmdSave = new JButton(); cmdSave.addActionListener(this); cmdSave.setText("保存"); cmdSave.setBounds(79, 277, 99, 23); getContentPane().add(cmdSave); //添加显示钮 final JButton cmdShow = new JButton(); cmdShow.addActionListener(this); cmdShow.setText("显示"); cmdShow.setBounds(294, 277, 99, 23); getContentPane().add(cmdShow); } public void actionPerformed(ActionEvent e) { String filename="C:/abc.txt"; if(e.getActionCommand().equals("保存")){//如果是保存 try { //以尾部添加方式打开文件 FileWriter out=new FileWriter(filename,true); //写出文字 out.write(txtInput.getText()+"\n"); out.close(); //清空 txtInput.setText(""); } catch (IOException e1) { e1.printStackTrace(); } }else{//否则是显示 try { //以缓冲方式打开文件 BufferedReader in=new BufferedReader(new FileReader(filename)); //清空文本框 txtInput.setText(""); String txt=null; while((txt=in.readLine())!=null){//如果读出内容不为空 //追加到文本框 txtInput.append(txt+"\n"); } in.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } }} 谢谢了阿!我会去学JAVA的!再次感谢各位! 为啥我有2个else if 没起作用 java类与接口 求教基本概念!!有关实例方法和重构父类的static方法。 怎么样用java连接access数据库?-----在线等待回答 对结果疑惑,请高手解答 关于static的问题 Note: Recompile with -deprecation for details.什么意思 怎么调用自己写的类? 关于Weblogic 求大神帮一下我吧,上面的图书数量怎么与下面的订单连接。 java中为什么没有全局变量 ServerSocket和URLConnection能成一对吗?
import java.awt.*;
import java.awt.event.*;
import java.io.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Test extends JPanel{
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JTextArea textArea = new JTextArea();
JButton saveButton = new JButton();
JButton showButton = new JButton();
public Test() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setLayout(borderLayout1);
textArea.setText("jTextArea1");
saveButton.setText("Save");
saveButton.addActionListener(new Test_saveButton_actionAdapter(this));
showButton.setText("Show");
showButton.addActionListener(new Test_showButton_actionAdapter(this));
this.add(jPanel1, BorderLayout.SOUTH);
jPanel1.add(saveButton, null);
jPanel1.add(showButton, null);
this.add(textArea, BorderLayout.CENTER);
}
/**
* 保存按钮
* 在这里将信息保存到文件
* @param e
*/
void saveButton_actionPerformed(ActionEvent e) {
String text=textArea.getText();
FileWriter fw = null;
try {
fw = new FileWriter("e:\\a.txt");
fw.write(text);
fw.flush();
}
catch (IOException ex) {
}finally{
if(fw!=null){
try {
fw.close();
}
catch (IOException ex1) {
}
}
}
}
/**
* 显示按钮
* 将保存的信息显示出来
* @param e
*/
void showButton_actionPerformed(ActionEvent e) {
FileReader fr = null; StringBuffer sb = null;
try {
fr = new FileReader("e:\\a.txt");
BufferedReader br = new BufferedReader(fr);
String line;
sb = new StringBuffer();
while ( (line = br.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
}
textArea.setText(sb.toString());
}
catch (IOException ex) {
}finally{
if(fr!=null){
try {
fr.close();
}
catch (IOException ex1) {
}
}
}
}
public static void main(String[] args) {
Test test1 = new Test();
JFrame f=new JFrame();
f.getContentPane().add(test1,BorderLayout.CENTER);
f.show();
}}class Test_saveButton_actionAdapter implements java.awt.event.ActionListener {
Test adaptee; Test_saveButton_actionAdapter(Test adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.saveButton_actionPerformed(e);
}
}class Test_showButton_actionAdapter implements java.awt.event.ActionListener {
Test adaptee; Test_showButton_actionAdapter(Test adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.showButton_actionPerformed(e);
}
}
点击显示按钮将所有被添加的内容显示出来 //只保存了一个而不是所有
求实现方法和源程序, //什么是实现方法呀
有注释的 //有点,:)我不抢这个分了
fool_leave(请及时结贴)
改改吧
改好了人家再给一百!
添加按钮存储数据,可以添加多个,然后点显示按钮显示出所有添加的数据!
希望在所有的类、函数和变量前写上注释,说明其功能,我也是被逼得没有办法了!谢谢了!
我发誓我要学JAVA!太郁闷!!!!!!!!
fw = new FileWriter("e:\\a.txt");
改成fw = new FileWriter("e:\\a.txt",true);
就可以了
解释没办法一个一个的加
自己看吧结不结贴随你
package ocean.accp.demo;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;public class ShowString extends JFrame implements ActionListener { private JTextArea txtInput;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
//准备窗口对象
ShowString frame = new ShowString();
//显示窗口
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the frame
*/
public ShowString() {
super();
//设置当前窗口对象排布方式为无(任意放置)
getContentPane().setLayout(null);
//设置当前窗口对象的大小
setBounds(100, 100, 500, 375);
//设置当前窗口对象的关闭钮处理方式
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//添加输入文本框
txtInput = new JTextArea();
txtInput.setBounds(10, 10, 472, 239);
getContentPane().add(txtInput);
//添加保存钮
final JButton cmdSave = new JButton();
cmdSave.addActionListener(this);
cmdSave.setText("保存");
cmdSave.setBounds(79, 277, 99, 23);
getContentPane().add(cmdSave);
//添加显示钮
final JButton cmdShow = new JButton();
cmdShow.addActionListener(this);
cmdShow.setText("显示");
cmdShow.setBounds(294, 277, 99, 23);
getContentPane().add(cmdShow);
}
public void actionPerformed(ActionEvent e) {
String filename="C:/abc.txt";
if(e.getActionCommand().equals("保存")){//如果是保存
try {
//以尾部添加方式打开文件
FileWriter out=new FileWriter(filename,true);
//写出文字
out.write(txtInput.getText()+"\n");
out.close();
//清空
txtInput.setText("");
} catch (IOException e1) {
e1.printStackTrace();
}
}else{//否则是显示
try {
//以缓冲方式打开文件
BufferedReader in=new BufferedReader(new FileReader(filename));
//清空文本框
txtInput.setText("");
String txt=null;
while((txt=in.readLine())!=null){//如果读出内容不为空
//追加到文本框
txtInput.append(txt+"\n");
}
in.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}