我对如何编程有点困惑。我有一个使用两个文本字段扩展 JFrame 的类。我希望能够从 JFrame 获取输入。不知何故,只需等待我的 mainMaybe 类的输入...有什么想法吗?
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class mainMaybe extends JFrame{
JTextField login;
JTextField pass;
public mainMaybe() throws InterruptedException{
login = new JTextField();
pass = new JTextField();
pass.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("DO SOMETHING");
}
});
add(login,BorderLayout.NORTH);
add(pass,BorderLayout.SOUTH);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void returnEmail(){
//SOME HOW WAIT FOR THE ACTION LISTENER?
}
public static void main(String[] args) throws InterruptedException {
new mainMaybe().returnEmail();
//CONTROL SHOULD STAY WITH mainMaybe until an email is returned
}
}
最佳答案
Java Swing 基于事件驱动架构
的概念。因此,事件会触发操作,并且根据这些事件,您将执行某种操作。
Using Swing Component开始理解并最终实现您想要在 UI 及其相应行为方面实现的目标将是一个很好的起点。因此,我建议您阅读一些相关内容,然后尝试解决您想要实现的目标。
我不太确定你想要实现什么,但如果我是你,我会有两个文本字段,可能附加一些标签和一个“提交”按钮。我会向此按钮添加一个 ActionListener
来执行某种操作。
也许是SSCCE可能会帮助您入门。那么看看这段代码:
package com.test;
import javax.swing.JOptionPane;
public class SimpleUI extends javax.swing.JFrame {
/**
* Creates new form SimpleUI
*/
public SimpleUI() {
initComponents();
}
private void initComponents() {
mainPanel = new javax.swing.JPanel();
labelName = new javax.swing.JLabel();
textName = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
textPassword = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2));
labelName.setText("Name: ");
jLabel1.setText("Password: ");
jButton1.setText("Submit");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(labelName)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(textName)
.addComponent(textPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(0, 65, Short.MAX_VALUE))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(labelName)
.addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addContainerGap(15, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}
/**
* This is the method that is called when an action is performed.
* Over here I just simply show an error message if any of the text fields are empty or just show their names.
*/
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name = textName.getText();
String password = String.valueOf(textPassword.getPassword());
if("".equals(password) || "".equals(name)){
JOptionPane.showMessageDialog(this, "Name or password is empty!", "Incorrect Input", JOptionPane.ERROR_MESSAGE);
}else{
JOptionPane.showMessageDialog(this, "Hello "+name+" with password "+password, "Incorrect Input", JOptionPane.PLAIN_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SimpleUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SimpleUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SimpleUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SimpleUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SimpleUI().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel labelName;
private javax.swing.JPanel mainPanel;
private javax.swing.JTextField textName;
private javax.swing.JPasswordField textPassword;
}
注意:我使用 NetBeans IDE 生成了此代码,因此我不必处理太多有关布局或任何内容的事情。如果您想了解布局,这里有一个有趣的教程:"A Visual Guide to Layout Managers"
我的主要想法是向您展示事件是如何触发的。所以我所做的就是在“提交”按钮中添加一个 actionlistener
,当有人单击该按钮时,我会执行某种操作。希望你能得到这张照片。
关于java - 从 JFrame 获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927963/