⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentwrite.java

📁 Java程序设计实验与实训源代码经典的JAVA学习教材
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;


class StudentWrite{
	public static void main(String[] args) {
		MyFrameNine myFrame=new MyFrameNine();
		myFrame.show();
	}
}

class MyFrameNine extends JFrame implements ActionListener{
	private JLabel xhLabel;
	private JLabel xmLabel;
	private JLabel fsLabel;
	private JTextField xhTF;
	private JTextField xmTF;
	private JTextField fsTF;
	private JButton bcButton;
	private JButton bcszButton;
	private FileDialog fd;	
	private String fpath;
	public static PrintWriter myPW;
	public static boolean flag;
		
MyFrameNine(){
		xhLabel=new JLabel("学号");
		xmLabel=new JLabel("姓名");
		fsLabel=new JLabel("分数");
		xhTF=new JTextField(10);
		xmTF=new JTextField(10);
		fsTF=new JTextField(10);
		bcButton=new JButton("保存");
		bcszButton=new JButton("文件设置");
		bcButton.addActionListener(this);
		bcszButton.addActionListener(this);
				
		Container myContentPane=getContentPane();
		myContentPane.setLayout(new FlowLayout());
		myContentPane.add(xhLabel);
		myContentPane.add(xhTF);
		myContentPane.add(xmLabel);
		myContentPane.add(xmTF);
		myContentPane.add(fsLabel);
		myContentPane.add(fsTF);
		myContentPane.add(bcButton);
		myContentPane.add(bcszButton);
		
		setSize(180,200);
		addWindowListener(new QuitWindow());		
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand()=="文件设置"){
			fd=new FileDialog(this,"保存文件",FileDialog.SAVE);
			fd.show();
			fpath=fd.getFile();
			if(fpath!=null){
				fpath=fd.getDirectory()+fpath;
				flag=true;
				try{
					myPW=new PrintWriter(new BufferedWriter(new FileWriter(fpath)));
					}catch(IOException ioe){
						System.err.println("无法保存文件"+fpath);
						System.exit(1);
					}
			
			}
		}		
		if(e.getActionCommand()=="保存"){
			if(flag){
				myPW.println(Integer.parseInt(xhTF.getText())+":"+xmTF.getText()+":"+Double.parseDouble(fsTF.getText()));
				xhTF.setText("");
				xmTF.setText("");
				fsTF.setText("");
			}
			else
				JOptionPane.showMessageDialog(this,"你没有选择保存的文件","警告",JOptionPane.ERROR_MESSAGE);
		}
	}
}

class QuitWindow extends WindowAdapter{
	public void windowClosing(WindowEvent e){
		if(MyFrameNine.flag)
			MyFrameNine.myPW.close();
		System.exit(1);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -