main所在类,
创建了窗口画面,
比例不懂弄有点难看。。
按钮点击时候就会报错package com.info;import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;public class PasswordManager {
JFrame theFrame;
JTextField passwordInput;
JTextField IDInput;
JTextField companyInput;
JTextArea displayWindow;
ArrayList<Information> infoList;
Information temp;
@SuppressWarnings("unused")
public void userGui(){
theFrame = new JFrame("账号密码管理器");
JPanel mainPanel = new JPanel();
//添加存储区面板
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenuItem saveMenuItem = new JMenuItem("Save");
JMenuItem loadMenuItem = new JMenuItem("Load");
saveMenuItem.addActionListener(new SaveListener());
loadMenuItem.addActionListener(new LoadListener());
fileMenu.add(saveMenuItem);
fileMenu.add(loadMenuItem);
menuBar.add(fileMenu);
mainPanel.add(menuBar);
//添加标注
JPanel labelPanel = new JPanel();
JLabel labelC = new JLabel("公司");
JLabel labelA = new JLabel("账号");
JLabel labelB = new JLabel("密码");
labelPanel.add(labelC);
labelPanel.add(labelA);
labelPanel.add(labelB);
mainPanel.add(labelPanel);
//添加输入区面板
JPanel inputPanel = new JPanel();
passwordInput = new JTextField(20);
IDInput = new JTextField(20);
companyInput = new JTextField(20);
inputPanel.add(passwordInput);
inputPanel.add(IDInput);
inputPanel.add(companyInput);
mainPanel.add(inputPanel);
//添加按钮
JPanel buttonPanel = new JPanel();
JButton addButton = new JButton("添加");
JButton deleteButton = new JButton("删除");
JButton changeButton = new JButton("修改");
JButton searchButton = new JButton("查找");
addButton.addActionListener(new AddListener());
deleteButton.addActionListener(new DeleteListener());
changeButton.addActionListener(new ChangeListener());
searchButton.addActionListener(new SearchListener());
buttonPanel.add(searchButton);
buttonPanel.add(deleteButton);
buttonPanel.add(addButton);
buttonPanel.add(changeButton);
mainPanel.add(buttonPanel);
//添加显示窗
displayWindow = new JTextArea(150,300);
Font bigFont = new Font("sanserif",Font.BOLD,24);
displayWindow.setFont(bigFont);
displayWindow.setLineWrap(true);
displayWindow.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(displayWindow);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
mainPanel.add(scroll);
//面板加入框架
theFrame.getContentPane().add(mainPanel);
theFrame.setSize(800, 500);
theFrame.setVisible(true);
}
//GUI结束
//储存
public class SaveListener implements ActionListener{
public void actionPerformed(ActionEvent a){
PowerImpl Pi = new PowerImpl();
for(Information first:infoList){
Pi.Save(first);
}
infoList=null;
}
}
//加载
public class LoadListener implements ActionListener{
public void actionPerformed(ActionEvent a){
String linker = null;
infoList = (ArrayList<Information>)new PowerImpl().Load();
if(!infoList.isEmpty()){
ClearAndShow(infoList);
}
else{displayWindow.setText("读取失败,文件为空");} }
}
//增加监听内部类
public class AddListener implements ActionListener{
public void actionPerformed(ActionEvent a){ String IDLine = IDInput.getText();
String passwordLine = passwordInput.getText();
String companyLine = companyInput.getText();
if(IDLine==null||passwordLine==null||companyLine==null){
System.out.println("信息不完整无法添加");
}
else{
temp =new Information(companyLine,IDLine,passwordLine);
infoList.add(temp);
ClearAndShow(infoList);
}
}
}
//删除监听内部类
public class DeleteListener implements ActionListener{
public void actionPerformed(ActionEvent a){
int i = 0;
Information changing = null;
SearchListener callSearch = new SearchListener();
for(Information one:callSearch.getSearched()){
i++;
if(i>1){
System.out.println("查询结果过多");
break;
}
changing = one;
};
if(i == 1){
infoList.remove(changing);
ClearAndShow(infoList);
}
}
}
//修改监听内部类
public class ChangeListener implements ActionListener{
public void actionPerformed(ActionEvent a){
int i = 0;
Information changing = null;
SearchListener callSearch = new SearchListener();
for(Information one:callSearch.getSearched()){
i++;
if(i>1){
System.out.println("查询结果过多");
break;
}
changing = one;
};
if(i == 1){
changing.setID(IDInput.getText());
changing.setCompany(companyInput.getText());
changing.setPassword(passwordInput.getText());
ArrayList<Information> temp = new ArrayList<Information>();
temp.add(changing);
ClearAndShow(temp);
}
}
}
//查找内部类
public class SearchListener implements ActionListener{
private ArrayList<Information> searched =null;
public ArrayList<Information> getSearched() {
return searched;
} private ArrayList<Information> searching =null;
public void actionPerformed(ActionEvent a){
if(infoList == null){System.out.println("当前列表为空");}
else{
String IDLine = IDInput.getText(); String companyLine = companyInput.getText();
if(IDLine==null&&companyLine==null){
System.out.println("未查找到对象");
}
else if(companyLine == null){
searched=IDSearch(infoList,IDLine);
}
else if(IDLine == null){
searched=CompanySearch(infoList,companyLine);
}
else{
searched = CompanySearch(infoList,companyLine);
searched=IDSearch(searched,IDLine);
}
ClearAndShow(searched);
}
}
private ArrayList<Information> CompanySearch(ArrayList<Information> al ,String s){
for(Information iSon: al){
if(s.equals(iSon.getCompany())){
searching.add(iSon);
};
}
return searching;
}
private ArrayList<Information> IDSearch(ArrayList<Information> al ,String s){
for(Information iSon: al){
if(s.equals(iSon.getID())){
searching.add(iSon);
};
}
return searching;
}
}
//清楚文本窗,并显示
private void ClearAndShow(ArrayList<Information> alist){
String linker =null;
displayWindow.setText("");
for(Information one: alist){
String s =one.toString()+'\n';
linker.concat(s);
}
displayWindow.setText(linker);
}
public static void main(String[] args) {
new PasswordManager().userGui(); }
}一个接口
package com.info;public interface Power {
void Save(Object a);
Object Load();
}接口的实现类,是存储的功能package com.info;import java.io.*;
import java.util.ArrayList;public class PowerImpl implements Power {
private File file = new File("Key.ser");
private ObjectInputStream objectinput;
private ObjectOutputStream objectoutput;
@Override
public void Save(Object a) {
try{
FileOutputStream fileoutput = new FileOutputStream(file);
objectoutput = new ObjectOutputStream(fileoutput);
objectoutput.writeObject(a);
}
catch(IOException ioex){
ioex.printStackTrace();
}
} @Override
public ArrayList<Information> Load() {
Information inA = null;
ArrayList<Information> inputList=null;
try{
objectinput = new ObjectInputStream(new FileInputStream(file)); while(true){
inA = (Information)objectinput.readObject();
if(inA==null){break;}
inputList.add(inA);
}
}
catch(Exception ex){
ex.printStackTrace();
}
return inputList; }
}package com.info;public class Information implements Comparable<Information> {
private String company;
private String ID;
private String password;
public int compareTo(Information i){
return company.compareTo(i.getCompany());
}
public Information(String company, String iD, String password) {
super();
this.company = company;
ID = iD;
this.password = password;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ID == null) ? 0 : ID.hashCode());
result = prime * result + ((company == null) ? 0 : company.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Information other = (Information) obj;
if (ID == null) {
if (other.ID != null)
return false;
} else if (!ID.equals(other.ID))
return false;
if (company == null) {
if (other.company != null)
return false;
} else if (!company.equals(other.company))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
return true;
}
@Override
public String toString() {
return "\t" + company + "\t账号\t" + ID + "\t密码\t" + password ;
}}
最后一个是信息的类;当我输入信息的时候就是报错添加删除查找修改都是
创建了窗口画面,
比例不懂弄有点难看。。
按钮点击时候就会报错package com.info;import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;public class PasswordManager {
JFrame theFrame;
JTextField passwordInput;
JTextField IDInput;
JTextField companyInput;
JTextArea displayWindow;
ArrayList<Information> infoList;
Information temp;
@SuppressWarnings("unused")
public void userGui(){
theFrame = new JFrame("账号密码管理器");
JPanel mainPanel = new JPanel();
//添加存储区面板
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenuItem saveMenuItem = new JMenuItem("Save");
JMenuItem loadMenuItem = new JMenuItem("Load");
saveMenuItem.addActionListener(new SaveListener());
loadMenuItem.addActionListener(new LoadListener());
fileMenu.add(saveMenuItem);
fileMenu.add(loadMenuItem);
menuBar.add(fileMenu);
mainPanel.add(menuBar);
//添加标注
JPanel labelPanel = new JPanel();
JLabel labelC = new JLabel("公司");
JLabel labelA = new JLabel("账号");
JLabel labelB = new JLabel("密码");
labelPanel.add(labelC);
labelPanel.add(labelA);
labelPanel.add(labelB);
mainPanel.add(labelPanel);
//添加输入区面板
JPanel inputPanel = new JPanel();
passwordInput = new JTextField(20);
IDInput = new JTextField(20);
companyInput = new JTextField(20);
inputPanel.add(passwordInput);
inputPanel.add(IDInput);
inputPanel.add(companyInput);
mainPanel.add(inputPanel);
//添加按钮
JPanel buttonPanel = new JPanel();
JButton addButton = new JButton("添加");
JButton deleteButton = new JButton("删除");
JButton changeButton = new JButton("修改");
JButton searchButton = new JButton("查找");
addButton.addActionListener(new AddListener());
deleteButton.addActionListener(new DeleteListener());
changeButton.addActionListener(new ChangeListener());
searchButton.addActionListener(new SearchListener());
buttonPanel.add(searchButton);
buttonPanel.add(deleteButton);
buttonPanel.add(addButton);
buttonPanel.add(changeButton);
mainPanel.add(buttonPanel);
//添加显示窗
displayWindow = new JTextArea(150,300);
Font bigFont = new Font("sanserif",Font.BOLD,24);
displayWindow.setFont(bigFont);
displayWindow.setLineWrap(true);
displayWindow.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(displayWindow);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
mainPanel.add(scroll);
//面板加入框架
theFrame.getContentPane().add(mainPanel);
theFrame.setSize(800, 500);
theFrame.setVisible(true);
}
//GUI结束
//储存
public class SaveListener implements ActionListener{
public void actionPerformed(ActionEvent a){
PowerImpl Pi = new PowerImpl();
for(Information first:infoList){
Pi.Save(first);
}
infoList=null;
}
}
//加载
public class LoadListener implements ActionListener{
public void actionPerformed(ActionEvent a){
String linker = null;
infoList = (ArrayList<Information>)new PowerImpl().Load();
if(!infoList.isEmpty()){
ClearAndShow(infoList);
}
else{displayWindow.setText("读取失败,文件为空");} }
}
//增加监听内部类
public class AddListener implements ActionListener{
public void actionPerformed(ActionEvent a){ String IDLine = IDInput.getText();
String passwordLine = passwordInput.getText();
String companyLine = companyInput.getText();
if(IDLine==null||passwordLine==null||companyLine==null){
System.out.println("信息不完整无法添加");
}
else{
temp =new Information(companyLine,IDLine,passwordLine);
infoList.add(temp);
ClearAndShow(infoList);
}
}
}
//删除监听内部类
public class DeleteListener implements ActionListener{
public void actionPerformed(ActionEvent a){
int i = 0;
Information changing = null;
SearchListener callSearch = new SearchListener();
for(Information one:callSearch.getSearched()){
i++;
if(i>1){
System.out.println("查询结果过多");
break;
}
changing = one;
};
if(i == 1){
infoList.remove(changing);
ClearAndShow(infoList);
}
}
}
//修改监听内部类
public class ChangeListener implements ActionListener{
public void actionPerformed(ActionEvent a){
int i = 0;
Information changing = null;
SearchListener callSearch = new SearchListener();
for(Information one:callSearch.getSearched()){
i++;
if(i>1){
System.out.println("查询结果过多");
break;
}
changing = one;
};
if(i == 1){
changing.setID(IDInput.getText());
changing.setCompany(companyInput.getText());
changing.setPassword(passwordInput.getText());
ArrayList<Information> temp = new ArrayList<Information>();
temp.add(changing);
ClearAndShow(temp);
}
}
}
//查找内部类
public class SearchListener implements ActionListener{
private ArrayList<Information> searched =null;
public ArrayList<Information> getSearched() {
return searched;
} private ArrayList<Information> searching =null;
public void actionPerformed(ActionEvent a){
if(infoList == null){System.out.println("当前列表为空");}
else{
String IDLine = IDInput.getText(); String companyLine = companyInput.getText();
if(IDLine==null&&companyLine==null){
System.out.println("未查找到对象");
}
else if(companyLine == null){
searched=IDSearch(infoList,IDLine);
}
else if(IDLine == null){
searched=CompanySearch(infoList,companyLine);
}
else{
searched = CompanySearch(infoList,companyLine);
searched=IDSearch(searched,IDLine);
}
ClearAndShow(searched);
}
}
private ArrayList<Information> CompanySearch(ArrayList<Information> al ,String s){
for(Information iSon: al){
if(s.equals(iSon.getCompany())){
searching.add(iSon);
};
}
return searching;
}
private ArrayList<Information> IDSearch(ArrayList<Information> al ,String s){
for(Information iSon: al){
if(s.equals(iSon.getID())){
searching.add(iSon);
};
}
return searching;
}
}
//清楚文本窗,并显示
private void ClearAndShow(ArrayList<Information> alist){
String linker =null;
displayWindow.setText("");
for(Information one: alist){
String s =one.toString()+'\n';
linker.concat(s);
}
displayWindow.setText(linker);
}
public static void main(String[] args) {
new PasswordManager().userGui(); }
}一个接口
package com.info;public interface Power {
void Save(Object a);
Object Load();
}接口的实现类,是存储的功能package com.info;import java.io.*;
import java.util.ArrayList;public class PowerImpl implements Power {
private File file = new File("Key.ser");
private ObjectInputStream objectinput;
private ObjectOutputStream objectoutput;
@Override
public void Save(Object a) {
try{
FileOutputStream fileoutput = new FileOutputStream(file);
objectoutput = new ObjectOutputStream(fileoutput);
objectoutput.writeObject(a);
}
catch(IOException ioex){
ioex.printStackTrace();
}
} @Override
public ArrayList<Information> Load() {
Information inA = null;
ArrayList<Information> inputList=null;
try{
objectinput = new ObjectInputStream(new FileInputStream(file)); while(true){
inA = (Information)objectinput.readObject();
if(inA==null){break;}
inputList.add(inA);
}
}
catch(Exception ex){
ex.printStackTrace();
}
return inputList; }
}package com.info;public class Information implements Comparable<Information> {
private String company;
private String ID;
private String password;
public int compareTo(Information i){
return company.compareTo(i.getCompany());
}
public Information(String company, String iD, String password) {
super();
this.company = company;
ID = iD;
this.password = password;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ID == null) ? 0 : ID.hashCode());
result = prime * result + ((company == null) ? 0 : company.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Information other = (Information) obj;
if (ID == null) {
if (other.ID != null)
return false;
} else if (!ID.equals(other.ID))
return false;
if (company == null) {
if (other.company != null)
return false;
} else if (!company.equals(other.company))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
return true;
}
@Override
public String toString() {
return "\t" + company + "\t账号\t" + ID + "\t密码\t" + password ;
}}
最后一个是信息的类;当我输入信息的时候就是报错添加删除查找修改都是
解决方案 »
- java for循环及函数问题
- 请问如何设定当前写入目录?
- setOpaque这个方法怎么用?
- 欢迎java高手进组
- org.eclipse.swt.browser 中的getText() 方法
- 200分给一人,JAVA关于SQL2000 exec sp_cursorfetch 180150002, 16, ****, 1的问题
- 关于editplus的参数配置问题!紧急求教!
- 请高手看看我这样写为什么会报错了
- 关于一个程序运行次数计数器的问题
- 各位大哥大姐,请教一个关于数据库connection的理论问题,让我透过现象看本质吧.
- java矩阵顺时针90度问题,结果竟然是这样?...
- 我配置了JAVA_HOME之后javac可以用,java不可以用,WHY...
public class AddListener implements ActionListener{
public void actionPerformed(ActionEvent a){ String IDLine = IDInput.getText();
String passwordLine = passwordInput.getText();
String companyLine = companyInput.getText();
if(IDLine==null||passwordLine==null||companyLine==null){
System.out.println("信息不完整无法添加");
}
else{
temp =new Information(companyLine,IDLine,passwordLine);
try{
infoList.add(temp);
}
catch(Exception e){
e.printStackTrace();
}
ClearAndShow(infoList);
}
}
}
空指针异常,不好意思第一次发帖。
我用getText取得文本框内的输入保存到字符串
再构造函数创建对象。。
是逻辑还是语法没理解好
public class AddListener implements ActionListener{
public void actionPerformed(ActionEvent a){ String IDLine = IDInput.getText();
String passwordLine = passwordInput.getText();
String companyLine = companyInput.getText();
if(IDLine==null||passwordLine==null||companyLine==null){
System.out.println("信息不完整无法添加");
}
else{
temp =new Information(companyLine,IDLine,passwordLine);
try{
infoList.add(temp);
}
catch(Exception e){
e.printStackTrace();
}
ClearAndShow(infoList);
}
}
}
空指针异常,不好意思第一次发帖。
我用getText取得文本框内的输入保存到字符串
再构造函数创建对象。。
是逻辑还是语法没理解好
我再试了一下是ArrayList添加时出了异常,但是对象有创建成功并可以toString出来但是List.add的时候就失败了
把上面申明改为:List<Information> infolist = new ArrayList<>();
更多javafx学习资料:javafx技术工兵