D
Duane Evenson
I'm having trouble getting components to center align in a Box or JPanel
with BoxLayout. The problem component seems to be a JTextField. Here is my
code:
// Text.java
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
Test() {
Box box = Box.createVerticalBox();
box.setAlignmentX((float) 0.5);
box.add(new JLabel("Client"));
box.add(new JTextField(40));
box.add(new JButton("Send"));
Container cp = getContentPane();
cp.add(box);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
}
with BoxLayout. The problem component seems to be a JTextField. Here is my
code:
// Text.java
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
Test() {
Box box = Box.createVerticalBox();
box.setAlignmentX((float) 0.5);
box.add(new JLabel("Client"));
box.add(new JTextField(40));
box.add(new JButton("Send"));
Container cp = getContentPane();
cp.add(box);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
}