这次的作业使我的提升很大,尤其是树形和列表都是第一次使用,通过查阅API文档,让我清晰明了的知道了其用法,首先,对布局的应用有了进一步的提升,让我能够熟练运用了网状、流式布局,在添加事件响应的时候,不同的按钮对应不同的监听器,使我有了更加清晰的影响。
先附上GUI界面图:
package bokeyuan; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Vector; import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.KeyStroke; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeModel; public class gui1 { private JFrame frame; String xueke="语文",xuanxiu="音乐鉴赏", shifou="是",paizi="奔驰"; private static final String VERSION = "Version1.0"; public gui1(){ makeFrame(); } private void makeFrame() { // TODO Auto-generated method stub frame = new JFrame("问卷调查"); makeMenuBar(frame); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("感谢您来填写本次调查问卷!"); contentPane.add(label,BorderLayout.NORTH); JLabel statusLabel = new JLabel(VERSION); contentPane.add(statusLabel, BorderLayout.SOUTH); JPanel panelWest = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel panelInWest = new JPanel(); DefaultMutableTreeNode top = new DefaultMutableTreeNode("请展开选择问卷类型"); JTree jt = new JTree(top); DefaultMutableTreeNode category = null; DefaultMutableTreeNode book = null; category = new DefaultMutableTreeNode("最喜欢的车型"); book = new DefaultMutableTreeNode("最喜欢的学科"); top.add(category); top.add(book); // // panelInWest.add(jt); panelWest.add(panelInWest); contentPane.add(panelWest, BorderLayout.WEST); // JPanel panelCenter1 = new JPanel(); JPanel panelCenter2 = new JPanel(); JPanel panelCenter = new JPanel(new GridLayout(3,1)); JLabel label1 = new JLabel("你喜欢车吗?"); JRadioButton jr = new JRadioButton("是",true); JRadioButton jr1 = new JRadioButton("否"); ButtonGroup bg = new ButtonGroup(); bg.add(jr); bg.add(jr1); panelCenter1.add(label1); panelCenter1.add(jr); panelCenter1.add(jr1); // jr.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { shifou="是"; }}); jr1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { shifou="否"; }}); // JLabel label2 = new JLabel("请从以下车牌中选择你喜爱的"); JCheckBox jr2 = new JCheckBox("奔驰"); JCheckBox jr3 = new JCheckBox("宝马"); JCheckBox jr4 = new JCheckBox("奥迪"); JCheckBox jr5 = new JCheckBox("路虎"); JCheckBox jr6 = new JCheckBox("捷豹"); JCheckBox jr7 = new JCheckBox("保时捷"); JCheckBox jr8 = new JCheckBox("玛莎拉蒂"); panelCenter2.add(label2); panelCenter2.add(jr2); panelCenter2.add(jr3); panelCenter2.add(jr4); panelCenter2.add(jr5); panelCenter2.add(jr6); panelCenter2.add(jr7); panelCenter2.add(jr8); jr2.setSelected(true); jr2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { paizi=paizi+" 奔驰"; }}); jr3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { paizi=paizi+" 宝马"; }}); jr4.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { paizi=paizi+" 奥迪"; }}); jr5.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { paizi=paizi+" 路虎"; }}); jr6.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { paizi=paizi+" 捷豹"; }}); jr7.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { paizi=paizi+" 保时捷"; }}); jr8.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { paizi=paizi+" 玛莎拉蒂"; }}); JButton jb = new JButton("提交"); jb.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null, "您所提交的问卷内容: 你喜欢车吗?:"+shifou+" 最喜欢的车牌:"+paizi, "结果",JOptionPane.INFORMATION_MESSAGE); paizi="奔驰"; jr2.setSelected(true); jr3.setSelected(false); jr4.setSelected(false); jr5.setSelected(false); jr6.setSelected(false); jr7.setSelected(false); jr8.setSelected(false); }}); panelCenter.add(panelCenter1); panelCenter.add(panelCenter2); JPanel panelCenter3 = new JPanel(); panelCenter3.add(jb); panelCenter.add(panelCenter3); panelCenter.setVisible(false); // // JPanel Centerpanel = new JPanel(new GridLayout(3,1)); JLabel label11 = new JLabel("请选择你最喜欢的学科"); String[] likes = {"语文","数学","英语","马列毛邓三","体育","计算机"}; JComboBox comboBox = new JComboBox(likes); comboBox.setEditable(false); comboBox.setMaximumRowCount(6); comboBox.setBounds(40, 20, 130, 25); JPanel Centerpanel1 = new JPanel(); Centerpanel1.add(label11); Centerpanel1.add(comboBox); Centerpanel.add(Centerpanel1); // comboBox.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if (e.getSource() == comboBox) { if(comboBox.getSelectedIndex()==0){ xueke="语文"; } if(comboBox.getSelectedIndex()==1){ xueke="数学"; } if(comboBox.getSelectedIndex()==2){ xueke="英语"; } if(comboBox.getSelectedIndex()==3){ xueke="马列毛邓三"; } if(comboBox.getSelectedIndex()==4){ xueke="体育"; } if(comboBox.getSelectedIndex()==5){ xueke="计算机"; }; } }}); // Create a JList that displays the strings in data[] String[] data = {"音乐鉴赏", "婚姻与爱情", "中医学", "陶艺","易学","情商的培养与提高"}; JList dataList = new JList(data); dataList.setSelectedIndex(0); dataList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (e.getSource() == dataList) { if(dataList.getSelectedIndex()==0){ xuanxiu="音乐鉴赏"; } if(dataList.getSelectedIndex()==1){ xuanxiu="婚姻与爱情"; } if(dataList.getSelectedIndex()==2){ xuanxiu="中医学"; } if(dataList.getSelectedIndex()==3){ xuanxiu="陶艺"; } if(dataList.getSelectedIndex()==4){ xuanxiu="易学"; } if(dataList.getSelectedIndex()==5){ xuanxiu="情商的培养与提高"; }; } } }); JScrollPane scrollPane = new JScrollPane(dataList); // Or in two steps: scrollPane.getViewport().setView(dataList); JPanel Centerpanel2 = new JPanel(); JLabel label12 = new JLabel("请选择你最喜欢的选修"); Centerpanel2.add(label12); Centerpanel2.add(dataList); Centerpanel.add(Centerpanel2); JButton jjb = new JButton("提交"); JPanel jp11 = new JPanel(); jp11.add(jjb); jjb.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null, "您所提交的问卷内容: 最喜欢的学科:"+xueke+" 最喜欢的选修:"+xuanxiu, "结果",JOptionPane.INFORMATION_MESSAGE); }}); Centerpanel.add(jp11); Centerpanel.setVisible(false); // frame.pack(); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setBounds(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2, 800, 500); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setVisible(true); // jt.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { int count = jt.getRowForLocation(e.getX(), e.getY()); if (e.getClickCount() == 1) {} if (count != -1) { String strFriendUsername = jt .getLastSelectedPathComponent().toString(); if (e.getClickCount() == 2 && count != 0) { if (jt.isRowSelected(jt.getRowForLocation( e.getX(), e.getY()))) { if(strFriendUsername=="最喜欢的车型"){ panelCenter.setVisible(true); Centerpanel.setVisible(false); contentPane.add(panelCenter, BorderLayout.CENTER); } else{ Centerpanel.setVisible(true); panelCenter.setVisible(false); contentPane.add(Centerpanel, BorderLayout.CENTER); }} } } } }); // } private void makeMenuBar(JFrame frame) { // TODO Auto-generated method stub final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); JMenuBar menubar = new JMenuBar(); frame.setJMenuBar(menubar); // create the File menu JMenu menuFile = new JMenu("File"); menubar.add(menuFile); JMenuItem itemOpen = new JMenuItem("Open"); itemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK)); menuFile.add(itemOpen); JMenuItem itemClose = new JMenuItem("Close"); itemClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK)); menuFile.add(itemClose); menuFile.addSeparator(); JMenuItem itemQuit = new JMenuItem("Quit"); itemQuit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK)); itemQuit.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub System.exit(0); }}); menuFile.add(itemQuit); JMenu help = new JMenu("Help"); menubar.add(help); JMenuItem aboutItem = new JMenuItem("About Research"); aboutItem.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null, "Thanks!", "About Research",JOptionPane.INFORMATION_MESSAGE); }}); help.add(aboutItem); } public static void main(String[] args) { // TODO Auto-generated method stub new gui1(); } }
单选按钮(JRadioButton)、复选框(JCheckBox)、组合框(JComboBox)、列表框(JList)、树形目录(JTree)等的结合,让我想到了做一个问卷调查来一起运用这些功能。问卷调查清晰明了的表达出了每个按钮的用法,在以后的运用中,能够更加熟练的进行运用。希望在下次作业中,能够继续加强旧知识的运用,并且结合新知识的运用来一起完成好下一次作业。
最后附上自拍照一张,希望老师能记住我