by Dinesh Thakur Category: Swing

A strut is a fixed width invisible component that forces a certain amount of space between components. You can create a strut by calling the createHorizontalStrut () or createVerticalStrut () method depending upon what type of strut you want to create. The static method

createHorizontalStrut(int width)

creates a horizontal strut component that places the specified amount of space between the components on either side. The static method

createVerticalStrut(int Height)

creates a vertical strut component that places the specified amount of space between the components above and below it. For example : The following statements adds a vertical strut of 10 pixels between four buttons in a vertical box.

b.add(new JButton("C"));

b.add(Box.createVerticalStrut(10));

b.add(new JButton("C++"));

b.add(Box.createVerticalStrut(10));

b.add(new JButton("Visual Basic"));

b.add(Box createVerticalStrut(10));

b. add (new JButton ("Java"));

 

import javax.swing.*;

import java.awt.*;
class BoxLayoutFrame extends JFrame
{
     JButton [] Buttons;
     BoxLayoutFrame()
     {
       int i;
       Box b = Box.createVerticalBox();
       b.add(new JButton("C"));
       b.add(Box.createVerticalStrut(10));
       b.add(new JButton("C++"));
       b.add(Box.createVerticalStrut(10));
       b.add(new JButton("Visual Basic"));
       b.add(Box.createVerticalStrut(10));
       b.add(new JButton("Java"));
       add(b);
     }
}
   class StrutsJavaExample
   {
        public static void main(String args[])
       {
            BoxLayoutFrame frame = new BoxLayoutFrame();
            frame.setTitle("Struts Java Example");
            frame.setBounds(200,250,200,200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
       }
   }

Struts in Java Swing Example





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles