Scala.use(better,Swing); TabbedPaneDemo

《前の記事|記事一覧|次の記事》
Scala.use(better,Swing)


Using Swing Components

TabbedPaneDemo

Java Tutorials Sample Code にリンク情報を付加して、索引として再構成しました

《参考記事》


《関連記事》

-

Java 版 (original)

  1: /*
  2:  * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
   :  ...
 30:  */ 
 31: 
 32: package components;
 33: 
 34: /*
 35:  * TabbedPaneDemo.java requires one additional file:
 36:  *   images/middle.gif.
 37:  */
 38: 
 39: import javax.swing.JTabbedPane;
 40: import javax.swing.ImageIcon;
 41: import javax.swing.JLabel;
 42: import javax.swing.JPanel;
 43: import javax.swing.JFrame;
 44: import javax.swing.JComponent;
 45: import javax.swing.SwingUtilities;
 46: import javax.swing.UIManager;
 47: import java.awt.BorderLayout;
 48: import java.awt.Dimension;
 49: import java.awt.GridLayout;
 50: import java.awt.event.KeyEvent;
 51: 
 52: public class TabbedPaneDemo extends JPanel {
 53:     public TabbedPaneDemo() {
 54:         super(new GridLayout(1, 1));
 55:         
 56:         JTabbedPane tabbedPane = new JTabbedPane();
 57:         ImageIcon icon = createImageIcon("images/middle.gif");
 58:         
 59:         JComponent panel1 = makeTextPanel("Panel #1");
 60:         tabbedPane.addTab("Tab 1", icon, panel1,
 61:                 "Does nothing");
 62:         tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
 63:         
 64:         JComponent panel2 = makeTextPanel("Panel #2");
 65:         tabbedPane.addTab("Tab 2", icon, panel2,
 66:                 "Does twice as much nothing");
 67:         tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
 68:         
 69:         JComponent panel3 = makeTextPanel("Panel #3");
 70:         tabbedPane.addTab("Tab 3", icon, panel3,
 71:                 "Still does nothing");
 72:         tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
 73:         
 74:         JComponent panel4 = makeTextPanel(
 75:                 "Panel #4 (has a preferred size of 410 x 50).");
 76:         panel4.setPreferredSize(new Dimension(410, 50));
 77:         tabbedPane.addTab("Tab 4", icon, panel4,
 78:                 "Does nothing at all");
 79:         tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
 80:         
 81:         //Add the tabbed pane to this panel.
 82:         add(tabbedPane);
 83:         
 84:         //The following line enables to use scrolling tabs.
 85:         tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
 86:     }
 87:     
 88:     protected JComponent makeTextPanel(String text) {
 89:         JPanel panel = new JPanel(false);
 90:         JLabel filler = new JLabel(text);
 91:         filler.setHorizontalAlignment(JLabel.CENTER);
 92:         panel.setLayout(new GridLayout(1, 1));
 93:         panel.add(filler);
 94:         return panel;
 95:     }
 96:     
 97:     /** Returns an ImageIcon, or null if the path was invalid. */
 98:     protected static ImageIcon createImageIcon(String path) {
 99:         java.net.URL imgURL = TabbedPaneDemo.class.getResource(path);
100:         if (imgURL != null) {
101:             return new ImageIcon(imgURL);
102:         } else {
103:             System.err.println("Couldn't find file: " + path);
104:             return null;
105:         }
106:     }
107:     
108:     /**
109:      * Create the GUI and show it.  For thread safety,
110:      * this method should be invoked from
111:      * the event dispatch thread.
112:      */
113:     private static void createAndShowGUI() {
114:         //Create and set up the window.
115:         JFrame frame = new JFrame("TabbedPaneDemo");
116:         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
117:         
118:         //Add content to the window.
119:         frame.add(new TabbedPaneDemo(), BorderLayout.CENTER);
120:         
121:         //Display the window.
122:         frame.pack();
123:         frame.setVisible(true);
124:     }
125:     
126:     public static void main(String[] args) {
127:         //Schedule a job for the event dispatch thread:
128:         //creating and showing this application's GUI.
129:         SwingUtilities.invokeLater(new Runnable() {
130:             public void run() {
131:                 //Turn off metal's use of bold fonts
132: 		UIManager.put("swing.boldMetal", Boolean.FALSE);
133: 		createAndShowGUI();
134:             }
135:         });
136:     }
137: }

 ↑ TOP

update*13/03/08 12:59:32