我最近回答了一个关于如何在另一个类的主方法中打开登录面板的问题。
因为我还没有上过任何 Swing 课程(只有基本的 Java 编程),所以我已经偶然发现了另一个问题。
我如何检测用户是否按下了 JPanel 中的按钮并使其执行某些操作。
例如:
用户按下登录 -> if (textfield1.getText() == "user"){ 打开另一个 JFrame } -> 等等。
这是我的主要代码:
import java.awt.*;
import javax.swing.*;
public class Corendon {
public static void main(String[] args) {
showLogin();
}
private static void showLogin(){
Login login = new Login();
JFrame loginFrame = new JFrame();
loginFrame.add(login);
loginFrame.pack();
loginFrame.setLocationRelativeTo(null);
loginFrame.setVisible(true);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这是登录类:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Daan
*/
public class Login extends javax.swing.JPanel {
/**
* Creates new form Login
*/
public Login() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNINGds: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jPasswordField1 = new javax.swing.JPasswordField();
jTextField1 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Daan\\Dropbox\\HvA\\Programming\\Corendon\\corendon.png")); // NOI18N
jLabel2.setText("Username");
jLabel3.setText("Password");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dutch", "English" }));
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Cancel");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jLabel1))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(45, 45, 45)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1))
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(22, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(16, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(35, 35, 35))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
所以我尝试在 Login.java 内部工作并使用作为事件处理程序的私有方法。我做了类似的事情:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String user = jTextField1.getText();
if(user == "user") {
jTextField1.setText("LOL");
}
}
只是为了测试它是否真的做了我想要的事情,但是当我按下按钮时它没有做任何事情。之后,我尝试在 main.java 中解决它,我实际上在其中创建了包含 JPanel 的框架。在阅读了大量教程之后,它们总是在同一个 java 文件中创建 JPanel 和 JButton 等(在我的例子中是 main.java)。
那么我如何检测登录按钮是否被按下,然后让它处理 JFrame 并继续使用另一个方法,在该方法中我创建另一个包含信息的 JFrame。
我希望我足够清楚,如果不清楚,请告诉我。
编辑:
当我将测试代码更改为: user.equals("user") 时,它确实有效。但现在我需要它来处理登录框架并访问 main.java 中的另一个方法。我如何从 Login.java 中的私有方法内部实现此目的?
提前致谢,