A textfield and three buttons arranged by a verticle BoxLayout #Programming Code Examples #Java/J2EE/J2ME #Layout Managers

BoxLayoutTest.java A textfield and three buttons arranged by a verticle BoxLayout. Uses WindowUtilities.java and ExitListener.java. 
##################
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** An example of BoxLayout.
 *
 ***********

public class BoxLayoutTest extends JPanel
                           implements ActionListener{
  BoxLayout layout;
  JButton topButton, middleButton, bottomButton;

  public BoxLayoutTest() {
    layout = new BoxLayout(this, BoxLayout.Y_AXIS);
    setLayout(layout);

    JLabel label = new JLabel("BoxLayout Demo");

    topButton = new JButton("Left Alignment");
    middleButton = new JButton("Center Alignment");
    bottomButton = new JButton("Right Alignment");
    topButton.addActionListener(this);
    middleButton.addActionListener(this);
    bottomButton.addActionListener(this);

    add(label);
    add(topButton);
    add(middleButton);
    add(bottomButton);
    setBackground(Color.white);
  }

  public void actionPerformed(ActionEvent event) {
    if (event.getSource() == topButton) {
      refresh(Component.LEFT_ALIGNMENT);
    } else if (event.getSource() == middleButton) {
      refresh(Component.CENTER_ALIGNMENT);
    } else if (event.getSource() == bottomButton) {
      refresh(Component.RIGHT_ALIGNMENT);
    }
  }

  private void refresh(float alignment){
    topButton.setAlignmentX(alignment);
    middleButton.setAlignmentX(alignment);
    bottomButton.setAlignmentX(alignment);
    revalidate();
    System.out.println("x: "+layout.getLayoutAlignmentX(this));
  }

  public static void main(String[] args) {
    WindowUtilities.setNativeLookAndFeel();
    WindowUtilities.openInJFrame(new BoxLayoutTest(), 300, 135,
                                 "BoxLayoutTest");
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

From: http://sitestree.com/?p=10350
Categories:Programming Code Examples, Java/J2EE/J2ME, Layout Managers
Tags:Java/J2EE/J2MELayout Managers
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada