標籤:java
2.import java.awt.FlowLayout;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.JRadioButton;import javax.swing.JTextArea;import javax.swing.JTextField;public class Test {public static void main (String[] args){JFrame test= new JFrame();test.setLayout(new FlowLayout());final JTextArea text1 = new JTextArea("", 16, 16);JPanel pan1 = new JPanel();pan1.add(new JLabel("2")); final JTextField text2 = new JTextField("姓名",30);final JRadioButton rbt1 = new JRadioButton("男");final JRadioButton rbt2 = new JRadioButton("女");String provinces[] = {"河南","河北","山東","山西"};String city[] = {"洛陽","洛龍區","龍門鎮","孟津縣"};final JComboBox cb1 = new JComboBox(provinces);final JComboBox cb2 = new JComboBox(city);final JButton but1 = new JButton("添加");but1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource() == but1){String s1 = text2.getText();String s2 = null;if (rbt1.isSelected())s2 = rbt1.getText();else if (rbt2.isSelected())s2 = rbt2.getText();String s3 = cb1.getSelectedItem().toString();String s4 = cb2.getSelectedItem().toString();text1.setText(s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n");}}});test.add(text1);test.add(pan1);test.add(text2);test.add(rbt1);test.add(rbt2);test.add(cb1);test.add(cb2);test.add(but1);test.setVisible(true);test.pack();}}
本文出自 “hacker” 部落格,請務必保留此出處http://anglecode.blog.51cto.com/5628271/1619871
那些年,一起學的Java 8-2