java怎么实现点击一个按钮打开一个窗口,再点一次显示窗口正在运行,关掉这个窗口后再点又能打开
2017-08-21
展开全部
第一个窗体LoginFrame.java:packagewinRelation;importjava.awt.Dimension;importjava.awt.Rectangle;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JOptionPane;publicclassLoginFrameextendsJFrame{JButtonbutton=newJButton("点击我");classLoginOKActionimplementsActionListener{publicvoidactionPerformed(ActionEvente){JOptionPane.showMessageDialog(null,"将进入另一个窗体!");newMainFrame();setVisible(false);}}publicLoginFrame(){super();this.setResizable(false);this.setSize(newDimension(300,205));this.setTitle("第一个窗体");this.setLayout(null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setLocation(300,200);this.setVisible(true);this.getContentPane().add(button,null);button.setBounds(newRectangle(111,70,78,27));button.addActionListener(newLoginOKAction());//给按钮加事件监听器}publicstaticvoidmain(String[]args){newLoginFrame();}}第二个窗体MainFrame.java:packagewinRelation;importjava.awt.BorderLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JDesktopPane;importjavax.swing.JFrame;importjavax.swing.JInternalFrame;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JToolBar;publicclassMainFrameextendsJFrame{privatestaticfinalJDesktopPaneDESKTOP_PANE=newJDesktopPane();publicMainFrame(){super("这是主窗体");setSize(640,480);//菜单设置JMenuBarmenuBar=newJMenuBar();this.setJMenuBar(menuBar);JMenumenu1=newJMenu("菜单1");JMenumenu101=newJMenu("菜单101");JMenuItemmenu10101=newJMenuItem("菜单10101");JMenuItemmenu102=newJMenuItem("菜单102");menu102.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){addIFame(newInternalFrame());}});menu101.add(menu10101);menu1.add(menu101);menu1.add(menu102);JMenumenu2=newJMenu("菜单2");menuBar.add(menu1);menuBar.add(menu2);this.getContentPane().add(DESKTOP_PANE);this.setVisible(true);}publicstaticvoidaddIFame(JInternalFrameiframe){//添加子窗体的方法DESKTOP_PANE.add(iframe);}publicstaticvoidmain(String[]args){newMainFrame();}}第三个窗体(MainFrame中的窗体)InternalFrame.java:packagewinRelation;importjavax.swing.JButton;importjavax.swing.JInternalFrame;publicclassInternalFrameextendsJInternalFrame{publicInternalFrame(){super();setClosable(true);setIconifiable(true);setTitle("内部窗体");setBounds(50,50,400,300);setVisible(true);}}已经测试通过!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询