我想这个肯定有很多的错误,只是编译的时候能通过...运行的时候不能通过。
希望大家帮帮我,我是新手..
谢谢大家啦import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;public class Register extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT =200;
private JTextField name;
private JPasswordField password;
private JPasswordField confirm;
private JLabel messageLabel;
private JLabel nameLabel;
private JLabel passwordLabel;
private JLabel confirmLabel;
private JButton ok;
private JButton reset;
private JButton exit;
private JPanel messagePanel;
private JPanel textPanel;
private JPanel buttonPanel;
public Register()
{
setTitle("Register_TestSystem");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new GridLayout(3,1));
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
throw new RuntimeException(e);
}
messagePanel = new JPanel();
messagePanel.setLayout(new FlowLayout());
messagePanel.setBackground(Color.white);
messagePanel.add(messageLabel);
messageLabel.setText("Please register first, and then to log in.");
textPanel = new JPanel();
textPanel.setLayout(new GridLayout(6,1));
textPanel.setBackground(Color.white);
textPanel.add(nameLabel);
textPanel.add(name);
textPanel.add(passwordLabel);
textPanel.add(password);
textPanel.add(confirmLabel);
textPanel.add(confirm);
nameLabel.setText("Name :");
passwordLabel.setText("Password :");
confirmLabel.setText("Confirm :");
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.white);
buttonPanel.add(ok);
buttonPanel.add(reset);
buttonPanel.add(exit);
ok = new JButton("ok");
ok.addActionListener(this);
reset = new JButton("reset");
reset.addActionListener(this);
exit = new JButton("exit");
exit.addActionListener(this);
JButton b = new JButton("shit");
content.add(messagePanel);
content.add(textPanel);
content.add(buttonPanel);
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(command.equals("ok"))
{
if(password.getText().equals(confirm.getText()))
{
BufferedReader fileInput = null;
PrintWriter output = null;
ObjectOutputStream pw = null;
String getUid;
boolean a = true;
try
{
fileInput = new BufferedReader(new FileReader("/home/kumuking/uid.txt"));
getUid = fileInput.readLine();
while(getUid != null)
{
if(getUid.equals(name.getText()))
{
a = false;
}
getUid = fileInput.readLine();
}
fileInput.close();
if(a)
{
output = new PrintWriter(new FileOutputStream("/home/kumuking/uid.txt"),true);
pw = new ObjectOutputStream(new FileOutputStream(name.getText()));
output.println(name.getText());
pw.writeUTF(password.getText());
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : The entered username has existed! TRY AGAIN!");
}
output.close();
pw.close();
}
catch(Exception e)
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : Enter two separate passwords! TRY AGAIN! ");
}
}
else if(command.equals("reset"))
{
name.setText("");
password.setText("");
confirm.setText("");
}
else if(command.equals("exit"))
{
System.exit(0);
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}}
希望大家帮帮我,我是新手..
谢谢大家啦import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;public class Register extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT =200;
private JTextField name;
private JPasswordField password;
private JPasswordField confirm;
private JLabel messageLabel;
private JLabel nameLabel;
private JLabel passwordLabel;
private JLabel confirmLabel;
private JButton ok;
private JButton reset;
private JButton exit;
private JPanel messagePanel;
private JPanel textPanel;
private JPanel buttonPanel;
public Register()
{
setTitle("Register_TestSystem");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new GridLayout(3,1));
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
throw new RuntimeException(e);
}
messagePanel = new JPanel();
messagePanel.setLayout(new FlowLayout());
messagePanel.setBackground(Color.white);
messagePanel.add(messageLabel);
messageLabel.setText("Please register first, and then to log in.");
textPanel = new JPanel();
textPanel.setLayout(new GridLayout(6,1));
textPanel.setBackground(Color.white);
textPanel.add(nameLabel);
textPanel.add(name);
textPanel.add(passwordLabel);
textPanel.add(password);
textPanel.add(confirmLabel);
textPanel.add(confirm);
nameLabel.setText("Name :");
passwordLabel.setText("Password :");
confirmLabel.setText("Confirm :");
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.white);
buttonPanel.add(ok);
buttonPanel.add(reset);
buttonPanel.add(exit);
ok = new JButton("ok");
ok.addActionListener(this);
reset = new JButton("reset");
reset.addActionListener(this);
exit = new JButton("exit");
exit.addActionListener(this);
JButton b = new JButton("shit");
content.add(messagePanel);
content.add(textPanel);
content.add(buttonPanel);
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(command.equals("ok"))
{
if(password.getText().equals(confirm.getText()))
{
BufferedReader fileInput = null;
PrintWriter output = null;
ObjectOutputStream pw = null;
String getUid;
boolean a = true;
try
{
fileInput = new BufferedReader(new FileReader("/home/kumuking/uid.txt"));
getUid = fileInput.readLine();
while(getUid != null)
{
if(getUid.equals(name.getText()))
{
a = false;
}
getUid = fileInput.readLine();
}
fileInput.close();
if(a)
{
output = new PrintWriter(new FileOutputStream("/home/kumuking/uid.txt"),true);
pw = new ObjectOutputStream(new FileOutputStream(name.getText()));
output.println(name.getText());
pw.writeUTF(password.getText());
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : The entered username has existed! TRY AGAIN!");
}
output.close();
pw.close();
}
catch(Exception e)
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : Enter two separate passwords! TRY AGAIN! ");
}
}
else if(command.equals("reset"))
{
name.setText("");
password.setText("");
confirm.setText("");
}
else if(command.equals("exit"))
{
System.exit(0);
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}}
解决方案 »
- JMStudio 注册了摄像头信息,但是无法调用,怎么办..?
- 想做一个这样的界面
- Java中使用信号量比使用synchronized有什么优点?
- 类重名的问题(java编程思想第5章)
- 为什么JDK 5.0版本装完后,调试的时候,那些错误的信息变成中文了啊?
- 连接mysql的问题
- 我把一个源码的图片替换成和原图不一样尺寸的后,不显示图片啦?
- TCP / IP数据包接收出现中断,接收不到数据
- JComboBox的下拉菜单如何向上打开??
- 编写的APPLICATION程序(.class) 文件怎么在windows下运行呀!?
- dispose()怎么使用,请大家帮忙解答!
- JDBC驱动的问题(MySql)
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;public class Register extends JFrame implements ActionListener {
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
private JTextField name = new JTextField();
private JPasswordField password = new JPasswordField();
private JPasswordField confirm = new JPasswordField();
private JLabel messageLabel = new JLabel();
private JLabel nameLabel = new JLabel();
private JLabel passwordLabel = new JLabel();
private JLabel confirmLabel = new JLabel();
private JButton ok = new JButton();
private JButton reset = new JButton();
private JButton exit = new JButton();
private JPanel messagePanel;
private JPanel textPanel;
private JPanel buttonPanel; public static void main(String[]args) {
new Register();
}
public Register() {
setTitle("Register_TestSystem");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new GridLayout(3, 1)); try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
throw new RuntimeException(e);
} messagePanel = new JPanel();
messagePanel.setLayout(new FlowLayout());
messagePanel.setBackground(Color.white);
messagePanel.add(messageLabel);
messageLabel.setText("Please register first, and then to log in."); textPanel = new JPanel();
textPanel.setLayout(new GridLayout(6, 1));
textPanel.setBackground(Color.white);
textPanel.add(nameLabel);
textPanel.add(name);
textPanel.add(passwordLabel);
textPanel.add(password);
textPanel.add(confirmLabel);
textPanel.add(confirm);
nameLabel.setText("Name :");
passwordLabel.setText("Password :");
confirmLabel.setText("Confirm :"); buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.white);
buttonPanel.add(ok);
buttonPanel.add(reset);
buttonPanel.add(exit);
ok = new JButton("ok");
ok.addActionListener(this);
reset = new JButton("reset");
reset.addActionListener(this);
exit = new JButton("exit");
exit.addActionListener(this);
JButton b = new JButton("shit"); content.add(messagePanel);
content.add(textPanel);
content.add(buttonPanel);
this.setVisible(true);
} public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("ok")) {
if (password.getText().equals(confirm.getText())) {
BufferedReader fileInput = null;
PrintWriter output = null;
ObjectOutputStream pw = null;
String getUid;
boolean a = true;
try {
fileInput = new BufferedReader(new FileReader(
"/home/kumuking/uid.txt"));
getUid = fileInput.readLine();
while (getUid != null) {
if (getUid.equals(name.getText())) {
a = false;
}
getUid = fileInput.readLine();
} fileInput.close(); if (a) {
output = new PrintWriter(new FileOutputStream(
"/home/kumuking/uid.txt"), true);
pw = new ObjectOutputStream(new FileOutputStream(name
.getText()));
output.println(name.getText());
pw.writeUTF(password.getText());
} else {
messagePanel.setBackground(Color.red);
messageLabel
.setText("ERROR : The entered username has existed! TRY AGAIN!");
} output.close();
pw.close();
} catch (Exception e) {
messagePanel.setBackground(Color.red);
messageLabel
.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
} else {
messagePanel.setBackground(Color.red);
messageLabel
.setText("ERROR : Enter two separate passwords! TRY AGAIN! ");
}
} else if (command.equals("reset")) {
name.setText("");
password.setText("");
confirm.setText("");
} else if (command.equals("exit")) {
System.exit(0);
} else {
messagePanel.setBackground(Color.red);
messageLabel
.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}}
我按你的做法添加了
public static void main(String[] args)
{
Register test = new Register();
test.setVisible(true); }
可是仍然不能显示,并且抛出了异常
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1070)
at java.awt.Container.add(Container.java:377)
at newTest.Register.<init>(Register.java:62)
at newTest.Register.main(Register.java:170)我很郁闷诶,从早上11点开始写的,结果竟然是这样的...
谢谢你,我已经找到我的错误所在啦,真是太感谢你啦现在有个更郁闷的问题。output = new PrintWriter(new FileOutputStream(
"/home/kumuking/uid.txt"), true);
这里我写的是希望能够在/home/kumuking/uid.txt 这个文件里添加新的用户名,可是为什么每次一运行,就直接把文件里原有的信息给刷没了呢?
我就是找不出和坏孩子有什么地方不一样的...package easy;import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;import UI.test1;import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;public class Register extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
private JTextField name = new JTextField();
private JPasswordField password = new JPasswordField();
private JPasswordField confirm = new JPasswordField();
private JLabel messageLabel = new JLabel();
private JLabel nameLabel = new JLabel();
private JLabel passwordLabel = new JLabel();
private JLabel confirmLabel = new JLabel();
private JButton ok = new JButton();
private JButton reset = new JButton();
private JButton exit = new JButton();
private JPanel messagePanel;
private JPanel textPanel;
private JPanel buttonPanel;
public static void main(String[] args) {
new Register();
}
public Register()
{
setTitle("Register_TestSystem");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new GridLayout(3,1));
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
throw new RuntimeException(e);
}
messagePanel = new JPanel();
messagePanel.setLayout(new FlowLayout());
messagePanel.setBackground(Color.white);
messagePanel.add(messageLabel);
messageLabel.setText("Please register first, and then to log in.");
textPanel = new JPanel();
textPanel.setLayout(new GridLayout(6,1));
textPanel.setBackground(Color.white);
textPanel.add(nameLabel);
textPanel.add(name);
textPanel.add(passwordLabel);
textPanel.add(password);
textPanel.add(confirmLabel);
textPanel.add(confirm);
nameLabel.setText("Name :");
passwordLabel.setText("Password :");
confirmLabel.setText("Confirm :");
ok = new JButton("ok");
ok.addActionListener(this);
reset = new JButton("reset");
reset.addActionListener(this);
exit = new JButton("exit");
exit.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.white);
buttonPanel.add(ok);
buttonPanel.add(reset);
buttonPanel.add(exit);
content.add(messagePanel);
content.add(textPanel);
content.add(buttonPanel);
this.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(command.equals("ok"))
{
if(password.getText().equals(confirm.getText()))
{
BufferedReader fileInput = null;
PrintWriter output = null;
ObjectOutputStream pw = null;
String getUid;
boolean a = true;
try
{
fileInput = new BufferedReader(new FileReader("/home/kumuking/uid.txt"));
getUid = fileInput.readLine();
while(getUid != null)
{
if(getUid.equals(name.getText()))
{
a = false;
}
getUid = fileInput.readLine();
}
fileInput.close();
if(a)
{
output = new PrintWriter(new FileOutputStream("/home/kumuking/uid.txt"),true);
pw = new ObjectOutputStream(new FileOutputStream(name.getText()));
output.println(name.getText());
pw.writeUTF(password.getText());
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : The entered username has existed! TRY AGAIN!");
}
output.close();
pw.close();
}
catch(Exception e)
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : Enter two separate passwords! TRY AGAIN! ");
}
}
else if(command.equals("reset"))
{
name.setText("");
password.setText("");
confirm.setText("");
}
else if(command.equals("exit"))
{
System.exit(0);
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}}难受 难受阿...