本文介绍Swing实现MDI风格界面的基础是JInternalFrame和JDesktopPane。以及介绍Swing实现MDI的方法。
JConsole是一个根据JMX的GUI东西,用于衔接正在运转的JVM,它是一个MDI风格的Java桌面使用。Swing完成MDI风格界面的根底是JInternalFrame和JDesktopPane。以下是Swing完成MDI的办法:
- importjava.awt.BorderLayout;
- importjava.awt.Container;
- importjava.awt.Dimension;
- importjava.awt.FlowLayout;
- importjava.awt.Insets;
- importjava.awt.event.ActionEvent;
- importjava.awt.event.ActionListener;
- importjava.awt.event.ComponentAdapter;
- importjava.awt.event.ComponentEvent;
- importjava.beans.PropertyVetoException;
- importjavax.swing.JButton;
- importjavax.swing.JDesktopPane;
- importjavax.swing.JFrame;
- importjavax.swing.JInternalFrame;
- importjavax.swing.JMenuBar;
- importjavax.swing.JMenuItem;
- importjavax.swing.JPanel;
- importjavax.swing.SwingUtilities;
- importjavax.swing.plaf.basic.BasicInternalFrameUI;
- /**
- *MDIFrameisaframeusingJInternalFrametoimplementsMDIasWordonWindows.
- *
- *@authorCheng
- *@version1.0.0forGPFMDItest
- */
- @SuppressWarnings("serial")
- publicclassMDIFrameextendsJFrame{
- /**Thedesktoppane.*/
- finalJDesktopPanedesktopPane=newJDesktopPane();
- /**Theoperationpanewithrestoreandclosebuttons.*/
- finalJPanelopPane=newJPanel();
- /**
- *InstantiatesanewmDIframe.
- */
- publicMDIFrame(){
- setTitle("MDIFrame");
- setSize(600,550);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- finalJMenuBarbar=newJMenuBar();
- JMenuItemexit=newJMenuItem("Exit");
- exit.addActionListener(newActionListener(){
- @Override
- publicvoidactionPerformed(ActionEvente){
- System.exit(0);
- }
- });
- bar.add(exit);
- //operationpanewithtwobutton,setinvisibleatfirst
- opPane.setLayout(newFlowLayout(FlowLayout.TRAILING));
- JButtonrestore=newJButton("#");
- restore.setMargin(newInsets(0,0,0,0));
- restore.setPreferredSize(newDimension(15,15));
- restore.addActionListener(newActionListener(){
- @Override
- publicvoidactionPerformed(ActionEvente){
- InnerFramei=(InnerFrame)desktopPane.getSelectedFrame();
- try{
- //noticethismethod,whenJInternalFramesetmaximunfalse
- //thisinternalframewillbesetasoldsize
- i.setMaximum(false);
- }catch(PropertyVetoExceptionex){
- ex.printStackTrace();
- }
- i.showNorthPanel();
- opPane.setVisible(false);
- }
- });
- opPane.add(restore);
- JButtonclose=newJButton("X");
- close.setMargin(newInsets(0,0,0,0));
- close.setPreferredSize(newDimension(15,15));
- close.addActionListener(newActionListener(){
- @Override
- publicvoidactionPerformed(ActionEvente){
- JInternalFramei=desktopPane.getSelectedFrame();
- i.dispose();
- opPane.setVisible(false);
- }
- });
- opPane.add(close);
- bar.add(opPane);
- opPane.setVisible(false);
- setJMenuBar(bar);
- Containercontent=getContentPane();
- content.add(desktopPane,BorderLayout.CENTER);
- finalJPanelctrlPane=newJPanel();
- JButtonadd=newJButton("add");
- add.addActionListener(newActionListener(){
- @Override
- publicvoidactionPerformed(ActionEvente){
- InnerFrameiFrame=newInnerFrame();
- iFrame.setVisible(true);
- desktopPane.add(iFrame);
- }
- });
- ctrlPane.add(add);
- content.add(ctrlPane,BorderLayout.SOUTH);
- setVisible(true);
- }
- /**
- *TheClassInnerFrame.
- */
- classInnerFrameextendsJInternalFrame{
- /**Theishidden.*/
- booleanisHidden=false;
- /**Theoldui.*/
- BasicInternalFrameUIoldUi=null;
- /**
- *Instantiatesanewinnerframe.
- */
- publicInnerFrame(){
- oldUi=(BasicInternalFrameUI)getUI();
- setSize(200,200);
- maximizable=true;
- closable=true;
- addComponentListener(newComponentAdapter(){
- publicvoidcomponentResized(ComponentEvente){
- InnerFrameselectedFrame=(InnerFrame)e.getSource();
- if(selectedFrame.isMaximum()){
- selectedFrame.hideNorthPanel();
- opPane.setVisible(true);
- try{
- selectedFrame.setMaximum(true);
- }catch(PropertyVetoExceptionex){
- ex.printStackTrace();
- }
- }
- }
- });
- }
- /**
- *Hidenorthpanel.
- */
- publicvoidhideNorthPanel(){
- ((BasicInternalFrameUI)this.getUI()).setNorthPane(null);
- this.putClientProperty("JInternalFrame.isPalette",Boolean.TRUE);
- isHidden=true;
- }
- /**
- *Shownorthpanel.
- */
- publicvoidshowNorthPanel(){
- this.setUI(oldUi);
- this.putClientProperty("JInternalFrame.isPalette",Boolean.FALSE);
- isHidden=false;
- }
- /*(non-Javadoc)
- *@seejavax.swing.JInternalFrame#updateUI()
- */
- publicvoidupdateUI(){
- super.updateUI();
- if(isHidden){
- hideNorthPanel();
- }
- }
- }
- /**
- *Themainmethod.
- *
- *@paramargsthearguments
- */
- publicstaticvoidmain(String[]args){
- SwingUtilities.invokeLater(newRunnable(){
- @Override
- publicvoidrun(){
- newMDIFrame();
- }
- });
- }
- }
以上是Swing完成MDI的办法,期望我们有用。
【修改引荐】
- 浅谈不论是AWT仍是Swing形式
- 全面比较Swing与SWT
- Swing程序中调用JavaFX代码
- 概述Swing大多数控件
- 浅谈Swing中的Look&Feel
转载请说明出处
知优网 » 浅析Swing完成MDI的办法
知优网 » 浅析Swing完成MDI的办法