實驗十二:SWING界面設計

1、源代碼java

package ziwojieshao;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;網絡

public class AA extends JFrame{
public AA()
{
JFrame jf1=new JFrame ("Hellow");
jf1.setLayout(new FlowLayout());
jf1.getContentPane().add(new JButton("姓名")) ;
jf1.getContentPane().add(new JTextField("牛明旺",10)) ;

JRadioButton j1=new JRadioButton("男",true);
JRadioButton j2=new JRadioButton("女");
ButtonGroup g=new ButtonGroup();
g.add(j1);
g.add(j2);
JPanel p1=new JPanel();
p1.add(j1);
p1.add(j2);
jf1.getContentPane().add(p1);
jf1.setSize(320,210);

String proList[] = { "年齡","民族" ,"籍貫","學號","學院","專業"};
JComboBox comboBox;
Container conPane = getContentPane();
comboBox = new JComboBox(proList);
comboBox.setEditable(true);
conPane.add(comboBox);
JTextField b=new JTextField(20);
jf1.add(conPane);
jf1.add(b);
comboBox.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
if(comboBox.getSelectedItem().equals("年齡"))
b.setText("20");
else if(comboBox.getSelectedItem().equals("民族"))
b.setText("漢");
else if(comboBox.getSelectedItem().equals("籍貫"))
b.setText("河北 邯鄲");
else if(comboBox.getSelectedItem().equals("學號"))
b.setText("20173311133");
else if(comboBox.getSelectedItem().equals("學院"))
b.setText("計算機學院");
else if(comboBox.getSelectedItem().equals("專業"))
b.setText("網絡工程");
}
});函數


jf1.setVisible(true);
}
public static void main (String[] args) {
new AA()
; }orm

 


}blog

2、結果get

3、心得體會it

一、單選框io

JRadioButton j1=new JRadioButton("男"); JRadioButton j2=new JRadioButton("女");event

二、有些函數要放到主函數外才對table

三、 JFrame和JDialog類擁有一個setDefaultCloseOperation()方法,可用於窗口/對話框關閉處理。

相關文章
相關標籤/搜索