在 "North" BorderLayout 中左右对齐两个 JLabel
align left and right two JLabels in a "North" BorderLayout
我正在为我的应用程序使用 BorderLayout
。
setLayout(new BorderLayout());
我需要在 JPanel
.
的 "NORTH"
中左右对齐两个 JLabels
这是我的代码:
JPanel top = new JPanel();
top.add(topTxtLabel);
top.add(logoutTxtLabel);
add(BorderLayout.PAGE_START, top);
所以我需要左边的 topTxtLabel 和右边的 logoutTxtLabel。
我尝试再次实现边框布局以使用 "WEST" 和 "EAST",但没有成功。想法?
假设您的应用程序由 JFrame
和 BorderLayout
组成,您可以尝试这样做:再次将 JPanel
的布局模式设置为 BorderLayout
。在框架的北部添加面板。然后在东边和西边加上2JLabels
。您还可以将 JFrame
替换为另一个 JPanel
.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main
{
public static void main(String[] args)
{
new Main();
}
Main()
{
JFrame frame = new JFrame("MyFrame");
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel(new BorderLayout());
JLabel left = new JLabel("LEFT");
JLabel right = new JLabel("RIGHT");
JPanel top = new JPanel(new BorderLayout());
top.add(left, BorderLayout.WEST);
top.add(right, BorderLayout.EAST);
panel.add(top, BorderLayout.NORTH);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JLabel("Another dummy Label"), BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我正在为我的应用程序使用 BorderLayout
。
setLayout(new BorderLayout());
我需要在 JPanel
.
"NORTH"
中左右对齐两个 JLabels
这是我的代码:
JPanel top = new JPanel();
top.add(topTxtLabel);
top.add(logoutTxtLabel);
add(BorderLayout.PAGE_START, top);
所以我需要左边的 topTxtLabel 和右边的 logoutTxtLabel。 我尝试再次实现边框布局以使用 "WEST" 和 "EAST",但没有成功。想法?
假设您的应用程序由 JFrame
和 BorderLayout
组成,您可以尝试这样做:再次将 JPanel
的布局模式设置为 BorderLayout
。在框架的北部添加面板。然后在东边和西边加上2JLabels
。您还可以将 JFrame
替换为另一个 JPanel
.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main
{
public static void main(String[] args)
{
new Main();
}
Main()
{
JFrame frame = new JFrame("MyFrame");
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel(new BorderLayout());
JLabel left = new JLabel("LEFT");
JLabel right = new JLabel("RIGHT");
JPanel top = new JPanel(new BorderLayout());
top.add(left, BorderLayout.WEST);
top.add(right, BorderLayout.EAST);
panel.add(top, BorderLayout.NORTH);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JLabel("Another dummy Label"), BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}