在這裏你有一個代碼開始...但你應該檢查一些教程和谷歌一點點,知道如何使它更漂亮。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test extends JPanel {
private static JComboBox comboBox;
private static JTextField textField;
// Create a form with the fields
public Test() {
super(new BorderLayout());
// Panel for the labels
JPanel labelPanel = new JPanel(new GridLayout(2, 1)); // 2 rows 1 column
add(labelPanel, BorderLayout.WEST);
// Panel for the fields
JPanel fieldPanel = new JPanel(new GridLayout(2, 1)); // 2 rows 1 column
add(fieldPanel, BorderLayout.CENTER);
// Combobox
JLabel labelCombo = new JLabel("Bank Code");
// Options in the combobox
String[] options = { "Option1", "Option2", "Option3", "Option4", "Option15" };
comboBox = new JComboBox(options);
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Do something when you select a value
}
});
// Textfield
JLabel labelTextField = new JLabel("Bank account number");
textField = new JTextField();
// Add labels
labelPanel.add(labelCombo);
labelPanel.add(labelTextField);
// Add fields
fieldPanel.add(comboBox);
fieldPanel.add(textField);
}
public static void main(String[] args) {
final Test form = new Test();
// Button submit
JButton submit = new JButton("Submit Form");
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createIban((String) comboBox.getSelectedItem(), textField.getText());
}
});
// Frame for our test
JFrame f = new JFrame("Text Form Example");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(form, BorderLayout.NORTH);
// Panel with the button
JPanel p = new JPanel();
p.add(submit);
f.getContentPane().add(p, BorderLayout.SOUTH);
// Show the frame
f.pack();
f.setVisible(true);
}
private static void createIban(String selectedItem, String text) {
// Do stuff with your data
System.out.println("Im in createIban with the values: " + selectedItem + " and " + text);
}
}
而你究竟在哪裏卡住?請張貼一些代碼來顯示你所嘗試的內容,並解釋什麼是不工作的。在這裏,我們不會爲你寫代碼,但我們更樂於幫你克服一個特定的問題 – Robin
它相當簡單,嘗試Google搜索它有很多的幫助,當你有一些代碼和卡住的特殊部分!這裏是關於如何做的[視頻](http://www.youtube.com/watch?v=jba-TQe4h-0),大概可以跳過上半場。 –
我google了很多,但無法找到一個簡單的例子,爲我工作。所以我很高興與一個簡單的例子,如下所示。這我可以用來讓我的完整版本工作。 – Mijno