
Technology Fundamentals
April's Program Challenge
New to Java Programming Center
Java Platform Overview |
Getting Started |
Step-by-Step Programming
Learning Paths |
References & Resources |
Certification |
Supplements
For April's challenge, create a three panel tabbed application, and each panel has the following characteristics:
- Panel 1
Create a panel that contains a label and a text field.
Make the text field as wide as the panel, but limit the height
to the text field's preferred height.
- Panel 2
Create a set of components in a single column.
Position the first component at the top of the screen. Put the
second in the middle, and the third at the bottom. Set each
component at its preferred height.
- Panel 3
Create a 3 x 3 grid of components, positioning components only
along one of the diagonals. At each position along the diagonal,
put two buttons at their preferred size.
Keep in mind that the characteristics described here need to
hold true if the frame is resized.
A possible solution to the April 2002
Java Technology Fundamentals Program Challenge, LayoutChallenge:
import java.awt.*;
import javax.swing.*;
public class LayoutChallenge {
public static void main(String args[]) {
JFrame frame = new JFrame("Challenge");
// Setup the frame to exit when it closes
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get reference to inner content pane
Container contentPane = frame.getContentPane();
// Create JTabbedPane
JTabbedPane jTabbedPane = new JTabbedPane();
// Place JTabbedPane in content pane
contentPane.add(jTabbedPane, BorderLayout.CENTER);
// Create panel for first tab
JPanel panel1 = new JPanel(new BorderLayout());
// Create inner panel to setup width
JPanel inner1 = new JPanel(new BorderLayout());
// Create label
JLabel jLabel1 = new JLabel("Label: ");
// Create text field
JTextField jTextField1 = new JTextField();
// Add label to west
inner1.add(jLabel1, BorderLayout.WEST);
// Add text field to center
// This ensures component is as wide as possible
inner1.add(jTextField1, BorderLayout.CENTER);
// Add inner panel to north to control height
panel1.add(inner1, BorderLayout.NORTH);
// Add panel to JTabbedPane
jTabbedPane.add(panel1, "Border");
// Create vertical box for second tab
Box box = Box.createVerticalBox();
// Create component for top
box.add(new JButton("Top"));
// Place glue between components
box.add(Box.createVerticalGlue());
// Create component for middle
box.add(new JButton("Middle"));
// Place glue between components
box.add(Box.createVerticalGlue());
// Create component for bottom
box.add(new JButton("Bottom"));
// Add box to JTabbedPane
jTabbedPane.add(box, "Box");
// Create panel for third tab
JPanel panel3 = new JPanel(new GridLayout(3, 3));
// Create inner panels for diagonal component sets
JPanel inner31 = new JPanel();
JPanel inner32 = new JPanel();
JPanel inner33 = new JPanel();
// Row One
// Create and add components for top-left
inner31.add(new JButton("One"));
inner31.add(new JButton("Two"));
// Add first component set
panel3.add(inner31);
// Add filler component
panel3.add(new JPanel());
// Add filler component
panel3.add(new JPanel());
// Row Two
// Create and add components for middle
inner32.add(new JButton("Three"));
inner32.add(new JButton("Four"));
// Add filler component
panel3.add(new JPanel());
// Add second component set
panel3.add(inner32);
// Add filler component
panel3.add(new JPanel());
// Row Three
// Create and add components for bottom-right
inner33.add(new JButton("Five"));
inner33.add(new JButton("Six"));
// Add filler component
panel3.add(new JPanel());
// Add filler component
panel3.add(new JPanel());
// Add third component set
panel3.add(inner33);
// Add panel to JTabbedPane
jTabbedPane.add(panel3, "Grid");
// Set frame size
frame.setSize(250, 250);
// Show frame
frame.show();
}
}
|
When the LayoutChallenge application is run, you see the following:

Label and Text Field

Box Panel

GridLayout Panel
Have a question about programming? Use Java Online
Support.
|
|