Alignment 不能对齐JKabel

Alignment 不能对齐JKabel,alignment,jlabel,Alignment,Jlabel,嗯,我有这个代码: static class things implements ActionListener { public void actionPerformed (ActionEvent e) { JFrame frame4 = new JFrame("More things"); frame4.setVisible(true); frame4.setResizable(true);

嗯,我有这个代码:

  static class things implements ActionListener {        
      public void actionPerformed (ActionEvent e) {     
        JFrame frame4 = new JFrame("More things");
        frame4.setVisible(true);
        frame4.setResizable(true);
        frame4.setSize(1366,730);
        JPanel panel = new JPanel();
        JLabel label = new JLabel("<html>One thing more</html>");
        label.setVerticalAlignment(SwingConstants.BOTTOM);
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(label); 
        frame4.add(panel);

      }
    }
但是当我运行它时,垂直/水平对齐的JLabel没有对齐,为什么?

这是因为LayoutManager。阅读更多关于使用不同

1JPanel默认使用FlowLayout。在的帮助下,设置为“右对齐零部件”

2JFrame默认使用BorderLayout。使用参数BorderLayout.SOUTH将面板添加到其中

3 useframe4.setVisibletrue;在构建GUI的最后,为了避免工件

4使用packmethod,而不是将大小设置为JFrame

JFrame frame4 = new JFrame("More things");
frame4.setResizable(true);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
JLabel label = new JLabel("<html>One thing more</html>");
panel.add(label); 
frame4.add(panel,BorderLayout.SOUTH);
frame4.pack();
frame4.setVisible(true);