简体   繁体   English

如何在Java Swing中对齐组件?

[英]How do I align components in Java Swing?

I'm building a simple beginner app in Java and I need your help with aligning components. 我正在用Java构建一个简单的初学者应用程序,我需要你的帮助来调整组件。 What I'm trying to do is to align component(JLabel "name") to the left side of the panel. 我要做的是将组件(JLabel“名称”)对齐到面板的左侧。 I've already tried with "new FlowLayout(FlowLayout.LEFT)" but it didn't work so I'm asking you to help me. 我已经尝试过“新的FlowLayout(FlowLayout.LEFT)”,但它没有用,所以我要求你帮助我。 Here is the picture of the frame and source code below it. 这是下面的框架和源代码的图片。

public class firstClass extends JFrame implements ActionListener {


private JFrame frame1;
private JFrame frame2;
private JPanel mainPanelFirst;
private JPanel secondPanel;
private JButton newWindowButton;
private int mulitplyPanels;
private JLabel leftLabel;
private JLabel rightLabel;
private JComboBox leftCB;
private JComboBox rightCB;

First Window: 第一窗口:

https://i.stack.imgur.com/DhXXM.png

public JFrame createMainUI(){

   frame1 = new JFrame("Main frame");
   frame1.setSize(600,600);
   frame1.setResizable(false);
   frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   frame1.setVisible(true);

   mainPanelFirst = new JPanel();
   mainPanelFirst.setLayout(new FlowLayout());
   frame1.add(mainPanelFirst);


   newWindowButton = new JButton("Open new window");
   newWindowButton.addActionListener(this);
   mainPanelFirst.add(newWindowButton);


   return frame1;

} }

Second Window(include the label I want to align): 第二个窗口(包括我想要对齐的标签):

https://i.stack.imgur.com/VRIFr.png

 public JFrame createSecondUI() {

    frame2 = new JFrame("Second frame");
    frame2.setSize(600, 600);
    frame2.setResizable(false);
    frame2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame2.setVisible(true);

    secondPanel = new JPanel();
    secondPanel.setLayout(new FlowLayout());
    secondPanel.setBackground(Color.gray);
    frame2.add(secondPanel);


    JPanel topPanel = new JPanel();
    topPanel.setLayout(new FlowLayout(70,400,20));
    topPanel.setBackground(Color.WHITE);
    secondPanel.add(topPanel);



    leftLabel = new JLabel("Name:");
    topPanel.add(leftLabel);



    return frame2;


}


 @Override
    public void actionPerformed(ActionEvent e) {

        createSecondUI();

    }
}

Thank you for your help :) 谢谢您的帮助 :)

Warning read this as well: Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? 警告也读这个: 我应该避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法吗?

Since that the JFrame is non-resizable , give the topPanel a defined size: 由于JFrame 不可调整大小 ,因此请为topPanel指定一个定义的大小:

JPanel topPanel = new JPanel();
topPanel.setPreferredSize(new Dimension(600,100));
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT,400,20));

I would suggest using a Layout Manager for your app. 我建议您为您的应用使用布局管理器。 The one you're looking for is most likely BorderLayout, unless you want specific abilities to control where and how your objects are laid out. 您正在寻找的那个很可能是BorderLayout,除非您需要特定的能力来控制对象的布局位置和方式。

Hope this helps 希望这可以帮助

Layout Managers 布局经理

How to use BorderLayout 如何使用BorderLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM