我使用Netbeans创建了一个GUI,它要求用户在文本字段和文本区域中输入内容。当我试图在其他类中使用它时,当我对其使用getText方法时,在文本字段中输入的文本不会出现。取而代之的是,在使用Netbeans中的编辑文本选项指定的文本字段中出现的文本,每当使用getText方法时都会出现。问题是否与某种排序或顺序有关?因为我相信GUI中的文本是在用户实际输入之前被引用的。
public class GUI extends javax.swing.JFrame {
public GUI() {
initComponents();
}
public void Grow(){
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: 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() {
jTextField1 = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
username = new javax.swing.JTextField();
UsernameLabel = new javax.swing.JLabel();
PasswordLabel = new javax.swing.JLabel();
subject = new javax.swing.JTextField();
SubjectLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
message = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
salutation = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
sendbutton = new javax.swing.JButton();
password = new javax.swing.JPasswordField();
jTextField1.setText("jTextField1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(204, 204, 255));
jPanel1.setBorder(new javax.swing.border.LineBorder(java.awt.SystemColor.controlDkShadow, 3, true));
username.setText("Enter your text here");
UsernameLabel.setFont(new java.awt.Font("Arial", 1, 13)); // NOI18N
UsernameLabel.setForeground(new java.awt.Color(255, 51, 51));
UsernameLabel.setText("Username");
PasswordLabel.setFont(new java.awt.Font("Arial", 1, 13)); // NOI18N
PasswordLabel.setForeground(new java.awt.Color(255, 51, 51));
PasswordLabel.setText("Password");
subject.setText("Enter your text here");
subject.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
subject.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
subjectActionPerformed(evt);
}
});
SubjectLabel.setFont(new java.awt.Font("Arial", 1, 13)); // NOI18N
SubjectLabel.setForeground(new java.awt.Color(255, 51, 51));
SubjectLabel.setText("Subject");
message.setColumns(20);
message.setRows(5);
message.setText("Enter your text here");
jScrollPane1.setViewportView(message);
jLabel1.setFont(new java.awt.Font("Arial", 1, 13)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 51, 51));
jLabel1.setText("Message");
salutation.setText("Enter your text here");
jLabel2.setFont(new java.awt.Font("Arial", 1, 13)); // NOI18N
jLabel2.setForeground(new java.awt.Color(255, 51, 51));
jLabel2.setText("Salutation");
sendbutton.setForeground(new java.awt.Color(255, 51, 51));
sendbutton.setText("Send");
sendbutton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendbuttonActionPerformed(evt);
}
});
password.setText("Enter your text here");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(SubjectLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(salutation)
.addComponent(subject))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(UsernameLabel, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(PasswordLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(username)
.addComponent(password))
.addGap(47, 47, 47))
.addComponent(jLabel1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(sendbutton)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(UsernameLabel)
.addComponent(subject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(SubjectLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PasswordLabel)
.addComponent(salutation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(11, 11, 11)
.addComponent(sendbutton))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void subjectActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void sendbuttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
MMMMain object = new MMMMain();
object.MassMail();
}
/**
* @param args the command line arguments
*/
public void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel PasswordLabel;
private javax.swing.JLabel SubjectLabel;
private javax.swing.JLabel UsernameLabel;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
public javax.swing.JTextArea message;
public javax.swing.JPasswordField password;
public javax.swing.JTextField salutation;
public javax.swing.JButton sendbutton;
public javax.swing.JTextField subject;
public javax.swing.JTextField username;
// End of variables declaration
}
问题是否与某种排序或顺序有关?因为我相信GUI中的文本是在用户实际输入之前被引用的。
无论您的代码实际上并没有重新创建您的问题,Swing都适用于事件驱动的场景。这意味着如果您想正确获取文本字段的文本,则必须发生事件。例如:
JTextField textField = new JTextField(20);
JFrame frame = new JFrame("Sample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(textField);
frame.pack();
frame.setVisible(true);
System.out.println(textField.getText());
然后将不会打印任何内容,因为在用户有机会编写任何内容之前调用了getText()。但如果你这样做:
final JTextField textField = new JTextField(20);
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println(textField.getText());
}
};
JFrame frame = new JFrame("Sample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(textField);
frame.pack();
frame.setVisible(true);
然后,当用户键入一些文本并按文本字段中的Enter键时,将打印文本。这是因为触发了ActionEvent,并且文本字段附加了一个ActionListener,它侦听这些事件并做出响应。您可以在Swing教程的“编写事件侦听器”部分中了解更多信息。
问题内容: 在我的应用程序中,我想要一个带有文本字段的警报。单击“完成”后,我想将文本字段输入保存为字符串。单击“取消”后,我只想关闭警报。我创建了这样的警报: 警报如下所示: 我想知道如何从文本字段中获取文本,以及如何为“完成”按钮和“取消”按钮创建事件。 问题答案: 您可以使用UIAlertController而不是UIAlertView。 我已经使用UIAlertController实现和测
我已经将查询结果格式化为MySql的样式,包含所有 ---- . 有没有办法让JTextArea中的文本和eclipse中的一样?
我正在从事一个JavaFX 2.2项目,在使用TextField控件时遇到问题。我想限制用户可以在每个文本字段中输入的字符数。但是,我找不到属性或类似maxlength的东西。Swing中也存在同样的问题,通过这种方式解决了。JavaFX 2.2如何解决这个问题?
我正在编写一个使用javafx FXML生成日志文件的程序。是否可以生成一组与仪器相关联的文本字段。示例:型号:Sn:Tg:Gas: 我需要收集每个仪器的信息,但有些地方有5个单位,有些只有1个。我正在寻找有字段生成从用户输入多少他们需要。这是我能做的吗?
我如何用材料组件做到这一点? 我不想在边框之间有这样的标签:(当你有多个填充字段时,这看起来不再好看了,在我看来,所有中断的边框都很紧张)
我在寻找一种方法来放弃我的程序中的一个getter。我有一堆动态创建的文本字段: 下面是getter,我想在这里返回聚焦的TextField 处理程序将接收聚焦字段并调用我的“model”-class中的checkInput()方法。 此刻,它接收到一个由我自己设置的预定义字段。检查答案的代码有效。感谢任何帮助我的人,如果我自己找到了一个方法,我一定会把它贴在这里。