实验十三:窗口设计

该博客展示了一段Java源代码,用于创建一个信息展示界面,包含按钮、文本框、单选框和下拉框等组件。当选择下拉框不同选项时,文本框会显示相应信息。此外,还分享了使用单选框、函数放置位置以及窗口关闭处理等方面的心得体会。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、源代码

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()
; }

 


}

二、结果

三、心得体会

1、单选框

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

2、有些函数要放到主函数外才对

3、 JFrame和JDialog类拥有一个setDefaultCloseOperation()方法,可用于窗口/对话框关闭处理。

转载于:https://www.cnblogs.com/nmw111/p/11074059.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值