OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / swing / ScrollBarDemo.java
1 /* ScrollBarDemo.java -- An example showing scroll bars in Swing.
2    Copyright (C) 2005, 2006, Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath examples.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 */
21
22
23 package gnu.classpath.examples.swing;
24
25 import java.awt.BorderLayout;
26 import java.awt.GridLayout;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29
30 import javax.swing.JButton;
31 import javax.swing.JComponent;
32 import javax.swing.JFrame;
33 import javax.swing.JPanel;
34 import javax.swing.JScrollBar;
35 import javax.swing.SwingUtilities;
36
37 /**
38  * A simple scroll bar demo showing various scroll bars in different states.
39  */
40 public class ScrollBarDemo 
41   extends JPanel
42   implements ActionListener 
43 {
44
45   /**
46    * Creates a new demo instance.
47    */
48   public ScrollBarDemo() 
49   {
50     super();
51     createContent();
52   }
53   
54   /**
55    * When the demo is run independently, the frame is displayed, so we should
56    * initialise the content panel (including the demo content and a close 
57    * button).  But when the demo is run as part of the Swing activity board,
58    * only the demo content panel is used, the frame itself is never displayed,
59    * so we can avoid this step.
60    */
61   void initFrameContent()
62   {
63     JPanel closePanel = new JPanel();
64     JButton closeButton = new JButton("Close");
65     closeButton.setActionCommand("CLOSE");
66     closeButton.addActionListener(this);
67     closePanel.add(closeButton);
68     add(closePanel, BorderLayout.SOUTH);
69   }
70        
71   /**
72    * Returns a panel with the demo content.  The panel
73    * uses a BorderLayout(), and the BorderLayout.SOUTH area
74    * is empty, to allow callers to add controls to the 
75    * bottom of the panel if they want to (a close button is
76    * added if this demo is being run as a standalone demo).
77    */       
78   private void createContent() 
79   {
80     setLayout(new BorderLayout());
81     JPanel panel = createScrollBarPanel();
82     add(panel);
83   }
84     
85   private JPanel createScrollBarPanel() 
86   {
87     JPanel panel = new JPanel(new BorderLayout());
88     
89     JPanel horizontalPanel = new JPanel();
90         
91     JScrollBar scroll1a = new JScrollBar(JScrollBar.HORIZONTAL);
92     JScrollBar scroll1b = new JScrollBar(JScrollBar.HORIZONTAL);
93     scroll1b.setEnabled(false);
94     JScrollBar scroll1c = new JScrollBar(JScrollBar.HORIZONTAL);
95     scroll1c.putClientProperty("JScrollBar.isFreeStanding", Boolean.FALSE);
96     JScrollBar scroll1d = new JScrollBar(JScrollBar.HORIZONTAL);
97     scroll1d.putClientProperty("JScrollBar.isFreeStanding", Boolean.FALSE);
98     scroll1d.setEnabled(false);
99     horizontalPanel.add(scroll1a);
100     horizontalPanel.add(scroll1b);
101     horizontalPanel.add(scroll1c);
102     horizontalPanel.add(scroll1d);
103         
104     panel.add(horizontalPanel, BorderLayout.NORTH);
105      
106     JPanel verticalPanel = new JPanel();
107     verticalPanel.setLayout(new GridLayout(1, 7));
108         
109     JScrollBar scroll2a = new JScrollBar(JScrollBar.VERTICAL);
110     JScrollBar scroll2b = new JScrollBar(JScrollBar.VERTICAL);
111     scroll2b.setEnabled(false);
112     JScrollBar scroll2c = new JScrollBar(JScrollBar.VERTICAL);
113     scroll2c.putClientProperty("JScrollBar.isFreeStanding", Boolean.FALSE);
114     JScrollBar scroll2d = new JScrollBar(JScrollBar.VERTICAL);
115     scroll2d.setEnabled(false);
116     scroll2d.putClientProperty("JScrollBar.isFreeStanding", Boolean.FALSE);
117       
118     verticalPanel.add(scroll2a);
119     verticalPanel.add(new JPanel());
120     verticalPanel.add(scroll2b);
121     verticalPanel.add(new JPanel());
122     verticalPanel.add(scroll2c);
123     verticalPanel.add(new JPanel());
124     verticalPanel.add(scroll2d);
125         
126     panel.add(verticalPanel, BorderLayout.EAST);
127         
128     JPanel centerPanel = new JPanel(new GridLayout(1, 2));
129     centerPanel.add(new JScrollBar(JScrollBar.HORIZONTAL));
130     centerPanel.add(new JScrollBar(JScrollBar.VERTICAL));
131     panel.add(centerPanel);
132     return panel;        
133   }
134     
135   public void actionPerformed(ActionEvent e) 
136   {
137     if (e.getActionCommand().equals("CLOSE"))
138     {
139       System.exit(0);
140     }
141   }
142
143   public static void main(String[] args) 
144   {
145     SwingUtilities.invokeLater
146     (new Runnable()
147      {
148        public void run()
149        {
150          ScrollBarDemo app = new ScrollBarDemo();
151          app.initFrameContent();
152          JFrame frame = new JFrame("ScrollBar Demo");
153          frame.getContentPane().add(app);
154          frame.pack();
155          frame.setVisible(true);
156        }});
157     }
158
159   /**
160    * Returns a DemoFactory that creates a ScrollBarDemo.
161    *
162    * @return a DemoFactory that creates a ScrollBarDemo
163    */
164   public static DemoFactory createDemoFactory()
165   {
166     return new DemoFactory()
167     {
168       public JComponent createDemo()
169       {
170         return new ScrollBarDemo();
171       }
172     };
173   }
174 }