JAVA点击按钮显示文本框,再点击按钮保存

JAVA点击按钮显示文本框,再点击按钮保存 考试要用 一点前有效 谢谢~!!!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Qxiaoi {
private JFrame frame;
private JPanel panel;
private JTextArea textArea;
private JButton buttonC, buttonS;
private boolean isNull = true;

public Qxiaoi() {
frame = new JFrame();
panel = new JPanel();
textArea = new JTextArea(10, 10);
buttonC = new JButton("检查");
buttonS = new JButton("保存");
init();
addActionHandle();
}

private void init() {
frame.add(panel);
panel.add(textArea);
panel.add(buttonC);
panel.add(buttonS);

frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private void addActionHandle() {
buttonC.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if (textArea.getText() != ""
&& textArea.getText().length() != 0) {
JOptionPane.showMessageDialog(Qxiaoi.this.frame,
"文本内容不为空!", "消息", JOptionPane.INFORMATION_MESSAGE);
isNull = false;
} else {
JOptionPane.showMessageDialog(Qxiaoi.this.frame, "文本内容为空!",
"消息", JOptionPane.INFORMATION_MESSAGE);
isNull = true;
}
}

});

buttonS.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if (!isNull) {
File file=new File("C:"+ File.separator + "save.txt");
System.out.println(textArea.getText());
FileOutputStream fos=null;
OutputStreamWriter osw=null;
try {
fos=new FileOutputStream(file);
osw=new OutputStreamWriter(fos,"UTF8");
osw.write(textArea.getText());
osw.flush();
} catch (Exception e1) {
e1.printStackTrace();
}finally{
if(osw!=null)try{osw.close();}catch(IOException e2){}
if(fos!=null)try{fos.close();}catch(IOException e3){}
}
JOptionPane.showMessageDialog(Qxiaoi.this.frame,
"保存成功!", "消息", JOptionPane.INFORMATION_MESSAGE);

} else {
JOptionPane.showMessageDialog(Qxiaoi.this.frame, "文本内容为空!",
"消息", JOptionPane.INFORMATION_MESSAGE);
}
}

});
}

public static void main(String[] args) {
new Qxiaoi();
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-06-05
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test extends JFrame implements ActionListener{
private static final long serialVersionUID = -5032790448320610769L;
private JPanel jpanel1,jpanel2;
private JButton jbutton;
private JTextField jf;
private boolean tt=true;

public Test(){
jpanel1=new JPanel();
jpanel2=new JPanel();
jf=new JTextField(10);
jbutton=new JButton("显示");
jbutton.addActionListener(this);
jpanel1.setLayout(new FlowLayout());
jpanel1.add(jbutton);
this.add(jpanel2);
this.add(jpanel1);
this.setLayout(new GridLayout(2,1));
this.setSize(200,200);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
});
}

public static void main(String [] args){
new Test();
}

public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jbutton)){
if(tt==true){
tt=false;
jbutton.setText("保存");
jpanel2.add(jf);
jpanel2.updateUI();
}else{
tt=true;
jbutton.setText("显示");
jpanel2.remove(jf);
jpanel2.updateUI();
JOptionPane.showMessageDialog(null,"文本框内容:"+jf.getText()+"保存成功!","操作提示!",JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
第2个回答  2009-06-05
import java.awt.*;
import java.awt.event.*;

public class ClickButton2 extends Frame {
Button b = new Button("Button");

TextField tf1 = new TextField(null, 10);

public ClickButton2() {
super("Click");
setLayout(new FlowLayout(FlowLayout.LEFT));

add(tf1);
tf1.setText("The text you want to display");

add(b);
pack();
setVisible(true);
tf1.setVisible(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());
}

class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (tf1.getText() != null) {
tf1.setVisible(true);
}

}

}

public static void main(String[] args) {
new ClickButton2();
}

}
第3个回答  2009-06-05
<script language="javascript">
var count=1;
function clickState(value){
if(count==1){
document.getElementById("content").style.display="";
}else if(count==2){
//这是一个假的保存
document.getElementById("content").value=document.getElementById("content").value;
//如果做真的,可以在这里进行表单提交,插入数据库中
//document.form名称.submit();
}else{
document.getElementById("content").style.display="none";
count=1;
}
count++;
}
</script>

<body>
<input type="button" name="myButton" value="点击" onclick="clickState()"><br>
<input type="text" name="content" style="display:none">
</body>
第4个回答  2009-06-05
不明白什么意思啊~~ 是点击按钮后 弹出文本框,还是本来没有文本框,点击按钮显示了,还有个保存按钮呢?
相似回答
大家正在搜