设计如下GUI界面:(当用户点击“完成”按钮后,把姓名、性别、兴趣等信息显示在文本区域中,点击“取消”按钮后,所有组件回复到初始状态)
<span style="font-size:14px;"><strong>import java.awt.Button; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.TextArea; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.TextEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowStateListener; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; class denglu extends JFrame{ private JLabel userJLabel,pwdJLabel,danxuan; private JTextField text; private JPasswordField password; private JButton buttn,quxiao; private JPanel pan; private JCheckBox jcb1=new JCheckBox("听歌"); private JCheckBox jcb2=new JCheckBox("轮滑"); private JCheckBox jcb3=new JCheckBox("街舞"); private JCheckBox jcb4=new JCheckBox("计算机"); private JRadioButton xuanzeyes=new JRadioButton("男"); private JRadioButton xuanzeno=new JRadioButton("女"); private JTextArea textArea; private String name; public denglu(){ textArea=new JTextArea(15, 15); textArea.setLineWrap(true); JScrollPane jScrollPane=new JScrollPane(textArea); Container container1=this.getContentPane(); Toolkit toolkit=Toolkit.getDefaultToolkit(); Dimension dimension=toolkit.getScreenSize(); setSize(dimension.width/3,dimension.height/3); setTitle("基本信息输入窗体"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(true); Container container=this.getContentPane(); FlowLayout flowLayout=new FlowLayout(); container.setLayout(flowLayout); userJLabel=new JLabel("用户名:"); Icon icon=new ImageIcon("img/Water lilies.JPG"); userJLabel.setIcon(icon); text=new JTextField(10); danxuan=new JLabel("性别:"); ButtonGroup bu=new ButtonGroup(); bu.add(this.xuanzeyes); bu.add(this.xuanzeno); pan=new JPanel(); pan.setBorder(BorderFactory.createTitledBorder("兴趣")); pan.setLayout(new GridLayout(1, 4)); pan.add(jcb1); pan.add(jcb2); pan.add(jcb3); pan.add(jcb4); buttn=new JButton("完成(L)"); buttn.setMnemonic('j'); quxiao=new JButton("取消(C)"); quxiao.setMnemonic('C'); setVisible(true); container.add(userJLabel); container.add(text); container.add(danxuan); container.add(xuanzeyes); container.add(xuanzeno); container.add(pan); container.add(buttn); container.add(quxiao); container.add(textArea); MyActionListener listener=new MyActionListener(); buttn.addActionListener(listener); quxiao.addActionListener(listener); } class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent arg0) { if(arg0.getSource()==buttn){ String s="姓名:"; s+=text.getText(); s+="\n性别:"; s+=xuanzeyes.isSelected()?"男":"女"; s+="\n爱好:"; if(jcb2.isSelected()==true){ s+="轮滑"; } if(jcb3.isSelected()==true){ s+="街舞"; } if(jcb4.isSelected()==true){ s+="计算机"; } if(jcb1.isSelected()==true){ s+="听歌"; } textArea.setText(s); }else{ textArea.setText(null); jcb1.setSelected(false);jcb2.setSelected(false);jcb3.setSelected(false);jcb4.setSelected(false); xuanzeyes.setSelected(false);xuanzeno.setSelected(false); text.setText(null); } } } } public class fg { public static void main(String[] args) { denglu dl=new denglu(); } } </strong></span>
时间: 2024-11-14 09:02:15