本文介绍Swing实现MDI风格界面的基础是JInternalFrame和JDesktopPane。以及介绍Swing实现MDI的方法。

JConsole是一个根据JMX的GUI东西,用于衔接正在运转的JVM,它是一个MDI风格的Java桌面使用。Swing完成MDI风格界面的根底是JInternalFrame和JDesktopPane。以下是Swing完成MDI的办法:
  1. importjava.awt.BorderLayout;
  2. importjava.awt.Container;
  3. importjava.awt.Dimension;
  4. importjava.awt.FlowLayout;
  5. importjava.awt.Insets;
  6. importjava.awt.event.ActionEvent;
  7. importjava.awt.event.ActionListener;
  8. importjava.awt.event.ComponentAdapter;
  9. importjava.awt.event.ComponentEvent;
  10. importjava.beans.PropertyVetoException;
  11. importjavax.swing.JButton;
  12. importjavax.swing.JDesktopPane;
  13. importjavax.swing.JFrame;
  14. importjavax.swing.JInternalFrame;
  15. importjavax.swing.JMenuBar;
  16. importjavax.swing.JMenuItem;
  17. importjavax.swing.JPanel;
  18. importjavax.swing.SwingUtilities;
  19. importjavax.swing.plaf.basic.BasicInternalFrameUI;
  20. /**
  21. *MDIFrameisaframeusingJInternalFrametoimplementsMDIasWordonWindows.
  22. *
  23. *@authorCheng
  24. *@version1.0.0forGPFMDItest
  25. */
  26. @SuppressWarnings("serial")
  27. publicclassMDIFrameextendsJFrame{
  28. /**Thedesktoppane.*/
  29. finalJDesktopPanedesktopPane=newJDesktopPane();
  30. /**Theoperationpanewithrestoreandclosebuttons.*/
  31. finalJPanelopPane=newJPanel();
  32. /**
  33. *InstantiatesanewmDIframe.
  34. */
  35. publicMDIFrame(){
  36. setTitle("MDIFrame");
  37. setSize(600,550);
  38. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. finalJMenuBarbar=newJMenuBar();
  40. JMenuItemexit=newJMenuItem("Exit");
  41. exit.addActionListener(newActionListener(){
  42. @Override
  43. publicvoidactionPerformed(ActionEvente){
  44. System.exit(0);
  45. }
  46. });
  47. bar.add(exit);
  48. //operationpanewithtwobutton,setinvisibleatfirst
  49. opPane.setLayout(newFlowLayout(FlowLayout.TRAILING));
  50. JButtonrestore=newJButton("#");
  51. restore.setMargin(newInsets(0,0,0,0));
  52. restore.setPreferredSize(newDimension(15,15));
  53. restore.addActionListener(newActionListener(){
  54. @Override
  55. publicvoidactionPerformed(ActionEvente){
  56. InnerFramei=(InnerFrame)desktopPane.getSelectedFrame();
  57. try{
  58. //noticethismethod,whenJInternalFramesetmaximunfalse
  59. //thisinternalframewillbesetasoldsize
  60. i.setMaximum(false);
  61. }catch(PropertyVetoExceptionex){
  62. ex.printStackTrace();
  63. }
  64. i.showNorthPanel();
  65. opPane.setVisible(false);
  66. }
  67. });
  68. opPane.add(restore);
  69. JButtonclose=newJButton("X");
  70. close.setMargin(newInsets(0,0,0,0));
  71. close.setPreferredSize(newDimension(15,15));
  72. close.addActionListener(newActionListener(){
  73. @Override
  74. publicvoidactionPerformed(ActionEvente){
  75. JInternalFramei=desktopPane.getSelectedFrame();
  76. i.dispose();
  77. opPane.setVisible(false);
  78. }
  79. });
  80. opPane.add(close);
  81. bar.add(opPane);
  82. opPane.setVisible(false);
  83. setJMenuBar(bar);
  84. Containercontent=getContentPane();
  85. content.add(desktopPane,BorderLayout.CENTER);
  86. finalJPanelctrlPane=newJPanel();
  87. JButtonadd=newJButton("add");
  88. add.addActionListener(newActionListener(){
  89. @Override
  90. publicvoidactionPerformed(ActionEvente){
  91. InnerFrameiFrame=newInnerFrame();
  92. iFrame.setVisible(true);
  93. desktopPane.add(iFrame);
  94. }
  95. });
  96. ctrlPane.add(add);
  97. content.add(ctrlPane,BorderLayout.SOUTH);
  98. setVisible(true);
  99. }
  100. /**
  101. *TheClassInnerFrame.
  102. */
  103. classInnerFrameextendsJInternalFrame{
  104. /**Theishidden.*/
  105. booleanisHidden=false;
  106. /**Theoldui.*/
  107. BasicInternalFrameUIoldUi=null;
  108. /**
  109. *Instantiatesanewinnerframe.
  110. */
  111. publicInnerFrame(){
  112. oldUi=(BasicInternalFrameUI)getUI();
  113. setSize(200,200);
  114. maximizable=true;
  115. closable=true;
  116. addComponentListener(newComponentAdapter(){
  117. publicvoidcomponentResized(ComponentEvente){
  118. InnerFrameselectedFrame=(InnerFrame)e.getSource();
  119. if(selectedFrame.isMaximum()){
  120. selectedFrame.hideNorthPanel();
  121. opPane.setVisible(true);
  122. try{
  123. selectedFrame.setMaximum(true);
  124. }catch(PropertyVetoExceptionex){
  125. ex.printStackTrace();
  126. }
  127. }
  128. }
  129. });
  130. }
  131. /**
  132. *Hidenorthpanel.
  133. */
  134. publicvoidhideNorthPanel(){
  135. ((BasicInternalFrameUI)this.getUI()).setNorthPane(null);
  136. this.putClientProperty("JInternalFrame.isPalette",Boolean.TRUE);
  137. isHidden=true;
  138. }
  139. /**
  140. *Shownorthpanel.
  141. */
  142. publicvoidshowNorthPanel(){
  143. this.setUI(oldUi);
  144. this.putClientProperty("JInternalFrame.isPalette",Boolean.FALSE);
  145. isHidden=false;
  146. }
  147. /*(non-Javadoc)
  148. *@seejavax.swing.JInternalFrame#updateUI()
  149. */
  150. publicvoidupdateUI(){
  151. super.updateUI();
  152. if(isHidden){
  153. hideNorthPanel();
  154. }
  155. }
  156. }
  157. /**
  158. *Themainmethod.
  159. *
  160. *@paramargsthearguments
  161. */
  162. publicstaticvoidmain(String[]args){
  163. SwingUtilities.invokeLater(newRunnable(){
  164. @Override
  165. publicvoidrun(){
  166. newMDIFrame();
  167. }
  168. });
  169. }
  170. }

以上是Swing完成MDI的办法,期望我们有用。

浅析Swing完成MDI的办法  Swing实现MDI 第1张

【修改引荐】

  1. 浅谈不论是AWT仍是Swing形式
  2. 全面比较Swing与SWT
  3. Swing程序中调用JavaFX代码
  4. 概述Swing大多数控件
  5. 浅谈Swing中的Look&Feel
转载请说明出处
知优网 » 浅析Swing完成MDI的办法

发表评论

您需要后才能发表评论