如何在java中链接类
我试图将我的登录类链接到我的笔记本类,但当我尝试运行该程序时,它只显示“登录成功”,当我按ok时,它显示为一个空白框。有人能告诉我为什么吗?我已经试着去搞乱执行的按钮操作,但结果仍然显示空白帧 登录类如何在java中链接类,java,Java,我试图将我的登录类链接到我的笔记本类,但当我尝试运行该程序时,它只显示“登录成功”,当我按ok时,它显示为一个空白框。有人能告诉我为什么吗?我已经试着去搞乱执行的按钮操作,但结果仍然显示空白帧 登录类 import java.awt.*; import java.io.*; import javax.swing.*; public class login extends JFrame{ JFrame frame = new JFrame(); JButton btnLogin
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class login extends JFrame{
JFrame frame = new JFrame();
JButton btnLogin;
JTextField fieldPass, fieldUser;
JLabel lblUser, lblPass;
String filePath="password.txt";
String filePathUser="userrname.txt";
String password="";
String username="";
BufferedWriter bw=null;
BufferedWriter bw2=null;
FileWriter fw=null;
FileWriter fw2=null;
File textFile=new File(filePath);
File textFileUser=new File(filePathUser);
Boolean login=false;
public login() {
function();
}
private void readFile() throws FileNotFoundException{
String user="";
String pass="";
boolean passFound=false;
boolean userFound=false;
BufferedReader passReader=null;
BufferedReader userReader=null;
password=fieldPass.getText();
username=fieldUser.getText();
boolean notFound=false;
try{
passReader=new BufferedReader(new FileReader("D:\\sementara buat ngoding\\password.txt"));
userReader=new BufferedReader(new FileReader("D:\\sementara buat ngoding\\username.txt"));
do{
user=userReader.readLine();
if (username.equalsIgnoreCase(user)) {
userFound=true;
}
pass=passReader.readLine();
if (password.equalsIgnoreCase(pass)) {
passFound=true;
}
if (pass==null||user==null) {
notFound=true;
}
}while(!userFound&&!passFound&&!notFound);
if (userFound&&passFound) {
login=true;
}
}
catch(IOException ex){
}
finally{
try{
if (passReader!=null) {
passReader.close();
}
if (userReader!=null) {
userReader.close();
}
}
catch(IOException ioe){
JOptionPane.showMessageDialog(null, "close error");
}
}
}
@SuppressWarnings("unchecked")
private void function() {
btnLogin = new JButton();
fieldPass = new JTextField();
fieldUser = new JTextField();
lblUser = new JLabel();
lblPass = new JLabel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnLogin.setText("LOGIN");
btnLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoginActionPerformed(evt);
}
});
fieldPass.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fieldPassActionPerformed(evt);
}
});
lblUser.setText("USERNAME:");
lblPass.setText("PASSWORD:");
}
private void fieldPassActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
try{
readFile();
if (login==true) {
JOptionPane.showMessageDialog(null, "login Success");
Laptop lapcl = new Laptop();
lapcl.setVisible(true);
}
else{
JOptionPane.showMessageDialog(null, "Login Failed");
}
}
catch(IOException ioe){
}
}
public static void main(String args[]) {
login LOG = new login();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setSize(500, 200);
panel.setBackground(Color.decode("#a020f0"));
LOG.lblUser.setBounds(72, 30, 100, 30);
LOG.lblUser.setBackground(Color.decode("#a020f0"));
LOG.lblUser.setFont(new Font("Arial",Font.BOLD,14));
LOG.fieldUser.setBounds(202, 30, 200, 30);
LOG.lblPass.setBounds(72, 60, 100, 30);
LOG.lblPass.setBackground(Color.decode("#a020f0"));
LOG.lblPass.setFont(new Font("Arial",Font.BOLD,14));
LOG.fieldPass.setBounds(202, 60, 200, 30);
LOG.btnLogin.setBounds(302, 100, 100, 30);
LOG.btnLogin.setFont(new Font("Arial",(Font.BOLD),12));
LOG.btnLogin.setBackground(Color.decode("#7d26cd"));
frame.setSize(500, 200);
frame.setVisible(true);
frame.add(LOG.lblUser);
frame.add(LOG.fieldPass);
frame.add(LOG.lblPass);
frame.add(LOG.fieldUser);
frame.add(LOG.btnLogin);
frame.add(panel);
}
}
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class Laptop extends JFrame{
JFrame frame = new JFrame();
JButton btnLaptop, btnTrans, btnHist;
public Laptop(){
function();
}
private void function() {
btnLaptop = new JButton();
btnTrans = new JButton();
btnHist = new JButton();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnLaptop.setText("Laptop Data");
btnTrans.setText("Transaction");
btnHist.setText("Transaction History");
}
public static void main(String[] args) {
Laptop LAP = new Laptop();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setSize(900, 600);
panel.setBackground(Color.decode("#a020f0"));
LAP.btnLaptop.setBounds(20, 20, 120, 30);
LAP.btnLaptop.setFont(new Font("Arial",(Font.BOLD),15));
LAP.btnLaptop.setBackground(Color.decode("#7d26cd"));
LAP.btnTrans.setBounds(160, 20, 120, 30);
LAP.btnTrans.setFont(new Font("Arial",(Font.BOLD),15));
LAP.btnTrans.setBackground(Color.decode("#7d26cd"));
LAP.btnHist.setBounds(300, 20, 120, 30);
LAP.btnHist.setFont(new Font("Arial",(Font.BOLD),15));
LAP.btnHist.setBackground(Color.decode("#7d26cd"));
frame.setVisible(true);
frame.setSize(900, 600);
frame.add(LAP.btnLaptop);
frame.add(LAP.btnTrans);
frame.add(LAP.btnHist);
frame.add(panel);
}
}
笔记本电脑类
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class login extends JFrame{
JFrame frame = new JFrame();
JButton btnLogin;
JTextField fieldPass, fieldUser;
JLabel lblUser, lblPass;
String filePath="password.txt";
String filePathUser="userrname.txt";
String password="";
String username="";
BufferedWriter bw=null;
BufferedWriter bw2=null;
FileWriter fw=null;
FileWriter fw2=null;
File textFile=new File(filePath);
File textFileUser=new File(filePathUser);
Boolean login=false;
public login() {
function();
}
private void readFile() throws FileNotFoundException{
String user="";
String pass="";
boolean passFound=false;
boolean userFound=false;
BufferedReader passReader=null;
BufferedReader userReader=null;
password=fieldPass.getText();
username=fieldUser.getText();
boolean notFound=false;
try{
passReader=new BufferedReader(new FileReader("D:\\sementara buat ngoding\\password.txt"));
userReader=new BufferedReader(new FileReader("D:\\sementara buat ngoding\\username.txt"));
do{
user=userReader.readLine();
if (username.equalsIgnoreCase(user)) {
userFound=true;
}
pass=passReader.readLine();
if (password.equalsIgnoreCase(pass)) {
passFound=true;
}
if (pass==null||user==null) {
notFound=true;
}
}while(!userFound&&!passFound&&!notFound);
if (userFound&&passFound) {
login=true;
}
}
catch(IOException ex){
}
finally{
try{
if (passReader!=null) {
passReader.close();
}
if (userReader!=null) {
userReader.close();
}
}
catch(IOException ioe){
JOptionPane.showMessageDialog(null, "close error");
}
}
}
@SuppressWarnings("unchecked")
private void function() {
btnLogin = new JButton();
fieldPass = new JTextField();
fieldUser = new JTextField();
lblUser = new JLabel();
lblPass = new JLabel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnLogin.setText("LOGIN");
btnLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoginActionPerformed(evt);
}
});
fieldPass.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fieldPassActionPerformed(evt);
}
});
lblUser.setText("USERNAME:");
lblPass.setText("PASSWORD:");
}
private void fieldPassActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
try{
readFile();
if (login==true) {
JOptionPane.showMessageDialog(null, "login Success");
Laptop lapcl = new Laptop();
lapcl.setVisible(true);
}
else{
JOptionPane.showMessageDialog(null, "Login Failed");
}
}
catch(IOException ioe){
}
}
public static void main(String args[]) {
login LOG = new login();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setSize(500, 200);
panel.setBackground(Color.decode("#a020f0"));
LOG.lblUser.setBounds(72, 30, 100, 30);
LOG.lblUser.setBackground(Color.decode("#a020f0"));
LOG.lblUser.setFont(new Font("Arial",Font.BOLD,14));
LOG.fieldUser.setBounds(202, 30, 200, 30);
LOG.lblPass.setBounds(72, 60, 100, 30);
LOG.lblPass.setBackground(Color.decode("#a020f0"));
LOG.lblPass.setFont(new Font("Arial",Font.BOLD,14));
LOG.fieldPass.setBounds(202, 60, 200, 30);
LOG.btnLogin.setBounds(302, 100, 100, 30);
LOG.btnLogin.setFont(new Font("Arial",(Font.BOLD),12));
LOG.btnLogin.setBackground(Color.decode("#7d26cd"));
frame.setSize(500, 200);
frame.setVisible(true);
frame.add(LOG.lblUser);
frame.add(LOG.fieldPass);
frame.add(LOG.lblPass);
frame.add(LOG.fieldUser);
frame.add(LOG.btnLogin);
frame.add(panel);
}
}
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class Laptop extends JFrame{
JFrame frame = new JFrame();
JButton btnLaptop, btnTrans, btnHist;
public Laptop(){
function();
}
private void function() {
btnLaptop = new JButton();
btnTrans = new JButton();
btnHist = new JButton();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnLaptop.setText("Laptop Data");
btnTrans.setText("Transaction");
btnHist.setText("Transaction History");
}
public static void main(String[] args) {
Laptop LAP = new Laptop();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setSize(900, 600);
panel.setBackground(Color.decode("#a020f0"));
LAP.btnLaptop.setBounds(20, 20, 120, 30);
LAP.btnLaptop.setFont(new Font("Arial",(Font.BOLD),15));
LAP.btnLaptop.setBackground(Color.decode("#7d26cd"));
LAP.btnTrans.setBounds(160, 20, 120, 30);
LAP.btnTrans.setFont(new Font("Arial",(Font.BOLD),15));
LAP.btnTrans.setBackground(Color.decode("#7d26cd"));
LAP.btnHist.setBounds(300, 20, 120, 30);
LAP.btnHist.setFont(new Font("Arial",(Font.BOLD),15));
LAP.btnHist.setBackground(Color.decode("#7d26cd"));
frame.setVisible(true);
frame.setSize(900, 600);
frame.add(LAP.btnLaptop);
frame.add(LAP.btnTrans);
frame.add(LAP.btnHist);
frame.add(panel);
}
}
您正在运行
login.class
文件。所以Laptop类的主要函数从未调用过。
你可以把你所做的事情放到笔记本电脑类的main方法中。
因此,登录类的btnLoginActionPerformed
方法如下
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
try{
readFile();
if (login==true) {
JOptionPane.showMessageDialog(null, "login Success");
Laptop LAP = new Laptop();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setSize(900, 600);
panel.setBackground(Color.decode("#a020f0"));
LAP.btnLaptop.setBounds(20, 20, 120, 30);
LAP.btnLaptop.setFont(new Font("Arial",(Font.BOLD),15));
LAP.btnLaptop.setBackground(Color.decode("#7d26cd"));
LAP.btnTrans.setBounds(160, 20, 120, 30);
LAP.btnTrans.setFont(new Font("Arial",(Font.BOLD),15));
LAP.btnTrans.setBackground(Color.decode("#7d26cd"));
LAP.btnHist.setBounds(300, 20, 120, 30);
LAP.btnHist.setFont(new Font("Arial",(Font.BOLD),15));
LAP.btnHist.setBackground(Color.decode("#7d26cd"));
frame.setVisible(true);
frame.setSize(900, 600);
frame.add(LAP.btnLaptop);
frame.add(LAP.btnTrans);
frame.add(LAP.btnHist);
frame.add(panel);
}
else{
JOptionPane.showMessageDialog(null, "Login Failed");
}
}
catch(IOException ioe){
}
}
您还可以删除Laptop类的main方法。因为它是无用的。为什么有两个
main
方法?此外,请遵循:类名称以PascalCase表示,变量名称以camelCase表示。另外,名为function
的方法不是一个明确的方法名称。此外,您还可以重新考虑设计,例如,您有变量userFound
和passFound
,但也有notFound
。如果你把代码分解成更小的部分,那么调试就更容易了。我看到了一些不太有意义的事情。例如,Laptop扩展了JFrame,那么为什么它仍然将JFrame声明为实例变量,而它本身就是JFrame呢。在main方法中重复同样的操作,创建一个膝上型计算机对象(实际上是一个JFrame),然后在它下面声明另一个JFrame。也许你也应该试着解释一下你希望用你的代码实现什么。正如@mcepender所建议的,也要重新考虑您的设计。为什么有空的catch
块?它可能隐藏着各种各样的问题。