我测试了下,把你的panel1.setAlignmentX(JPanel.LEFT_ALIGNMENT);改为
panel1.setLayout(new FlowLayout(FlowLayout.LEFT));这样应该行了。
下面是我的测试代码:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestJPanel {
public TestJPanel() {
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(3,0));
//设置JPanel的对齐方式,暂时发现只有这样才能使JPanel改变对齐方式。
JPanel JP1 = new JPanel();
JP1.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel JP2 = new JPanel();
JP2.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel JP3 = new JPanel();
JP3.setLayout(new FlowLayout(FlowLayout.LEFT));
//测试标签
JLabel JL1 = new JLabel("测试1");
JLabel JL2 = new JLabel(&