Java Swing基於event-driven architecture
的概念。因此,事件觸發行爲並基於這些事件執行某種操作。
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
,當有人點擊按鈕時,我執行某種操作。希望你得到的照片。
+1理解的問題(和一個不錯的答案) – MadProgrammer 2012-08-13 04:00:56
@MadProgrammer - 謝謝! :) – Sujay 2012-08-13 04:04:46
好的,在事件被觸發之後......我如何告訴我的「主」,以及如何告訴這個主要等待事件被觸發? – user1000232 2012-08-13 04:09:24