将 null 布局与卡片布局相结合 [java]
combining null layout with card layout [java]
我正在尝试将 cardlayout 功能与 null 布局相结合。
但是,当我尝试 运行 该程序时,它不起作用。这些按钮没有出现在我告诉他们的边界处。我是做错了什么还是使用它们的整个概念都是错误的?
public class CardLayoutTest extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel cardPanel, jp1, jp2, buttonPanel;
private JLabel jl1, jl2;
private JButton btn1, btn2;
private CardLayout cardLayout = new CardLayout();
public CardLayoutTest() {
setTitle("Test med CardLayout");
setSize(400, 300);
Insets insets = getInsets();
cardPanel = new JPanel();
buttonPanel = new JPanel();
cardPanel.setLayout(cardLayout);
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("Card 1");
jl2 = new JLabel("Card 2");
jp1.add(jl1);
jp2.add(jl2);
cardPanel.add(jp1, "1");
cardPanel.add(jp2, "2");
btn1 = new JButton("Show Card 1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "1");
}
});
btn2 = new JButton("Show Card 2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "2");
}
});
buttonPanel.add(btn1);
buttonPanel.add(btn2);
add(cardPanel, BorderLayout.NORTH);
add(buttonPanel, null);
Dimension size1 = btn1.getPreferredSize();
btn1.setBounds(100 + insets.left, 100 + insets.top,
size1.width, size1.height);
Dimension size = btn2.getPreferredSize();
btn2.setBounds(210 + insets.left, 130 + insets.top,
size.width, size.height);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
CardLayoutTest frame = new CardLayoutTest();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Am i doing something wrong
是的,假设 null
布局可以帮助您(但不了解 null
布局是什么以及您需要从布局管理中接管的工作 API)
or is the whole concept of using them both wrong?
简而言之,是的。 99.99% 的时间你认为你需要 null
布局,但你不需要,甚至在你 "might" 的那 0.01% 的时间里,你可能不需要
那么,答案是什么?令人惊讶的是,使用适当的布局管理器并停止担心 "pixel perfect" 布局,它们是一种幻觉。现代计算机系统 运行 硬件和软件的真实性改变了 UI 的各个方面,这将改变组件需要交互或调整大小的方式。
虽然您可能正在考虑 运行 仅在一个 OS 上使用此功能,但即使如此,硬件和软件驱动程序以及操作设置仍会更改字体大小、字体规格和 DPI 所有其中将影响组件需要正确呈现的 space 数量。
相反,关注 UI 的工作流程和可用性,以最大化用户体验。
因此,根据我对您问题的(有限)理解,我在 buttonPanel
上使用了 GridBagLayout
而不是 "mimic" 您的预期结果。
buttonPanel = new JPanel(new GridBagLayout());
//...
add(buttonPanel, BorderLayout.SOUTH);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(100, 0, 0, 0);
buttonPanel.add(btn1, gbc);
gbc.insets = new Insets(0, 0, 0, 0);
gbc.gridx++;
gbc.gridy++;
buttonPanel.add(btn2, gbc);
开始使用布局管理器可能有点困难,但它们是非常强大的工具。我的整个职业生涯都在使用所有内置的布局管理器、一个第 3 方布局和几个我为解决特定问题而构建的自定义布局管理器。我从来没有觉得有必要退回到 null
布局,我发现它们有局限性,它们需要大量时间和工作来维护并且通常很懒惰
我正在尝试将 cardlayout 功能与 null 布局相结合。 但是,当我尝试 运行 该程序时,它不起作用。这些按钮没有出现在我告诉他们的边界处。我是做错了什么还是使用它们的整个概念都是错误的?
public class CardLayoutTest extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel cardPanel, jp1, jp2, buttonPanel;
private JLabel jl1, jl2;
private JButton btn1, btn2;
private CardLayout cardLayout = new CardLayout();
public CardLayoutTest() {
setTitle("Test med CardLayout");
setSize(400, 300);
Insets insets = getInsets();
cardPanel = new JPanel();
buttonPanel = new JPanel();
cardPanel.setLayout(cardLayout);
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("Card 1");
jl2 = new JLabel("Card 2");
jp1.add(jl1);
jp2.add(jl2);
cardPanel.add(jp1, "1");
cardPanel.add(jp2, "2");
btn1 = new JButton("Show Card 1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "1");
}
});
btn2 = new JButton("Show Card 2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "2");
}
});
buttonPanel.add(btn1);
buttonPanel.add(btn2);
add(cardPanel, BorderLayout.NORTH);
add(buttonPanel, null);
Dimension size1 = btn1.getPreferredSize();
btn1.setBounds(100 + insets.left, 100 + insets.top,
size1.width, size1.height);
Dimension size = btn2.getPreferredSize();
btn2.setBounds(210 + insets.left, 130 + insets.top,
size.width, size.height);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
CardLayoutTest frame = new CardLayoutTest();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Am i doing something wrong
是的,假设 null
布局可以帮助您(但不了解 null
布局是什么以及您需要从布局管理中接管的工作 API)
or is the whole concept of using them both wrong?
简而言之,是的。 99.99% 的时间你认为你需要 null
布局,但你不需要,甚至在你 "might" 的那 0.01% 的时间里,你可能不需要
那么,答案是什么?令人惊讶的是,使用适当的布局管理器并停止担心 "pixel perfect" 布局,它们是一种幻觉。现代计算机系统 运行 硬件和软件的真实性改变了 UI 的各个方面,这将改变组件需要交互或调整大小的方式。
虽然您可能正在考虑 运行 仅在一个 OS 上使用此功能,但即使如此,硬件和软件驱动程序以及操作设置仍会更改字体大小、字体规格和 DPI 所有其中将影响组件需要正确呈现的 space 数量。
相反,关注 UI 的工作流程和可用性,以最大化用户体验。
因此,根据我对您问题的(有限)理解,我在 buttonPanel
上使用了 GridBagLayout
而不是 "mimic" 您的预期结果。
buttonPanel = new JPanel(new GridBagLayout());
//...
add(buttonPanel, BorderLayout.SOUTH);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(100, 0, 0, 0);
buttonPanel.add(btn1, gbc);
gbc.insets = new Insets(0, 0, 0, 0);
gbc.gridx++;
gbc.gridy++;
buttonPanel.add(btn2, gbc);
开始使用布局管理器可能有点困难,但它们是非常强大的工具。我的整个职业生涯都在使用所有内置的布局管理器、一个第 3 方布局和几个我为解决特定问题而构建的自定义布局管理器。我从来没有觉得有必要退回到 null
布局,我发现它们有局限性,它们需要大量时间和工作来维护并且通常很懒惰