java对菜单项的监听_JAVA中下拉菜单中的菜单项怎样做监听

importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.border.*;importjava.util.*;importjava.text.SimpleDateFormat;/*ComboBoxDemo2.javarequiresnootherfiles.*/publicclassComboBoxDemo2extendsJPanelimplementsActionListener{staticJFrameframe;JLabelresult;StringcurrentPattern;publicComboBoxDemo2(){setLayout(newBoxLayout(this,BoxLayout.PAGE_AXIS));String[]patternExamples={"ddMMMMMyyyy","dd.MM.yy","MM/dd/yy","yyyy.MM.ddG'at'hh:mm:ssz","EEE,MMMd,''yy","h:mma","H:mm:ss:SSS","K:mma,z","yyyy.MMMMM.ddGGGhh:mmaaa"};currentPattern=patternExamples[0];//SetuptheUIforselectingapattern.JLabelpatternLabel1=newJLabel("Enterthepatternstringor");JLabelpatternLabel2=newJLabel("selectonefromthelist:");JComboBoxpatternList=newJComboBox(patternExamples);patternList.setEditable(true);patternList.addActionListener(this);//CreatetheUIfordisplayingresult.JLabelresultLabel=newJLabel("CurrentDate/Time",JLabel.LEADING);//==LEFTresult=newJLabel("");result.setForeground(Color.black);result.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(5,5,5,5)));//Layouteverything.JPanelpatternPanel=newJPanel();patternPanel.setLayout(newBoxLayout(patternPanel,BoxLayout.PAGE_AXIS));patternPanel.add(patternLabel1);patternPanel.add(patternLabel2);patternList.setAlignmentX(Component.LEFT_ALIGNMENT);patternPanel.add(patternList);JPanelresultPanel=newJPanel(newGridLayout(0,1));resultPanel.add(resultLabel);resultPanel.add(result);patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT);resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);add(patternPanel);add(Box.createRigidArea(newDimension(0,10)));add(resultPanel);setBorder(BorderFactory.createEmptyBorder(10,10,10,10));reformat();}//constructorpublicvoidactionPerformed(ActionEvente){JComboBoxcb=(JComboBox)e.getSource();StringnewSelection=(String)cb.getSelectedItem();currentPattern=newSelection;reformat();}/**Formatsanddisplaystoday'sdate.*/publicvoidreformat(){Datetoday=newDate();SimpleDateFormatformatter=newSimpleDateFormat(currentPattern);try{StringdateString=formatter.format(today);result.setForeground(Color.black);result.setText(dateString);}catch(IllegalArgumentExceptioniae){result.setForeground(Color.red);result.setText("Error:"+iae.getMessage());}}/***CreatetheGUIandshowit.Forthreadsafety,*thismethodshouldbeinvokedfromthe*event-dispatchingthread.*/privatestaticvoidcreateAndShowGUI(){//Createandsetupthewindow.JFrameframe=newJFrame("ComboBoxDemo2");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Createandsetupthecontentpane.JComponentnewContentPane=newComboBoxDemo2();newContentPane.setOpaque(true);//contentpanesmustbeopaqueframe.setContentPane(newContentPane);//Displaythewindow.frame.pack();frame.setVisible(true);}publicstaticvoidmain(String[]args){//Scheduleajobfortheevent-dispatchingthread://creatingandshowingthisapplication'sGUI.javax.swing.SwingUtilities.invokeLater(newRunnable(){publicvoidrun(){createAndShowGUI();}});}}

阅读全文 >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值