-2

当我按下登录按钮时,我想将一个字符串值从 Login.java 传递给 homeframe.java。我不明白我应该写什么以及在哪一部分。我试图创建一个带有返回的方法并在第二类中调用它,但它没有用。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.TextAttribute;
import java.util.Map;

public class Login extends JFrame {

public static void main(String[] args) {
Login frameTabel = new Login();
}

JButton blogin = new JButton("Login");
JLabel bregister= new JLabel ("Register");
JLabel luser=new JLabel("username: ");
JLabel lpass=new JLabel("password: ");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);

Login(){
super("Login Authentication");
setSize(260,400);
setLocation(500,280);
panel.setLayout (null); 

luser.setBounds(10,40,80,20);
txuser.setBounds(80,40,80,20);
lpass.setBounds(10,70,80,20);
pass.setBounds(80,70,80,20);
blogin.setBounds(80,120,80,20);
bregister.setBounds(190,5,80,20);
bregister.setForeground(Color.blue);
Font font = bregister.getFont();
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
bregister.setFont(font.deriveFont(attributes));

panel.add(blogin);
panel.add(txuser);
panel.add(pass);
panel.add(bregister);
panel.add(luser);
panel.add(lpass);

getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
mouseactionlabel();

}

void mouseactionlabel(){
bregister.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
registerFrame regFac =new registerFrame();
regFac.setVisible(true);
dispose();
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
}

public void actionlogin(){
blogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String puname = txuser.getText();
String ppaswd = pass.getText();
if(puname.equals("test") && ppaswd.equals("12345")) {
homeframe regFace =new homeframe();
regFace.setVisible(true);
dispose();
} else {

JOptionPane.showMessageDialog(null,"Wrong Password or Username");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}

}
});
}
}

二等

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.TextAttribute;
import java.util.Map;

public class homeframe extends JFrame {

public static void main(String[] args) {
homeframe frameTabel = new homeframe();
}

JLabel lsearch=new JLabel ("Search by name");
JLabel lupload=new JLabel ("Upload a file");
JPanel panel = new JPanel();

homeframe(){
super("Home");
setSize(260,400);
setLocation(500,280);
panel.setLayout (null); 

lsearch.setBounds(10,30,100,20);
lupload.setBounds(10,60,80,20);

lsearch.setForeground(Color.blue);
Font font1 = lsearch.getFont();
Map attributes = font1.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
lsearch.setFont(font1.deriveFont(attributes));
lupload.setForeground(Color.blue);
Font font2 = lupload.getFont();
Map attributes2 = font2.getAttributes();
attributes2.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
lupload.setFont(font2.deriveFont(attributes2));

panel.add(lsearch);
panel.add(lupload);

getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
mouseactionlabel();
}

void mouseactionlabel(){
lsearch.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
searchFrame regFac =new searchFrame();
regFac.setVisible(true);
dispose();
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});

lupload.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
uploadFrame regFac =new uploadFrame();
regFac.setVisible(true);
dispose();
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
}

}
4

1 回答 1

0

假设当您发布到许多代码时,您的课程无法处理它:

public class Foo extends JFrame{
  private JButton login;
  public Foo(){ //....}
  @Override 
  public void actionPerformed(ActionEvent ev){
    if(ev.getSource()==login){ 
      //.....
      //first way to pass the values
       new Bar("value1","value2");
      //or
      Bar bar = new Bar();
      bar.setValue1("value1");
      bar.setValue2("value2");

    }
  }
}

这将是您希望将变量传递给它的第二帧:

public class Bar extends JFrame{
  private String value1;
  private String value2;
  //create setters and getters
  public setValue1(String value){
   value1 = value;
  }
  public setValue2(String value){
   value2 = value;
  }
 // ..... 

 //default construcor
 public Bar(){}
 //parametrize construcor
 public Bar(String value1,String value2){
  this();
  setValue1(value1);
  setValue2(value2);
 }
}

注意:这不是编译代码,这只是针对您的问题的演示。

于 2013-07-27T22:45:42.430 回答