生活随笔
收集整理的這篇文章主要介紹了
兴趣爱好选择
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
隨堂練習
實驗目的:
- 1.掌握Swing常用的組件的使用;
- 2.掌握程序界面開發(fā)的步驟;
- 3.掌握事件監(jiān)聽器的使用。
實驗內容:
在文本框中輸入姓名,選擇性別(單選)、愛好(可多選)、未來職業(yè)(下拉框),單擊“確定”按鈕,彈出消息提示框
未來職業(yè)的下拉框:
界面信息:
輸入信息點擊確定:
源代碼:
package demo
;import java
.awt
.GridLayout
;
import java
.awt
.event
.ActionEvent
;
import java
.awt
.event
.ActionListener
;import javax
.swing
.ButtonGroup
;
import javax
.swing
.JButton
;
import javax
.swing
.JCheckBox
;
import javax
.swing
.JComboBox
;
import javax
.swing
.JFrame
;
import javax
.swing
.JLabel
;
import javax
.swing
.JOptionPane
;
import javax
.swing
.JPanel
;
import javax
.swing
.JRadioButton
;
import javax
.swing
.JTextField
;public class Login extends JFrame implements ActionListener{JLabel jl1
= new JLabel("姓名:");JTextField jtf1
= new JTextField(15);JLabel sexLabel
= new JLabel("性別:");JRadioButton man
= new JRadioButton("男");JRadioButton woman
= new JRadioButton("女");ButtonGroup group
= new ButtonGroup();JLabel hobbyLabel
= new JLabel("愛好:");JCheckBox checkbox1
= new JCheckBox("音樂"); JCheckBox checkbox2
= new JCheckBox("運動");JCheckBox checkbox3
= new JCheckBox("上網");JLabel jobLabel
=new JLabel("未來職業(yè):");JComboBox
<String> job
= new JComboBox<String>();JButton jb1
= new JButton("確定");JButton jb2
= new JButton("取消");JPanel jp1
= new JPanel();JPanel jp2
= new JPanel();JPanel jp3
= new JPanel();JPanel jp4
= new JPanel();JPanel jp5
= new JPanel();public Login() {this.setLayout(new GridLayout(5,1));this.setTitle("興趣愛好選擇");this.setBounds(400, 400, 600, 400);this.setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);jp1
.add(jl1
);jp1
.add(jtf1
);jp2
.add(sexLabel
);jp2
.add(man
);jp2
.add(woman
);man
.setActionCommand("男");woman
.setActionCommand("女");group
.add(man
);group
.add(woman
);jp3
.add(hobbyLabel
);jp3
.add(checkbox1
);jp3
.add(checkbox2
);jp3
.add(checkbox3
);jp4
.add(jobLabel
);jp4
.add(job
);job
.addItem("軟件工程師");job
.addItem("經濟專家");job
.addItem("老師");job
.addItem("創(chuàng)業(yè)");jp5
.add(jb1
);jp5
.add(jb2
);this.add(jp1
);this.add(jp2
);this.add(jp3
);this.add(jp4
);this.add(jp5
);this.man
.addActionListener(this);this.woman
.addActionListener(this);this.checkbox1
.addActionListener(this);this.checkbox2
.addActionListener(this);this.checkbox3
.addActionListener(this);this.job
.addActionListener(this);this.jb1
.addActionListener(this);this.jb2
.addActionListener(this);}public static void main(String
[] args
) {Login login
= new Login();login
.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e
) {if(e
.getSource()==jb1
) {String hobby
="";String name
=jtf1
.getText();String sex
=group
.getSelection().getActionCommand();if(checkbox1
.isSelected()) hobby
+=checkbox1
.getText()+" ";if(checkbox2
.isSelected()) hobby
+=checkbox2
.getText()+" ";if(checkbox3
.isSelected()) hobby
+=checkbox3
.getText()+" ";String job1
=(String
)job
.getSelectedItem();String str
="我是"+name
+"\n性別:"+sex
+"\n愛好:"+hobby
+"\n未來職業(yè):"+job1
;JOptionPane
.showMessageDialog(this,str
);}else if(e
.getSource()==jb2
) {System
.exit(0);}}}
需要解決的問題:
(1)兩個按鈕綁定同一個監(jiān)聽器,如何判斷用戶單擊了哪個按鈕?
- 通過getSource()方法獲取監(jiān)聽的源按鈕是哪一個,從而判斷用戶按了哪一個按鈕。
(2)如何判斷單選按鈕、復選框的選中狀態(tài)及標題文本的獲取?
- 單選按鈕:通過按鈕組的getSelection()方法獲取到選擇的按鈕,再通過getActionCommand()獲取按鈕信息,從而獲得選中狀態(tài)。
- 復選框:對復選框中每一項用isSelected()方法判斷是否被選取,即可獲得選中狀態(tài)。
- 標題文本:通過文本框的getText()方法獲取標題文本信息。
(3)如何退出Java程序?
- 點擊退出按鈕(System.exit(0)😉,或者點擊右上角×按鈕。
在學霸的幫助下完成了本次任務,唉~覺得自己好菜啊!o(╥﹏╥)o,還好有CSDN,有同學,還是要繼續(xù)加油!
小白句子分享 :
- “你向一個方向走了很多年,山重水復,什么障礙都越過,卻可能在平原輕易迷失自己。”——林達《一路走來一路讀》
- “沉默會自我繁衍。越長時間不說話,就越難找到可說的話題。同理,事情擱置的時間越長,就難以討論。”——伊維塔·澤魯巴維爾《房間里的大象》
總結
以上是生活随笔為你收集整理的兴趣爱好选择的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。