中国地质大学北京信息管理与信息系统——Java程序设计第二次上机part1

实验4

package learning_swing;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.*;
public class homework4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		JFrame jf=new JFrame("成绩录入界面");
		jf.setLayout(new GridLayout(5,2));
		JLabel jl1=new JLabel("课程名称");
		JTextField jt1=new JTextField();
		JLabel jl2=new JLabel("课程学分");
		JTextField jt2=new JTextField();
		JLabel jl3=new JLabel("课程成绩");
		JTextField jt3=new JTextField();
		JLabel jl4=new JLabel("开设专业");
		JTextField jt4=new JTextField();
		JButton jb1=new JButton("提交");
		JButton jb2=new JButton("重置");
		jb2.addActionListener(new AbstractAction() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jt1.setText("");
				jt2.setText("");
				jt3.setText("");
			}
		});
		
		jb1.addActionListener(new AbstractAction() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					File file=new File("java_text.txt");
					if(!file.exists()) {
						file.createNewFile();
					}
					///Users/morty/eclipse-workspace/Nobody care
					FileWriter fileWriter=new FileWriter("java_text.txt",true);
					fileWriter.write(jl1.getText()+" ");fileWriter.write(jt1.getText()+' ');
					fileWriter.write(jl2.getText()+" ");fileWriter.write(jt2.getText()+" ");
					fileWriter.write(jl3.getText()+" ");fileWriter.write(jt3.getText()+" ");
					fileWriter.write(jl4.getText()+" ");fileWriter.write(jt4.getText()+" ");
					fileWriter.close();
				}catch(IOException e1){
					e1.printStackTrace();
				}
			}
		});
		jf.add(jl1);jf.add(jt1);jf.add(jl2);jf.add(jt2);
		jf.add(jl3);jf.add(jt3);jf.add(jl4);jf.add(jt4);
		jf.add(jb1);jf.add(jb2);
		jf.setBounds(500,500,400,400);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
}

实验5:
滚动模版测试窗口

package learning_swing;

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 homework5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				JFrame jf=new JFrame("滚动模版测试窗口");
				jf.setLayout(new GridLayout(3,1));
				JTextArea jt1=new JTextArea(300,100);  // 写入面板
				JScrollPane js=new JScrollPane(jt1);
				JButton jb1=new JButton("写入");
				JButton jb2=new JButton("读出");
				
				jb1.addActionListener(new AbstractAction() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						try {
							File file=new File("/Users/morty/Desktop/java_text88.txt");
							if(!file.exists()) {
								file.createNewFile();
							}
							///Users/morty/eclipse-workspace/Nobody care
							FileWriter fileWriter=new FileWriter("/Users/morty/Desktop/java_text88.txt",true);
							fileWriter.write(jt1.getText());
							fileWriter.close();
						}catch(IOException e1){
							e1.printStackTrace();
						}
						new one();
					}
				});
				jb2.addActionListener(new AbstractAction() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						jt1.setText("");
						try {
							String sb = new String("");
							FileReader reader = new FileReader("/Users/morty/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();
					}
				});
				jf.add(js);jf.add(jb1);jf.add(jb2);
				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);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值