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");
}
}
Similar Posts
Machine Learning, Big Data, Data Science, Analytics, Cloud, Security, AI, Robotics, Database, BI, Development: Software, Web, Mobile