|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | import javax.swing.*; import java.awt.*; import java.awt.Color; public class BoxLayoutExample_Answer_3{ public JPanel createContentPane (){ // We create a bottom JPanel to place everything on. // We set the layout of the main JPanel to be BoxLayout. // LINE_AXIS sets them left to right, PAGE_AXIS sets them // from top to bottom. JPanel totalGUI = new JPanel(); totalGUI.setLayout(new BoxLayout(totalGUI, BoxLayout.LINE_AXIS)); JPanel redPanel = new JPanel(); redPanel.setBackground(Color.red); redPanel.setMinimumSize(new Dimension(40, 40)); redPanel.setMaximumSize(new Dimension(40, 40)); redPanel.setPreferredSize(new Dimension(40, 40)); redPanel.setAlignmentY(Component.TOP_ALIGNMENT); totalGUI.add(redPanel); // This is the first spacer. This creates a spacer 10px wide that // will never get bigger or smaller. totalGUI.add(Box.createRigidArea(new Dimension(10,0))); JPanel yellowPanel = new JPanel(); yellowPanel.setBackground(Color.yellow); yellowPanel.setMinimumSize(new Dimension(60, 60)); yellowPanel.setMaximumSize(new Dimension(60, 60)); yellowPanel.setPreferredSize(new Dimension(60, 60)); yellowPanel.setAlignmentY(Component.TOP_ALIGNMENT); totalGUI.add(yellowPanel); // This spacer takes any spare space and places it as part of the spacer // If you drag the window wider, the space will get wider. totalGUI.add(Box.createHorizontalGlue()); JPanel greenPanel = new JPanel(); greenPanel.setBackground(Color.green); greenPanel.setMinimumSize(new Dimension(80, 80)); greenPanel.setMaximumSize(new Dimension(80, 80)); greenPanel.setPreferredSize(new Dimension(80, 80)); greenPanel.setAlignmentY(Component.BOTTOM_ALIGNMENT); totalGUI.add(greenPanel); // This spacer is a custom spacer. // The minimum size acts like a rigid area that // will not get any smaller than 10 pixels on the x-axis (horizontal) // and not get any smaller than 50 pixels on the y axis (vertical). // The way the maximum size is set up means the spacer acts like glue // and will expand to fit the available space. Dimension minSize = new Dimension(10, 50); Dimension prefSize = new Dimension(10, 50); Dimension maxSize = new Dimension(Short.MAX_VALUE, 50); totalGUI.add(new Box.Filler(minSize, prefSize, maxSize)); JPanel bluePanel = new JPanel(); bluePanel.setBackground(Color.blue); bluePanel.setMinimumSize(new Dimension(100, 100)); bluePanel.setMaximumSize(new Dimension(100, 100)); bluePanel.setPreferredSize(new Dimension(100, 100)); bluePanel.setAlignmentY(Component.CENTER_ALIGNMENT); totalGUI.add(bluePanel); totalGUI.setOpaque(true); return totalGUI; } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[=] BoxLayout Exercises [=]"); BoxLayoutExample_Answer_3 demo = new BoxLayoutExample_Answer_3(); frame.setContentPane(demo.createContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } // ************************************************************* // Name : BoxLayoutExample_Answer_3.java // Author : Stuart Davidson // Date : 23/12/2006 // Description : This GUI shows the position of the boxes in correlation to // the picture exercise 3. // ************************************************************* |