中国地质大学北京信息管理与信息系统——Java程序设计第三次上机part0在界面排序数组并输出,滚动面板改进(第二次上机实验,part1)
作者:互联网
package Nobody_care;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class homework_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
JFrame jf=new JFrame("滚动模版测试窗口");
jf.setLayout(new GridLayout(4,1));
JTextArea jt1=new JTextArea(300,100); // 写入面板
JScrollPane js=new JScrollPane(jt1);
JButton jb1=new JButton("写入");
JButton jb2=new JButton("读出");
JButton jb3=new JButton("重置");
jb1.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
File file=new File("C:\\Users\\Dav\\Desktop\\java_text88.txt");
if(!file.exists()) {
file.createNewFile();
}
///Users/morty/eclipse-workspace/Nobody care
FileWriter fileWriter=new FileWriter("C:\\Users\\Dav\\Desktop\\java_text88.txt",true);
fileWriter.write(jt1.getText());
fileWriter.close();
}catch(IOException e1){
e1.printStackTrace();
}
jt1.setText("");
new one();
}
});
jb2.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
jt1.setText("");
try {
String sb = new String("");
FileReader reader = new FileReader("C:\\Users\\Dav\\Desktop\\java_text88.txt");
BufferedReader br = new BufferedReader(reader);
String str = null;
while ((str = br.readLine()) != null)
{
sb=sb+(str + "\n");
System.out.println(str);
}
jt1.setText(sb);
br.close();
reader.close();
}catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
new myjd2();
}
});
jb3.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
jt1.setText("");
}
});
jf.add(js);jf.add(jb1);jf.add(jb2);jf.add(jb3);
jf.setBounds(500,250,400,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class one extends JDialog{
public one() {
this.setVisible(true);this.setBounds(500,250,200,80);Container c = this.getContentPane();
JLabel l1= new JLabel("写入成功",JLabel.CENTER);
c.add(l1);
}
}
class myjd2 extends JDialog{
public myjd2() {
this.setVisible(true);this.setBounds(500,250,200,80);Container c = this.getContentPane();
JLabel l1= new JLabel("成功读入",JLabel.CENTER);
c.add(l1);
}
}
标签:Java,上机,java,import,part0,swing,new,jf,javax 来源: https://blog.csdn.net/MortyC99430/article/details/122040553