兴趣爱好选择程序java+Swing界面
生活随笔
收集整理的這篇文章主要介紹了
兴趣爱好选择程序java+Swing界面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實驗目的:
1.掌握Swing常用的組件的使用;
2.掌握程序界面開發的步驟;
3.掌握事件監聽器的使用。
實驗內容:
如圖1界面,在文本框中輸入姓名,選擇性別(單選)、愛好(可多選)、未來職業(下拉框),單擊“確定”按鈕,彈出消息提示框,如圖2,單擊“退出”按鈕結束運行。
(界面代碼已給出,請閱讀理解,補充事件監聽代碼)
?
圖1? 信息輸入界面
需要解決的問題:
(2)如何判斷單選按鈕、復選框的選中狀態及標題文本的獲取?
?(3)如何退出Java程序?
源代碼:
//SelectFrame.java文件: import javax.swing.*; import javax.swing.event.AncestorListener; import java.awt.*; import java.awt.event.*; public class SelectFrame extends JFrame implements ActionListener { //實現動作監聽器窗體private static final long serialVersionUID = 1L;private JLabel labName = new JLabel("姓名:");private JTextField tf = new JTextField(10); //文本框private JLabel sex = new JLabel("性別:");private JRadioButton male= new JRadioButton("男");private JRadioButton female = new JRadioButton("女"); private JLabel labLove = new JLabel("愛好:");private JCheckBox cbMusic = new JCheckBox("音樂"); private JCheckBox cbSport = new JCheckBox("運動"); private JCheckBox cbWeb = new JCheckBox("上網");private JLabel labDo = new JLabel("未來職業:");private String[] labs = {"軟件工程師","網絡工程師","數據庫工程師","其它"};private JComboBox labCombo = new JComboBox(labs);private JButton butOk = new JButton("確定");private JButton butExit = new JButton("退出"); private ButtonGroup bg = new ButtonGroup(); //單選按鈕組 private JPanel pan1 = new JPanel(); //創建面板1private JPanel pan2 = new JPanel();private JPanel pan3 = new JPanel();private JPanel pan4 = new JPanel();private JPanel pan5 = new JPanel();public SelectFrame() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());this.setLayout(new GridLayout(5,1));this.setTitle("興趣愛好選擇");this.setSize(500, 300);this.setLocationRelativeTo(null);//面板1加入姓名文本框pan1.add(labName);pan1.add(tf);//面板2加入性別單選組pan2.add(sex);pan2.add(male);pan2.add(female);bg.add(male);bg.add(female);//面板3 加入愛好復選框pan3.add(labLove);pan3.add(cbMusic);pan3.add(cbSport);pan3.add(cbWeb);//面板4 加入未來職業下拉框pan4.add(labDo);pan4.add(labCombo); //面板5 加入兩個按鈕pan5.add(butOk);pan5.add(butExit);//把5個中間層窗口添加到主界面this.add(pan1);this.add(pan2);this.add(pan3);this.add(pan4);this.add(pan5);butOk.addActionListener(this);butExit.addActionListener(this);}//完成實現 ActionListener接口,并完成注冊監聽器.....//***************begin***************** public void actionPerformed(ActionEvent e) {if(e.getSource()==butOk) {StringBuffer sb=new StringBuffer();sb.append("我是"+ tf.getText());sb.append("\n性別:");if(male.isSelected()){sb.append(male.getText() +" ");}if(female.isSelected()){sb.append(female.getText()+" ");}sb.append("\n愛好:");if(cbMusic.isSelected()){sb.append(cbMusic.getText() + " ");}if(cbSport.isSelected()){sb.append(cbSport.getText()+" ");}if(cbWeb.isSelected()){sb.append(cbWeb.getText()+" ");}sb.append("\n未來職業: ");if(true){sb.append(labCombo.getSelectedItem()+" ");}JOptionPane.showMessageDialog(this, sb);}if(e.getSource()==butExit){System.exit(0);}} //***************end***************** public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {SelectFrame sf= new SelectFrame();sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);sf.setVisible(true);} }總結
以上是生活随笔為你收集整理的兴趣爱好选择程序java+Swing界面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于jeecgboot的支持flowab
- 下一篇: 云服务器远程登录方法