当我按下登录按钮时,我想将一个字符串值从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) {
}
});
}
}
答案 0 :(得分: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);
}
}
注意:这不是已编译的代码,这只是针对您的问题的演示。