标签未与中心布局的左侧对齐。如果GridLayout
不存在,则它正确移动。有没有办法将 JLabel 移动到最左边?
我试过 setHorizontalAlignment 和 setAlignmentX 都没有工作
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class asd {
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel lab = new JLabel("LABEL",SwingConstants.LEFT);
//lab.setHorizontalAlignment(SwingConstants.CENTER);
GridLayout grid = new GridLayout(3,3,2,1);
JPanel yourGrid = new JPanel(grid);
panel.add(lab);
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
JButton but = new JButton();
yourGrid.add(but);
}
}
panel.add(yourGrid);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setVisible(true);
frame.pack();
}
}