int
result=result
//这是你在第一步正锋磨计算后得到的值,保存在全局变量result里面
然后
如果
你要保存到的文件目录是d:/result.txt
那么就可以实例化一个文件输出流
fileoutputstream
fis
=
new
fileoutputstream("d:/result.txt
")
fis.writeint(result)
然后举斗你要将它读出来的时候
可以实例化一个文件输入流
fileintputstream
fis
=
new
fileintputstream("d:/result.txt
")
int
result=
fis.readint(result)
system.out.print("从文件中读取的结果为:"+result)
这样就ok了
import java.awt.FlowLayoutimport java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JDialog
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JTextArea
import javax.swing.JTextField
public class MainFrame {
private JFrame frame = new JFrame()
private JButton add = new JButton("添加")
private JButton del = new JButton("删除信宴洞")
private JButton update = new JButton("修改")
private JPanel panel = new JPanel()
private JTextArea message = new JTextArea(10, 20)
private JScrollPane message_pane = new JScrollPane(message)
private JTextField name = null
private JTextField password = null
private JScrollPane update_message_pane = null
private JTextArea update_message = null
public static void main(String[] args) {
MainFrame mf = new MainFrame()
mf.init()
mf.addActionListener()
}
/祥咐/ 向文本域添加值
private class Enroll_BtnAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!name.getText().equals("") &&!password.getText().equals("")) {
message.append("姓名:" + name.getText() + " " + "密码:"
+ password.getText())
} else {
System.out.println("用户名密码为空")
}
}
}
private class Update_BtnAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
message.setText(update_message.getText())
}
}
// 为按钮添加事件
public void addActionListener() {
ActionListener add_action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog enroll_dialog = new JDialog(frame, "滑枯注册", true)
JLabel name_t = new JLabel("姓名")
name = new JTextField(10)
JLabel password_t = new JLabel("密码")
password = new JTextField(10)
JButton enroll_ok = new JButton("确定")
enroll_dialog.setLayout(new FlowLayout())
enroll_dialog.add(name_t)
enroll_dialog.add(name)
enroll_dialog.add(password_t)
enroll_dialog.add(password)
enroll_dialog.add(enroll_ok)
enroll_dialog.setBounds(20, 10, 180, 350)
enroll_ok.addActionListener(new Enroll_BtnAction())
enroll_dialog.setVisible(true)
}
}
// 清空文本域
ActionListener del_action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
message.setText("")
}
}
ActionListener update_action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog update_dialog = new JDialog(frame, "修改", true)
update_message= new JTextArea(18,20)
update_message_pane= new JScrollPane(update_message)
JButton update_ok = new JButton("确定")
update_message.setText(message.getText())
update_message.setWrapStyleWord(true)
update_dialog.add(update_message_pane)
update_dialog.add(update_ok)
update_dialog.setLayout(new FlowLayout())
update_dialog.setBounds(600, 100, 240, 400)
update_ok.addActionListener(new Update_BtnAction())
update_dialog.setVisible(true)
}
}
add.addActionListener(add_action)
del.addActionListener(del_action)
update.addActionListener(update_action)
}
// 控件初始化
public void init() {
frame.setBounds(100, 100, 400, 400)
panel.setLayout(new FlowLayout())
panel.add(add)
panel.add(del)
panel.add(update)
panel.add(message_pane)
message.setEditable(false)
frame.add(panel)
frame.setVisible(true)
frame.setDefaultCloseOperation(3)
}
}
答: 可棚蔽桐以给文本域JTextArea添加鼠标点击事件
效果图
参考代码
import java.awt.event.*import javax.swing.*
public class JTFDemo extends JFrame {
public JTFDemo() {
JTextArea jtf1 = new JTextArea()//创建文本域JTextArea
//给文本域添加事件
jtf1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {//当点击文本域时..d出对话框
JOptionPane.showMessageDialog(null, "点击了文本域")
}
})
add(jtf1)
setTitle("测试Demo")// 标题
setSize(280, 180)// 窗口大小
setLocationRelativeTo(null)// 并亏窗口居中链坦
setDefaultCloseOperation(EXIT_ON_CLOSE)// 窗口点击关闭时,退出程序
setVisible(true)// 窗口可见
}
public static void main(String[] args) {
new JTFDemo()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)