Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the special container for Java Swing programming?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about what is a special container for Java Swing programming. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Swing also provides us with many special containers for us to program, JSplitPane (split panel), JTabbedPane (multiple tabs), JLayeredPane (layered containers, which allow components to overlap each other), and two complex containers, JDesktopPane and JInternalFrame, which are mostly for the implementation of MDI (multi-document interface). These containers are not clear in a few words, so I will give examples (most of them are examples in the book, all of which are good. You can't bear to write one by one by yourself. If you still don't understand, please consult more API documents.

Eg:JSplitPane (split panel)

Public class TestSplitPane {Book [] books = new Book [] {new Book ("Struts2 authoritative Guide", new ImageIcon ("ico/struts2.jpg"), "Comprehensive introduction to all aspects of Struts2 knowledge"), new Book ("lightweight J2EE Enterprise Application practice", new ImageIcon ("ico/j2ee.jpg") "introduce the knowledge of Struts, Spring and / nHibernate integrated development"), new Book ("Ajax based on J2EE", new ImageIcon ("ico/ajax.jpg"), "comprehensively introduce all aspects of Ajax/n development on the J2EE platform")} JFrame jf = new JFrame ("Test JSplitPane"); JList bookList = new JList (books); JLabel bookCover = new JLabel (); JTextArea bookDesc = new JTextArea (); public void init () {/ / set * * size bookList.setPreferredSize (new Dimension (150,300)) for three components; bookCover.setPreferredSize (new Dimension (300,150)) BookDesc.setPreferredSize (new Dimension (300,150)); / / add event listeners bookList.addListSelectionListener (new ListSelectionListener () {public void valueChanged (ListSelectionEvent event) {Book book = (Book) bookList.getSelectedValue (); bookCover.setIcon (book.getIco ()) to the drop-down list) BookDesc.setText (book.getDesc ());}}); / / create a vertical split panel, / / put bookCover on top and bookDesc below, and support continuous layout JSplitPane left = new JSplitPane (JSplitPane.VERTICAL_SPLIT, true, bookCover, new JScrollPane (bookDesc)) / / Open the "touch and unfold" feature left.setOneTouchExpandable (true); / / the following code sets the size of the splitter bar. / / left.setDividerSize (50); / / set the split panel to adjust the layout left.resetToPreferredSizes () according to the * size of the included components; / / create a horizontal split panel / / put the left component on the left and put the bookList component on the right 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 (multiple tabs)

Public class TestJTabbedPane {JFrame jf = new JFrame ("test Tab page"); / / the tag for creating a Tab page is placed on the left, and JTabbedPane JTabbedPane tabbedPane = new JTabbedPane (JTabbedPane.LEFT, JTabbedPane.WRAP_TAB_LAYOUT) with line wrap layout strategy; ImageIcon icon = new ImageIcon ("ico/close.gif"); String [] layouts = {"line wrap layout", "scroll bar layout"} String [] positions = {"left", "top", "right", "bottom"}; Map books = new LinkedHashMap (); public void init () {books.put ("ROR Agile Development * practices", "ror.jpg"); books.put ("Struts2 authoritative Guide", "struts2.jpg") Books.put ("Ajax based on J2EE", "ajax.jpg"); books.put ("lightweight J2EE Enterprise applications", "j2ee.jpg"); books.put ("Spring2.0 Treasure Book", "spring.jpg"); String tip = "you can see the cover photo of this book" / / add 5 Tab pages to JTabbedPane with a title, icon and prompt specified, but the components of the Tab page are null for (String bookName: books.keySet ()) {tabbedPane.addTab (bookName, icon, null, tip);} jf.add (tabbedPane, BorderLayout.CENTER) / / add event listener tabbedPane.addChangeListener (new ChangeListener () {public void stateChanged (ChangeEvent event) {/ / if the selected component is still empty if (tabbedPane.getSelectedComponent () = = null) { / / get the selected Tab page int n = tabbedPane.getSelectedIndex () / / load content loadTab (n) for the specified front page;}); / / choose * page by default and load * page content loadTab (0); tabbedPane.setPreferredSize (new Dimension (500,300)) / / add radio buttons JPanel buttonPanel = new JPanel (); ChangeAction action = new ChangeAction (); buttonPanel.add (new ButtonPanel (action, "Select tag layout Policy", layouts); buttonPanel.add (new ButtonPanel (action, "Select tag location", positions)); jf.add (buttonPanel, BorderLayout.SOUTH) Jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); jf.pack (); jf.setVisible (true);} / / load content private void loadTab (int n) {String title = tabbedPane.getTitleAt (n) for the specified tab / / get the corresponding book cover ImageIcon bookImage = new ImageIcon ("ico/" + books.get (title)); tabbedPane.setComponentAt (n, new JLabel (bookImage)); / / change the icon tabbedPane.setIconAt (n, new ImageIcon ("ico/open.gif")) of the tab according to the title of the tab. } / / define the layout strategy for changing the tab, and the listener 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 ();}} / / defines a JPanel class extension class whose objects contain multiple vertically arranged JRadioButton controls / / and the Panel extension class can specify a string as 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); //添加事件监听器 b.addActionListener(action); group.add(b); b.setSelected(i == 0); } } } eg:JLayeredPane(层容器,允许组件互相重叠) public class TestJLayeredPane { JFrame jf = new JFrame("测试JLayeredPane"); JLayeredPane layeredPane = new JLayeredPane(); public void init() { //向layeredPane中添加3个组件 layeredPane.add(new ContentPanel(10 , 20 , "Struts2权威指南" , "ico/struts2.jpg"), JLayeredPane.MODAL_LAYER); layeredPane.add(new ContentPanel(100 , 60 , "RoR敏捷开发***实践", "ico/ror.jpg"), JLayeredPane.DEFAULT_LAYER); layeredPane.add(new ContentPanel(190 , 100 , "轻量级J2EE企业应用实战", "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(); } } //扩展了JPanel类,可以直接创建一个放在指定位置, //且有指定标题、放置指定图标的JPanel对象 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例子由于都是广告,我就不贴给大家了,没图片不影响程序的效果。 ***是JDesktopPane和JInternalFrame来实现MDI public class TestInternalFrame { final int DESKTOP_WIDTH = 480; final int DESKTOP_HEIGHT = 360; final int FRAME_DISTANCE = 30; JFrame jf = new JFrame("MDI界面"); //定义一个虚拟桌面 private MyJDesktopPane desktop = new MyJDesktopPane(); //保存下一个内部窗口的座标点 private int nextFrameX; private int nextFrameY; //定义内部窗口为虚拟桌面的1/2大小 private int width = DESKTOP_WIDTH / 2; private int height = DESKTOP_HEIGHT / 2; //为主窗口定义2个菜单 JMenu fileMenu = new JMenu("文件"); JMenu windowMenu = new JMenu("窗口"); //定义newAction用于创建菜单和工具按钮 Action newAction = new AbstractAction("新建", new ImageIcon("ico/new.png")) { public void actionPerformed(ActionEvent event) { //创建内部窗口 final JInternalFrame iframe = new JInternalFrame("新文档", true, // 可改变大小 true, // 可关闭 true, // 可***化 true); // 可最小化 iframe.add(new JScrollPane(new JTextArea(8, 40))); //将内部窗口添加到虚拟桌面中 desktop.add(iframe); //设置内部窗口的原始位置(内部窗口默认大小是0X0,放在0,0位置) iframe.reshape(nextFrameX, nextFrameY, width, height); //使该窗口可见,并尝试选中它 iframe.show(); //计算下一个内部窗口的位置 nextFrameX += FRAME_DISTANCE; nextFrameY += FRAME_DISTANCE; if (nextFrameX + width >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report