1、十/二进制相互转换
提示:文本框中敲回车会触发动作事件。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Main {
public static void main(String[] args) {
new F();
}
}
class F extends JFrame implements ActionListener{
JTextField tb=new JTextField("",25);
JTextField ta=new JTextField("",25);
//private JTextField ta,tb;
F() {
this.setTitle("进制转换");
this.setLayout(new FlowLayout());
this.add(new JLabel("二进制数"));
//ta = new JTextField("",25);
this.add(ta);
this.setLayout(new FlowLayout());
this.add(new JLabel("十进制数"));
//tb = new JTextField("",25);
this.add(tb);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 150);
this.setVisible(true);
ta.addActionListener(this);
tb.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
String s=ta.getText();
String z=tb.getText();
if(!"".equals(s)) {
int a=Integer.parseInt(ta.getText());
String m=Integer.toString(a);
int q= Integer.valueOf(m, 2);
String p=Integer.toString(q);
tb.setText(p);
}else if(!"".equals(z)) {
int b=Integer.parseInt(tb.getText());
String c=Integer.toBinaryString(b);
ta.setText(c);
}
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(this,"error");
}
}
}
2、猜数程序,随机产生一个0-200间的整数让用户去猜,每猜一次,程序给出数偏大或偏小的提示,直到用户猜中。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Main {
public static void main(String[] args) {
new F();
}
}
class F extends JFrame implements ActionListener{
//JTextField tb=new JTextField("",25);
JTextField ta=new JTextField("",20);
JLabel jl=new JLabel();
Random r = new Random();
int i = r.nextInt(200);
int sum=0;
//private JTextField ta,tb;
F() {
this.setTitle("Gusses");
this.setLayout(new FlowLayout());
this.add(new JLabel("A num between 0 and 200 is generated,plase guess it"));
//ta = new JTextField("",25);
this.add(ta);
this.setLayout(new FlowLayout());
this.add(jl);
//tb = new JTextField("",25);
//this.add(tb);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 150);
this.setVisible(true);
ta.addActionListener(this);
//tb.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
while(true) {
int a=Integer.parseInt(ta.getText());
if(a>i) {
sum=sum+1;
jl.setText("The num you guessed id too big"+"right is"+i+"sum is"+sum);
ta.setText("");
}else if(a<i) {
sum=sum+1;
jl.setText("The num you guessed id too small"+"right is"+i+"sum is"+sum);
ta.setText("");
}else if(a==i) {
sum=sum+1;
jl.setText("Congratuations!You have got the right answer for"+sum+"times");
break;
}
}
}
}
3、gui+文件练习:简单实现记事本功能,能打开文件,编辑文件,保存文件。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main extends JFrame implements ActionListener{
public static void main(String[] args) {
Main fc=new Main();
}
private JTextArea ta;
private JFileChooser jfc=new JFileChooser(new File("."));
private JScrollPane ps;
private JPanel jp1;
private JButton bOpen,bSave;
private JScrollPane jsp;
public Main(){
ta=new JTextArea(20,50);
ps=new JScrollPane(ta);
jp1 = new JPanel();
bOpen=new JButton("打开文件");
bSave=new JButton("保存文件");
jp1.add(bOpen);
jp1.add(bSave);
bOpen.addActionListener(this);
bSave.addActionListener(this);
this.add("North", jp1);
this.add("Center",ps);
this.setTitle("文件操作");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd=e.getActionCommand();
if("打开文件".equals(cmd)){
//打开文件
int status=jfc.showOpenDialog(this);
if(status!=JFileChooser.APPROVE_OPTION){
ta.setText("没有选中文件");
}
else{
try {
//被选中的文件保存为文件对象
File file=jfc.getSelectedFile();
Scanner cin=new Scanner(file);
String info="";
while(cin.hasNextLine())
{
String str=cin.nextLine();
info+=str+"\r\n";
}
//把读取的数据存到文本框中
ta.setText(info);
} catch (FileNotFoundException e1) {
System.out.println("系统没有找到此文件");
e1.printStackTrace();
}
}
}
//存
else{
int re=jfc.showOpenDialog(this);
if(re==JFileChooser.APPROVE_OPTION){
File f=jfc.getSelectedFile();
try {
FileOutputStream fsp=new FileOutputStream(f);
BufferedOutputStream out=new BufferedOutputStream(fsp);
//将文本内容转换为字节存到字节数组
byte[] b=(ta.getText()).getBytes();
//将数组b中的全部内容写到out流对应的文件中
out.write(b, 0, b.length);
out.close();
} catch (FileNotFoundException e1) {
System.out.println("error");
} catch (IOException e1) {
System.out.println("IOException");
}
}
}
}
}
打开文件:
保存文件:
4、gui+集合+文件练习:
student.txt中存放了若干学生的学号,姓名和某科成绩,它们之间用一个空格隔开。
1 2 3 4 |
|
编写图形用户界面程序实现如下功能:
(1) 读取文件(提示:读取的文件内容可用JTextArea显示)
(2) 添加记录
(3) 查找记录(按学号查找)
(4) 修改记录(按学号修改)
(5) 删除记录(按学号删除)
(6) 记录按成绩升序排列
(7) 保存文件
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Main extends JFrame implements ActionListener{
public static void main(String[] ar) {
Main fc=new Main();
}
List<Student1> studentList=new ArrayList<Student1>();;
private JTextArea jta;
private JFileChooser jfc=new JFileChooser(new File("."));
private JPanel jp1,jp2;
private JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7;
private JScrollPane jsp;
private JTextField jtf1,jtf2,jtf3;
public Main(){
this.setTitle("文件操作");
jp1 = new JPanel();
jp2 = new JPanel();
jp2.setLayout(new GridLayout(1,2,2,2));
jb1=new JButton("打开文件");
jb2=new JButton("添加");
jb3=new JButton("查找");
jb4=new JButton("修改");
jb5=new JButton("删除");
jb6=new JButton("排序");
jb7=new JButton("保存");
jta=new JTextArea(15,20);
jsp=new JScrollPane(jta);
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
jp1.add(jb1);
jp1.add(jb6);
jp1.add(jb7);
jp2.add(jsp);
JPanel jp3=new JPanel(new GridLayout(8,2,1,1));
jp3.add(new JLabel(" "));
jp3.add(new JLabel(" "));
jp3.add(new JLabel(" 学号",JLabel.CENTER));
jp3.add(jtf1);
jp3.add(new JLabel(" 姓名",JLabel.CENTER));
jp3.add(jtf2);
jp3.add(new JLabel(" 分数",JLabel.CENTER));
jp3.add(jtf3);
jp3.add(jb2);
jp3.add(jb3);
jp3.add(jb4);
jp3.add(jb5);
jp2.add(jp3);
this.add("North",jp1);this.add("Center",jp2);
this.add("South",new JLabel(" "));
this.add("West",new JLabel(" "));
this.add("East",new JLabel(" "));
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jb6.addActionListener(this);
jb7.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 400);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String jbtext=e.getActionCommand();
if("打开文件".equals(jbtext)){
int status=jfc.showOpenDialog(this);
if(status!=JFileChooser.APPROVE_OPTION){
jta.setText("没有选中文件");
}
else{
try {
//被选中的文件保存为文件对象
File file=jfc.getSelectedFile();
Scanner cin=new Scanner(file);
String info="";
while(cin.hasNextLine())
{
String str=cin.nextLine();
info+=str+"\r\n";
}
//把读取的数据存到文本框中
jta.setText(info);
} catch (FileNotFoundException e1) {
System.out.println("系统没有找到此文件");
e1.printStackTrace();
}
}
}else if("添加".equals(jbtext)){
try {
//被选中的文件保存为文件对象
File file=jfc.getSelectedFile();
Scanner cin=new Scanner(file);
while(cin.hasNext())
{
Student1 student = new Student1(cin.next(), cin.next(), cin.nextInt());
studentList.add(student);
}
} catch (FileNotFoundException e1) {
System.out.println("系统没有找到此文件");
e1.printStackTrace();
}
String sno = jtf1.getText();
String sname= jtf2.getText();
int score=Integer.parseInt(jtf3.getText());
Student1 student = new Student1(sno, sname, score);
studentList.add(student);
jta.setText("");
this.showData();
}else if("查找".equals(jbtext)){
try {
File file=jfc.getSelectedFile();
Scanner cin=new Scanner(file);
while(cin.hasNext())
{
Student1 student = new Student1(cin.next(), cin.next(), cin.nextInt());
studentList.add(student);
}
} catch (FileNotFoundException e1) {
System.out.println("系统没有找到此文件");
e1.printStackTrace();
}
ArrayList List = new ArrayList();
String sno = jtf1.getText();
for (int i = 0; i<studentList.size(); i++) {
if (studentList.get(i).getSno().equals(sno)) {
jta.setText("");
studentList.get(i);
for(Student1 st:studentList){
if(st==studentList.get(i))
jta.append(st.toString()+"\n");
}
}
}
}else if("修改".equals(jbtext)){
try {
File file=jfc.getSelectedFile();
Scanner cin=new Scanner(file);
while(cin.hasNext())
{
Student1 student = new Student1(cin.next(), cin.next(), cin.nextInt());
studentList.add(student);
}
} catch (FileNotFoundException e1) {
System.out.println("系统没有找到此文件");
e1.printStackTrace();
}
String sno = jtf1.getText();
for (int i = 0; i<studentList.size(); i++) {
if (studentList.get(i).getSno().equals(sno)) {
String sname = jtf2.getText();
studentList.get(i).setSname(sname);
int score=Integer.parseInt(jtf3.getText());
studentList.get(i).setScore(score);
}
}
jta.setText("");
this.showData();
}else if("删除".equals(jbtext)){
try {
File file=jfc.getSelectedFile();
Scanner cin=new Scanner(file);
while(cin.hasNext())
{
Student1 student = new Student1(cin.next(), cin.next(), cin.nextInt());
studentList.add(student);
}
} catch (FileNotFoundException e1) {
System.out.println("系统没有找到此文件");
e1.printStackTrace();
}
String sno = jtf1.getText();
for (int i = 0; i<studentList.size(); i++) {
if (studentList.get(i).getSno().equals(sno)) {
studentList.remove(i);
}
}
jta.setText("");
this.showData();
}else if("排序".equals(jbtext)){
try {
//被选中的文件保存为文件对象
File file=jfc.getSelectedFile();
Scanner cin=new Scanner(file);
while(cin.hasNext())
{
Student1 student = new Student1(cin.next(), cin.next(), cin.nextInt());
studentList.add(student);
}
} catch (FileNotFoundException e1) {
System.out.println("系统没有找到此文件");
e1.printStackTrace();
}
Collections.sort(studentList, new Comparator1());
jta.setText("");
this.showData();
}else if("保存".equals(jbtext)){
int re=jfc.showOpenDialog(this);
if(re==JFileChooser.APPROVE_OPTION){
File f=jfc.getSelectedFile();
try {
FileOutputStream fsp=new FileOutputStream(f);
BufferedOutputStream out=new BufferedOutputStream(fsp);
//将文本内容转换为字节存到字节数组
byte[] b=(jta.getText()).getBytes();
//将数组b中的全部内容写到out流对应的文件中
out.write(b, 0, b.length);
out.close();
} catch (FileNotFoundException e1) {
System.out.println("error");
} catch (IOException e1) {
System.out.println("IOException");
}
}
}
}
void showData(){
jta.setText("");
jta.append("学号 姓名 分数\n");
for(Student1 st:studentList){
jta.append(st.toString()+"\n");
}
}
}
class Student1 implements Comparable<Student1>{
String sno,sname;
int score;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int compareTo(Student1 arg0) {
return arg0.getScore()-this.getScore();
}
public Student1(String sno, String sname, int score) {
super();
this.sno = sno;
this.sname = sname;
this.score = score;
}
public String toString(){
return this.sno+" "+this.getSname()+" "+this.getScore();
}
}
class Comparator1 implements Comparator<Student1> {
@Override
public int compare(Student1 o1, Student1 o2) {
// TODO Auto-generated method stub
return o1.score - o2.score;
}
}
- 读取文件(提示:读取的文件内容可用JTextArea显示)
2.添加记录
3.查找记录(按学号查找)
4.修改记录(按学号修改)
5.删除记录(按学号删除)
6.记录按成绩升序排序
7.保存文件