import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AddDomitry implements ActionListener {
public void actionPerformed(ActionEvent e){
JFrame frame02 = new JFrame();
frame02.setTitle("添加宿舍信息");
frame02.setBounds(450, 200, 400,400);
Container content01 = frame02.getContentPane();
//File file=new File("D:/eclipse/实训/src/studentdomitory/信息.txt");
JPanel panel = new JPanel();
JPanel panel01 = new JPanel();
JPanel panel02 = new JPanel();
JPanel panel03 = new JPanel();
JPanel panel04 = new JPanel();
JPanel panel05 = new JPanel();
JPanel panel06 = new JPanel();
JPanel panel07=new JPanel();
JPanel panel08=new JPanel();
JLabel susheno=new JLabel("宿舍编号");
JTextField textField1=new JTextField(20);
JLabel renshu=new JLabel("实住人数");
JTextField textField3=new JTextField(20);
JLabel louNo=new JLabel("楼栋编号");
JTextField textField6=new JTextField(10);
JLabel louceng=new JLabel("楼层数:7");
JLabel chuangweishu=new JLabel("床位数:4");
JLabel roomSum=new JLabel("一栋楼宿舍房间数目:70");
JLabel sex=new JLabel("性别");
String names1[] = {"男","女" };
JComboBox comboBox1 = new JComboBox( names1 );
JButton b1=new JButton("添加");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=susheno.getText();
String s2=renshu.getText();
String s3=louNo.getText();
String s4=louceng.getText();
String s5=chuangweishu.getText();
String s6=roomSum.getText();
// Object o1=comboBox1.getSelectedItem();
// File file=new File("D:/eclipse/实训/src/studentdomitory/信息.txt");
FileWriter out;
try{
/*if(!file.exists()||!file.isFile()){
file.createNewFile();
}*/
//OutputStreamWriter out=new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
out=new FileWriter("D:/eclipse/实训/src/studentdomitory/信息.txt");
out.write(s1);
out.write(s2);
out.write(s3);
out.write(s4);
out.write(s5);
out.write(s6);
//out.write((int) o1);
out.close();
JOptionPane.showMessageDialog(frame02, "ok");
}
catch(IOException e1){
e1.printStackTrace();
}
}
});
panel01.add(susheno);
panel01.add(textField1);
panel02.add(renshu);
panel02.add(textField3);
panel03.add(louNo);
panel03.add(textField6);
panel04.add(louceng);
panel05.add(chuangweishu);
panel08.add(roomSum);
panel06.add(comboBox1);
panel07.add(b1);
panel.add(panel01);
panel.add(panel02);
panel.add(panel03);
panel.add(panel04);
panel.add(panel05);
panel.add(panel06);
panel.add(panel08);
panel.add(panel07);
content01.add(panel,BorderLayout.CENTER);
frame02.setVisible(true);
}
}
8 个解决方案
#1
JLabel susheno=new JLabel("宿舍编号");
JTextField textField1=new JTextField(20);
JLabel renshu=new JLabel("实住人数");
JTextField textField3=new JTextField(20);
JLabel louNo=new JLabel("楼栋编号");
JTextField textField6=new JTextField(10);
你这3个都没有获取
JTextField textField1=new JTextField(20);
JLabel renshu=new JLabel("实住人数");
JTextField textField3=new JTextField(20);
JLabel louNo=new JLabel("楼栋编号");
JTextField textField6=new JTextField(10);
你这3个都没有获取
#2
用那么多的jPanel有必要吗?
#3
嗯,我昨天再看了一下,找到原因了,谢谢;
因为我不熟悉这个,写多几个也没什么,就写了
因为我不熟悉这个,写多几个也没什么,就写了
#4
不过我想知道,那个我保存到文件里,只能保存一条信息,这是为什么?
#5
被覆盖了
new FileInpputStream(String , boolean)
你去看一下这个重载函数的api
new FileInpputStream(String , boolean)
你去看一下这个重载函数的api
#6
谢谢,我去看看,我网上找到了一个代码,但是保存的是一堆代码,这是为什么,这是我的代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class DomitorySituation extends JPanel implements ActionListener{
Hashtable 基本宿舍信息表 =null;
JTextField 宿舍号,实住人数,楼栋号;
JRadioButton 男,女;
Domitory 宿舍 =null;
ButtonGroup group=null;
JButton 录入,重置;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
FileOutputStream outOne=null;
ObjectOutputStream outTwo=null;
File file1=null;
public DomitorySituation(File file1){
this.file1=file1;
宿舍号 =new JTextField(10);
实住人数 =new JTextField(10);
楼栋号 =new JTextField(10);
group=new ButtonGroup();
男 =new JRadioButton("男",true);
女 =new JRadioButton("女",false);
group.add(男);
group.add(女);
录入 =new JButton("录入");
重置 =new JButton("重置");
录入.addActionListener(this);
重置.addActionListener(this);
Box box1=Box.createHorizontalBox();//创建6个水平BOX容器
box1.add(new JLabel("宿舍号:",JLabel.CENTER));
box1.add(宿舍号);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("实住人数:",JLabel.CENTER));
box2.add(实住人数);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("楼栋号:",JLabel.CENTER));
box3.add(楼栋号);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("性别:",JLabel.CENTER));
box4.add(男);
box4.add(女);
Box boxH=Box.createVerticalBox();//创建一个垂直Box容器,其中从上向下排列
//4个水平Box容器.
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(录入);
pSouth.add(重置);
add(pSouth,BorderLayout.SOUTH);
validate();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==录入)
{
String susheno=" ";
susheno=宿舍号.getText();
if(susheno.length()>0)//如果已经输入宿舍号
{
try
{
inOne=new FileInputStream(file1);
inTwo=new ObjectInputStream(inOne);
基本宿舍信息表 =(Hashtable)inTwo.readObject();
//首先到文件中读取 Hashtable对象
inOne.close();
inTwo.close();
}
catch(Exception ee)
{}
if(基本宿舍信息表.containsKey(susheno))
{
String warning="该生信息已存在,请到修改页面进行修改!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
else
{
String m="基本信息将被录入!";
int ok=JOptionPane.showConfirmDialog(this,m,"确认",JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
String shizhuren=实住人数.getText();
String loudongno=楼栋号.getText();
String sex=null;
if(男.isSelected())
{
sex=男.getText();
}
else
sex=女.getText();
宿舍 =new Domitory();
宿舍.setSusheno(susheno);
宿舍.setShuzhuren(shizhuren);
宿舍.setSex(sex);
try
{
outOne=new FileOutputStream(file1);
outTwo=new ObjectOutputStream(outOne);
基本宿舍信息表.put(susheno,宿舍);
outTwo.writeObject(基本宿舍信息表);
outTwo.close();
outOne.close();
宿舍号.setText(null);
实住人数.setText(null);
楼栋号.setText(null);
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}
}
else
{
String warning="必须输入宿舍号";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource()==重置)
{
宿舍号.setText(null);
实住人数.setText(null);
楼栋号.setText(null);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class DomitorySituation extends JPanel implements ActionListener{
Hashtable 基本宿舍信息表 =null;
JTextField 宿舍号,实住人数,楼栋号;
JRadioButton 男,女;
Domitory 宿舍 =null;
ButtonGroup group=null;
JButton 录入,重置;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
FileOutputStream outOne=null;
ObjectOutputStream outTwo=null;
File file1=null;
public DomitorySituation(File file1){
this.file1=file1;
宿舍号 =new JTextField(10);
实住人数 =new JTextField(10);
楼栋号 =new JTextField(10);
group=new ButtonGroup();
男 =new JRadioButton("男",true);
女 =new JRadioButton("女",false);
group.add(男);
group.add(女);
录入 =new JButton("录入");
重置 =new JButton("重置");
录入.addActionListener(this);
重置.addActionListener(this);
Box box1=Box.createHorizontalBox();//创建6个水平BOX容器
box1.add(new JLabel("宿舍号:",JLabel.CENTER));
box1.add(宿舍号);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("实住人数:",JLabel.CENTER));
box2.add(实住人数);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("楼栋号:",JLabel.CENTER));
box3.add(楼栋号);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("性别:",JLabel.CENTER));
box4.add(男);
box4.add(女);
Box boxH=Box.createVerticalBox();//创建一个垂直Box容器,其中从上向下排列
//4个水平Box容器.
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(录入);
pSouth.add(重置);
add(pSouth,BorderLayout.SOUTH);
validate();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==录入)
{
String susheno=" ";
susheno=宿舍号.getText();
if(susheno.length()>0)//如果已经输入宿舍号
{
try
{
inOne=new FileInputStream(file1);
inTwo=new ObjectInputStream(inOne);
基本宿舍信息表 =(Hashtable)inTwo.readObject();
//首先到文件中读取 Hashtable对象
inOne.close();
inTwo.close();
}
catch(Exception ee)
{}
if(基本宿舍信息表.containsKey(susheno))
{
String warning="该生信息已存在,请到修改页面进行修改!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
else
{
String m="基本信息将被录入!";
int ok=JOptionPane.showConfirmDialog(this,m,"确认",JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
String shizhuren=实住人数.getText();
String loudongno=楼栋号.getText();
String sex=null;
if(男.isSelected())
{
sex=男.getText();
}
else
sex=女.getText();
宿舍 =new Domitory();
宿舍.setSusheno(susheno);
宿舍.setShuzhuren(shizhuren);
宿舍.setSex(sex);
try
{
outOne=new FileOutputStream(file1);
outTwo=new ObjectOutputStream(outOne);
基本宿舍信息表.put(susheno,宿舍);
outTwo.writeObject(基本宿舍信息表);
outTwo.close();
outOne.close();
宿舍号.setText(null);
实住人数.setText(null);
楼栋号.setText(null);
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}
}
else
{
String warning="必须输入宿舍号";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource()==重置)
{
宿舍号.setText(null);
实住人数.setText(null);
楼栋号.setText(null);
}
}
}
#7
谢谢,我去看了那个,终于不覆盖之前的数据了
#8
我想结贴,为什么它总是说给分与总分不符QAQ
#1
JLabel susheno=new JLabel("宿舍编号");
JTextField textField1=new JTextField(20);
JLabel renshu=new JLabel("实住人数");
JTextField textField3=new JTextField(20);
JLabel louNo=new JLabel("楼栋编号");
JTextField textField6=new JTextField(10);
你这3个都没有获取
JTextField textField1=new JTextField(20);
JLabel renshu=new JLabel("实住人数");
JTextField textField3=new JTextField(20);
JLabel louNo=new JLabel("楼栋编号");
JTextField textField6=new JTextField(10);
你这3个都没有获取
#2
用那么多的jPanel有必要吗?
#3
嗯,我昨天再看了一下,找到原因了,谢谢;
因为我不熟悉这个,写多几个也没什么,就写了
因为我不熟悉这个,写多几个也没什么,就写了
#4
不过我想知道,那个我保存到文件里,只能保存一条信息,这是为什么?
#5
被覆盖了
new FileInpputStream(String , boolean)
你去看一下这个重载函数的api
new FileInpputStream(String , boolean)
你去看一下这个重载函数的api
#6
谢谢,我去看看,我网上找到了一个代码,但是保存的是一堆代码,这是为什么,这是我的代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class DomitorySituation extends JPanel implements ActionListener{
Hashtable 基本宿舍信息表 =null;
JTextField 宿舍号,实住人数,楼栋号;
JRadioButton 男,女;
Domitory 宿舍 =null;
ButtonGroup group=null;
JButton 录入,重置;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
FileOutputStream outOne=null;
ObjectOutputStream outTwo=null;
File file1=null;
public DomitorySituation(File file1){
this.file1=file1;
宿舍号 =new JTextField(10);
实住人数 =new JTextField(10);
楼栋号 =new JTextField(10);
group=new ButtonGroup();
男 =new JRadioButton("男",true);
女 =new JRadioButton("女",false);
group.add(男);
group.add(女);
录入 =new JButton("录入");
重置 =new JButton("重置");
录入.addActionListener(this);
重置.addActionListener(this);
Box box1=Box.createHorizontalBox();//创建6个水平BOX容器
box1.add(new JLabel("宿舍号:",JLabel.CENTER));
box1.add(宿舍号);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("实住人数:",JLabel.CENTER));
box2.add(实住人数);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("楼栋号:",JLabel.CENTER));
box3.add(楼栋号);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("性别:",JLabel.CENTER));
box4.add(男);
box4.add(女);
Box boxH=Box.createVerticalBox();//创建一个垂直Box容器,其中从上向下排列
//4个水平Box容器.
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(录入);
pSouth.add(重置);
add(pSouth,BorderLayout.SOUTH);
validate();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==录入)
{
String susheno=" ";
susheno=宿舍号.getText();
if(susheno.length()>0)//如果已经输入宿舍号
{
try
{
inOne=new FileInputStream(file1);
inTwo=new ObjectInputStream(inOne);
基本宿舍信息表 =(Hashtable)inTwo.readObject();
//首先到文件中读取 Hashtable对象
inOne.close();
inTwo.close();
}
catch(Exception ee)
{}
if(基本宿舍信息表.containsKey(susheno))
{
String warning="该生信息已存在,请到修改页面进行修改!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
else
{
String m="基本信息将被录入!";
int ok=JOptionPane.showConfirmDialog(this,m,"确认",JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
String shizhuren=实住人数.getText();
String loudongno=楼栋号.getText();
String sex=null;
if(男.isSelected())
{
sex=男.getText();
}
else
sex=女.getText();
宿舍 =new Domitory();
宿舍.setSusheno(susheno);
宿舍.setShuzhuren(shizhuren);
宿舍.setSex(sex);
try
{
outOne=new FileOutputStream(file1);
outTwo=new ObjectOutputStream(outOne);
基本宿舍信息表.put(susheno,宿舍);
outTwo.writeObject(基本宿舍信息表);
outTwo.close();
outOne.close();
宿舍号.setText(null);
实住人数.setText(null);
楼栋号.setText(null);
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}
}
else
{
String warning="必须输入宿舍号";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource()==重置)
{
宿舍号.setText(null);
实住人数.setText(null);
楼栋号.setText(null);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class DomitorySituation extends JPanel implements ActionListener{
Hashtable 基本宿舍信息表 =null;
JTextField 宿舍号,实住人数,楼栋号;
JRadioButton 男,女;
Domitory 宿舍 =null;
ButtonGroup group=null;
JButton 录入,重置;
FileInputStream inOne=null;
ObjectInputStream inTwo=null;
FileOutputStream outOne=null;
ObjectOutputStream outTwo=null;
File file1=null;
public DomitorySituation(File file1){
this.file1=file1;
宿舍号 =new JTextField(10);
实住人数 =new JTextField(10);
楼栋号 =new JTextField(10);
group=new ButtonGroup();
男 =new JRadioButton("男",true);
女 =new JRadioButton("女",false);
group.add(男);
group.add(女);
录入 =new JButton("录入");
重置 =new JButton("重置");
录入.addActionListener(this);
重置.addActionListener(this);
Box box1=Box.createHorizontalBox();//创建6个水平BOX容器
box1.add(new JLabel("宿舍号:",JLabel.CENTER));
box1.add(宿舍号);
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("实住人数:",JLabel.CENTER));
box2.add(实住人数);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("楼栋号:",JLabel.CENTER));
box3.add(楼栋号);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("性别:",JLabel.CENTER));
box4.add(男);
box4.add(女);
Box boxH=Box.createVerticalBox();//创建一个垂直Box容器,其中从上向下排列
//4个水平Box容器.
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(录入);
pSouth.add(重置);
add(pSouth,BorderLayout.SOUTH);
validate();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==录入)
{
String susheno=" ";
susheno=宿舍号.getText();
if(susheno.length()>0)//如果已经输入宿舍号
{
try
{
inOne=new FileInputStream(file1);
inTwo=new ObjectInputStream(inOne);
基本宿舍信息表 =(Hashtable)inTwo.readObject();
//首先到文件中读取 Hashtable对象
inOne.close();
inTwo.close();
}
catch(Exception ee)
{}
if(基本宿舍信息表.containsKey(susheno))
{
String warning="该生信息已存在,请到修改页面进行修改!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
else
{
String m="基本信息将被录入!";
int ok=JOptionPane.showConfirmDialog(this,m,"确认",JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
String shizhuren=实住人数.getText();
String loudongno=楼栋号.getText();
String sex=null;
if(男.isSelected())
{
sex=男.getText();
}
else
sex=女.getText();
宿舍 =new Domitory();
宿舍.setSusheno(susheno);
宿舍.setShuzhuren(shizhuren);
宿舍.setSex(sex);
try
{
outOne=new FileOutputStream(file1);
outTwo=new ObjectOutputStream(outOne);
基本宿舍信息表.put(susheno,宿舍);
outTwo.writeObject(基本宿舍信息表);
outTwo.close();
outOne.close();
宿舍号.setText(null);
实住人数.setText(null);
楼栋号.setText(null);
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}
}
else
{
String warning="必须输入宿舍号";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource()==重置)
{
宿舍号.setText(null);
实住人数.setText(null);
楼栋号.setText(null);
}
}
}
#7
谢谢,我去看了那个,终于不覆盖之前的数据了
#8
我想结贴,为什么它总是说给分与总分不符QAQ