溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java Swing編程的特殊容器是什么

發(fā)布時(shí)間:2021-10-29 10:15:18 來(lái)源:億速云 閱讀:112 作者:柒染 欄目:編程語(yǔ)言

今天就跟大家聊聊有關(guān)Java Swing編程的特殊容器是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Swing還提供我們?cè)S多特殊容器方便我們編程,JSplitPane(分割面板),JTabbedPane(多選項(xiàng)卡),JLayeredPane(層容器,允許組件互相重疊),講兩個(gè)復(fù)雜的容器JDesktopPane和JInternalFrame這些多是為了實(shí)現(xiàn)MDI(多文檔界面),這些容器不是三言兩語(yǔ)能說(shuō)清楚的,所以我將以舉例的方式(其中多是書中的例子,舉的都不錯(cuò),自己一個(gè)一個(gè)寫可吃不消),如還有不懂的,請(qǐng)多查閱API文檔。

eg:JSplitPane(分割面板)

public class TestSplitPane  {      Book[] books = new Book[]{          new Book("Struts2權(quán)威指南" , new ImageIcon("ico/struts2.jpg") ,               "全面介紹Struts2的各方/n面知識(shí)"),          new Book("輕量級(jí)J2EE企業(yè)應(yīng)用實(shí)戰(zhàn)" , new ImageIcon("ico/j2ee.jpg") ,              "介紹Struts、Spring和/nHibernate整合開(kāi)發(fā)的知識(shí)"),          new Book("基于J2EE的Ajax寶典" , new ImageIcon("ico/ajax.jpg") ,               "全面介紹J2EE平臺(tái)上Ajax/n開(kāi)發(fā)的各方面知識(shí)")      };      JFrame jf = new JFrame("測(cè)試JSplitPane");      JList bookList = new JList(books);      JLabel bookCover = new JLabel();      JTextArea bookDesc = new JTextArea();       public void init()      {          //為三個(gè)組件設(shè)置***大小          bookList.setPreferredSize(new Dimension(150, 300));          bookCover.setPreferredSize(new Dimension(300, 150));          bookDesc.setPreferredSize(new Dimension(300, 150));          //為下拉列表添加事件監(jiān)聽(tīng)器          bookList.addListSelectionListener(new ListSelectionListener()          {              public void valueChanged(ListSelectionEvent event)              {                    Book book = (Book)bookList.getSelectedValue();                  bookCover.setIcon(book.getIco());                  bookDesc.setText(book.getDesc());              }           });          //創(chuàng)建一個(gè)垂直的分割面板,          //將bookCover放在上面,將bookDesc放在下面 , 支持連續(xù)布局          JSplitPane left = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true ,               bookCover, new JScrollPane(bookDesc));          //打開(kāi)“一觸即展”的特性          left.setOneTouchExpandable(true);          //下面代碼設(shè)置分割條的大小。          //left.setDividerSize(50);           //設(shè)置該分割面板根據(jù)所包含組件的***大小來(lái)調(diào)整布局           left.resetToPreferredSizes();          //創(chuàng)建一個(gè)水平的分割面板          //將left組件放在左邊,將bookList組件放在右邊          JSplitPane content = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,              left, bookList);          jf.add(content);          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          jf.pack();          jf.setVisible(true);      }      public static void main(String[] args)      {            new TestSplitPane().init();      }  }  class Book  {       private String name;      private Icon ico;      private String desc;       public Book(){}       public Book(String name , Icon ico , String desc)      {          this.name = name;          this.ico = ico;          this.desc = desc;      }       public void setName(String name)      {          this.name = name;      }      public String getName()      {           return this.name;      }       public void setIco(Icon ico)      {          this.ico = ico;      }      public Icon getIco()      {           return this.ico;      }       public void setDesc(String desc)      {          this.desc = desc;      }      public String getDesc()      {           return this.desc;      }      public String toString()      {          return name;      }  }

eg:JTabbedPane(多選項(xiàng)卡)

public class TestJTabbedPane  {      JFrame jf = new JFrame("測(cè)試Tab頁(yè)面");      //創(chuàng)建一個(gè)Tab頁(yè)面的標(biāo)簽放在左邊,采用換行布局策略的JTabbedPane      JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT , JTabbedPane.WRAP_TAB_LAYOUT);         ImageIcon icon = new ImageIcon("ico/close.gif");      String[] layouts = {"換行布局" , "滾動(dòng)條布局"};      String[] positions = {"左邊" , "頂部" , "右邊" , "底部"};      Map<String , String> books = new LinkedHashMap<String , String>();       public void init()      {          books.put("ROR敏捷開(kāi)發(fā)***實(shí)踐" , "ror.jpg");          books.put("Struts2權(quán)威指南" , "struts2.jpg");          books.put("基于J2EE的Ajax寶典" , "ajax.jpg");          books.put("輕量級(jí)J2EE企業(yè)應(yīng)用實(shí)戰(zhàn)" , "j2ee.jpg");          books.put("Spring2.0寶典" , "spring.jpg");          String tip = "可看到本書的封面照片";          //向JTabbedPane中添加5個(gè)Tab頁(yè)面,指定了標(biāo)題、圖標(biāo)和提示,但該Tab頁(yè)面的組件為null          for (String bookName : books.keySet())          {              tabbedPane.addTab(bookName, icon, null , tip);          }          jf.add(tabbedPane, BorderLayout.CENTER);          //為JTabbedPane添加事件監(jiān)聽(tīng)器          tabbedPane.addChangeListener(new ChangeListener()          {              public void stateChanged(ChangeEvent event)              {                         //如果被選擇的組件依然是空                  if (tabbedPane.getSelectedComponent() == null)                  {                      //獲取所選Tab頁(yè)                      int n = tabbedPane.getSelectedIndex();                      //為指定標(biāo)前頁(yè)加載內(nèi)容                      loadTab(n);                  }              }          });          //系統(tǒng)默認(rèn)選擇***頁(yè),加載***頁(yè)內(nèi)容          loadTab(0);          tabbedPane.setPreferredSize(new Dimension(500 , 300));          //增加控制標(biāo)簽布局、標(biāo)簽位置的單選按鈕          JPanel buttonPanel = new JPanel();          ChangeAction action = new ChangeAction();          buttonPanel.add(new ButtonPanel(action , "選擇標(biāo)簽布局策略" ,layouts));          buttonPanel.add (new ButtonPanel(action , "選擇標(biāo)簽位置" ,positions));          jf.add(buttonPanel, BorderLayout.SOUTH);          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          jf.pack();          jf.setVisible(true);       }      //為指定標(biāo)簽頁(yè)加載內(nèi)容      private void loadTab(int n)      {          String title = tabbedPane.getTitleAt(n);          //根據(jù)標(biāo)簽頁(yè)的標(biāo)題獲取對(duì)應(yīng)圖書封面          ImageIcon bookImage = new ImageIcon("ico/" + books.get(title));          tabbedPane.setComponentAt(n, new JLabel(bookImage));          //改變標(biāo)簽頁(yè)的圖標(biāo)          tabbedPane.setIconAt(n, new ImageIcon("ico/open.gif"));      }      //定義改變標(biāo)簽頁(yè)的布局策略,放置位置的監(jiān)聽(tīng)器      class ChangeAction implements ActionListener      {          public void actionPerformed(ActionEvent event)          {              JRadioButton source = (JRadioButton)event.getSource();              String selection = source.getActionCommand();              if (selection.equals(layouts[0]))              {                  tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);              }              else if (selection.equals(layouts[1]))              {                  tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);              }              else if (selection.equals(positions[0]))              {                  tabbedPane.setTabPlacement(JTabbedPane.LEFT);              }              else if (selection.equals(positions[1]))              {                  tabbedPane.setTabPlacement(JTabbedPane.TOP);              }              else if (selection.equals(positions[2]))              {                  tabbedPane.setTabPlacement(JTabbedPane.RIGHT);              }              else if (selection.equals(positions[3]))              {                  tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);              }          }      }       public static void main(String[] args)      {            new TestJTabbedPane().init();      }  }   //定義一個(gè)JPanel類擴(kuò)展類,該類的對(duì)象包含多個(gè)縱向排列的JRadioButton控件  //且Panel擴(kuò)展類可以指定一個(gè)字符串作為TitledBorder  class ButtonPanel extends JPanel  {        private ButtonGroup group;      public ButtonPanel(TestJTabbedPane.ChangeAction action , String title, String[] labels)      {            setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));          setLayout(new BoxLayout(this, BoxLayout.X_AXIS));          group = new ButtonGroup();          for (int i = 0; labels!= null && i < labels.length; i++)          {                JRadioButton b = new JRadioButton(labels[i]);              b.setActionCommand(labels[i]);              add(b);              //添加事件監(jiān)聽(tīng)器              b.addActionListener(action);              group.add(b);              b.setSelected(i == 0);          }      }  }

eg:JLayeredPane(層容器,允許組件互相重疊)

public class TestJLayeredPane  {      JFrame jf = new JFrame("測(cè)試JLayeredPane");      JLayeredPane layeredPane = new JLayeredPane();      public void init()      {          //向layeredPane中添加3個(gè)組件          layeredPane.add(new ContentPanel(10 , 20 , "Struts2權(quán)威指南" ,               "ico/struts2.jpg"), JLayeredPane.MODAL_LAYER);          layeredPane.add(new ContentPanel(100 , 60 , "RoR敏捷開(kāi)發(fā)***實(shí)踐",               "ico/ror.jpg"), JLayeredPane.DEFAULT_LAYER);          layeredPane.add(new ContentPanel(190 , 100 , "輕量級(jí)J2EE企業(yè)應(yīng)用實(shí)戰(zhàn)",               "ico/j2ee.jpg"), 4);          layeredPane.setPreferredSize(new Dimension(400, 300));          layeredPane.setVisible(true);          jf.add(layeredPane);          jf.pack();          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          jf.setVisible(true);      }      public static void main(String[] args)       {          new TestJLayeredPane().init();      }  }  //擴(kuò)展了JPanel類,可以直接創(chuàng)建一個(gè)放在指定位置,  //且有指定標(biāo)題、放置指定圖標(biāo)的JPanel對(duì)象  class ContentPanel extends JPanel  {      public ContentPanel(int xPos , int yPos , String title , String ico)      {          setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));          JLabel label = new JLabel(new ImageIcon(ico));          add(label);          setBounds(xPos , yPos , 160, 200);      }  }

以上3例子由于都是廣告,我就不貼給大家了,沒(méi)圖片不影響程序的效果。

***是JDesktopPane和JInternalFrame來(lái)實(shí)現(xiàn)MDI

public class TestInternalFrame  {      final int DESKTOP_WIDTH = 480;      final int DESKTOP_HEIGHT = 360;      final int FRAME_DISTANCE = 30;       JFrame jf = new JFrame("MDI界面");      //定義一個(gè)虛擬桌面      private MyJDesktopPane desktop = new MyJDesktopPane();      //保存下一個(gè)內(nèi)部窗口的座標(biāo)點(diǎn)      private int nextFrameX;      private int nextFrameY;      //定義內(nèi)部窗口為虛擬桌面的1/2大小      private int width = DESKTOP_WIDTH / 2;      private int height = DESKTOP_HEIGHT / 2;      //為主窗口定義2個(gè)菜單      JMenu fileMenu = new JMenu("文件");      JMenu windowMenu = new JMenu("窗口");      //定義newAction用于創(chuàng)建菜單和工具按鈕      Action newAction = new AbstractAction("新建", new ImageIcon("ico/new.png"))      {          public void actionPerformed(ActionEvent event)          {              //創(chuàng)建內(nèi)部窗口              final JInternalFrame iframe = new JInternalFrame("新文檔",                  true, // 可改變大小                  true, // 可關(guān)閉                  true, // 可***化                  true); // 可最小化              iframe.add(new JScrollPane(new JTextArea(8, 40)));              //將內(nèi)部窗口添加到虛擬桌面中              desktop.add(iframe);              //設(shè)置內(nèi)部窗口的原始位置(內(nèi)部窗口默認(rèn)大小是0X0,放在0,0位置)              iframe.reshape(nextFrameX, nextFrameY, width, height);              //使該窗口可見(jiàn),并嘗試選中它              iframe.show();              //計(jì)算下一個(gè)內(nèi)部窗口的位置              nextFrameX += FRAME_DISTANCE;              nextFrameY += FRAME_DISTANCE;              if (nextFrameX + width > desktop.getWidth()) nextFrameX = 0;              if (nextFrameY + height > desktop.getHeight()) nextFrameY = 0;          }      };      //定義exitAction用于創(chuàng)建菜單和工具按鈕      Action exitAction = new AbstractAction("退出", new ImageIcon("ico/exit.png"))      {          public void actionPerformed(ActionEvent event)          {              System.exit(0);          }      };       public void init()      {          //為窗口安裝菜單條和工具條          JMenuBar menuBar = new JMenuBar();          JToolBar toolBar = new JToolBar();          jf.setJMenuBar(menuBar);          menuBar.add(fileMenu);          fileMenu.add(newAction);          fileMenu.add(exitAction);          toolBar.add(newAction);          toolBar.add(exitAction);          menuBar.add(windowMenu);          JMenuItem nextItem = new JMenuItem("下一個(gè)");          nextItem.addActionListener(new ActionListener()          {              public void actionPerformed(ActionEvent event)              {                  desktop.selectNextWindow();              }          });          windowMenu.add(nextItem);          JMenuItem cascadeItem = new JMenuItem("級(jí)聯(lián)");          cascadeItem.addActionListener(new ActionListener()          {              public void actionPerformed(ActionEvent event)              {                  //級(jí)聯(lián)顯示窗口,內(nèi)部窗口的大小是外部窗口的0.75                  desktop.cascadeWindows(FRAME_DISTANCE , 0.75);              }          });          windowMenu.add(cascadeItem);          JMenuItem tileItem = new JMenuItem("平鋪");          tileItem.addActionListener(new ActionListener()          {              public void actionPerformed(ActionEvent event)              {                  //平鋪顯示所有內(nèi)部窗口                  desktop.tileWindows();              }          });          windowMenu.add(tileItem);          final JCheckBoxMenuItem dragOutlineItem = new JCheckBoxMenuItem("僅顯示拖動(dòng)窗口的輪廓");          dragOutlineItem.addActionListener(new ActionListener()          {              public void actionPerformed(ActionEvent event)              {                  //根據(jù)該菜單項(xiàng)是否選擇來(lái)決定采用哪種拖動(dòng)模式                  desktop.setDragMode(dragOutlineItem.isSelected()                       ? JDesktopPane.OUTLINE_DRAG_MODE                      : JDesktopPane.LIVE_DRAG_MODE);              }          });          windowMenu.add(dragOutlineItem);          desktop.setPreferredSize(new Dimension(480, 360));          //將虛擬桌面添加到***JFrame容器中          jf.add(desktop);          jf.add(toolBar , BorderLayout.NORTH);          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          jf.pack();          jf.setVisible(true);      }       public static void main(String[] args)      {            new TestInternalFrame().init();      }  }  class MyJDesktopPane extends JDesktopPane  {      //將所有窗口以級(jí)聯(lián)方式顯示,      //其中offset是兩個(gè)窗口的位移距離,scale是內(nèi)部窗口與JDesktopPane的大小比例      public void cascadeWindows(int offset , double scale)      {          //定義級(jí)聯(lián)顯示窗口時(shí)內(nèi)部窗口的大小          int width = (int)(getWidth() * scale);          int height = (int)(getHeight() * scale);          //用于保存級(jí)聯(lián)窗口時(shí)每個(gè)窗口的位置          int x = 0;          int y = 0;          for (JInternalFrame frame : getAllFrames())          {                try             {                    //取消內(nèi)部窗口的***化,最小化                  frame.setMaximum(false);                  frame.setIcon(false);                  //把窗口重新放置在指定位置                  frame.reshape(x, y, width, height);                  x += offset;                  y += offset;                  //如果到了虛擬桌面邊界                  if (x + width > getWidth()) x = 0;                  if (y + height > getHeight()) y = 0;              }              catch (PropertyVetoException e)              {}          }      }      //將所有窗口以平鋪方式顯示      public void tileWindows()      {            //統(tǒng)計(jì)所有窗口          int frameCount = 0;          for (JInternalFrame frame : getAllFrames())          {              frameCount++;          }          //計(jì)算需要多少行、多少列才可以平鋪所有窗口          int rows = (int) Math.sqrt(frameCount);          int cols = frameCount / rows;          //需要額外增加到其他列中的窗口          int extra = frameCount % rows;          //計(jì)算平鋪時(shí)內(nèi)部窗口的大小          int width = getWidth() / cols;          int height = getHeight() / rows;          //用于保存平鋪窗口時(shí)每個(gè)窗口在橫向、縱向上的索引          int x = 0;          int y = 0;          for (JInternalFrame frame : getAllFrames())          {                try             {                  //取消內(nèi)部窗口的***化,最小化                  frame.setMaximum(false);                  frame.setIcon(false);                  //將窗口放在指定位置                  frame.reshape(x * width, y * height, width, height);                  y++;                  //每排完一列窗口                  if (y == rows)                  {                      //開(kāi)始排放下一列窗口                      y = 0;                      x++;                      //如果額外多出的窗口與剩下的列數(shù)相等,則后面所有列都需要多排列一個(gè)窗口                      if (extra == cols - x)                      {                          rows++;                          height = getHeight() / rows;                      }                  }              }              catch (PropertyVetoException e)              {}          }      }      //選中下一個(gè)非圖標(biāo)窗口      public void selectNextWindow()      {            JInternalFrame[] frames = getAllFrames();          for (int i = 0; i < frames.length; i++)          {                if (frames[i].isSelected())              {                    // 找出下一個(gè)非最小化的窗口,嘗試選中它,                  //如果選中失敗,則繼續(xù)嘗試選中下一個(gè)窗口                  int next = (i + 1) % frames.length;                  while (next != i)                  {                      //如果該窗口不是處于最小化狀態(tài)                      if (!frames[next].isIcon())                      {                          try                         {                                frames[next].setSelected(true);                              frames[next].toFront();                              frames[i].toBack();                              return;                          }                          catch (PropertyVetoException e)                          {}                      }                      next = (next + 1) % frames.length;                  }              }          }      }  }

大家注意看繼承JDesktopPane的類MyJDesktopPane

其中的3個(gè)方法非常有用,這是對(duì)窗口控制的基本方法(級(jí)聯(lián),平鋪,選下個(gè)窗口),大家可以保留下來(lái),以備后用。

看完上述內(nèi)容,你們對(duì)Java Swing編程的特殊容器是什么有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI