gpt4 book ai didi

java - 在 BorderLayout 中向 NORTH 组件添加垂直间距

转载 作者:行者123 更新时间:2023-12-01 07:28:00 25 4
gpt4 key购买 nike

我有一个带有 BorderLayout 的 JFrame。我在 JFrame 的北侧添加了一个 JPanel。在此面板中,我想以绝对定位向其添加组件。在 JFrame 的中心一侧,我添加了另一个 JPanel ,它应该占用很大的空间。然而,当我运行该应用程序时,我从北侧 JPanel 上看不到任何东西,因为中心 JPanel 占据了 JFrame 的所有空间!如何为 North JPanel 提供垂直空间?

我确实需要对北 JPanel 使用绝对定位。

这是我的代码:

public class AAAA extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AAAA frame = new AAAA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public AAAA() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1136, 520);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(null);

JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(0, 0, 117, 29);
panel.add(btnNewButton);

JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
}

}

最佳答案

更新 1

我看到您已经选择了答案(我认为为时过早)。这是我相信您想要实现的目标的第一次迭代。无需设置边界或首选大小..

Laid Out 1

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class AAAA extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

public void run() {
try {
AAAA frame = new AAAA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public AAAA() {
super("Laid Out");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// OMG! If you can make a GUI break at 1336 px wide, it should be
// possible to make it break at ..much smaller!
//setBounds(100, 100, 1136, 520);
setBackground(Color.YELLOW);
contentPane = new JPanel();
contentPane.setBackground(Color.BLUE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

// make it a FlowLayout as FlowLayout.LEADING with no spacing to
// make the button snug up against the top left
JPanel panel = new JPanel(
new FlowLayout(FlowLayout.LEADING, 0, 0));
panel.setBackground(Color.GREEN);
contentPane.add(panel, BorderLayout.NORTH);
//panel.setPreferredSize(new Dimension(1024,400));

JButton btnNewButton = new JButton("New button");
// we change the margin to make the button bigger than natural size.
btnNewButton.setMargin(new Insets(6, 22, 6, 22));
panel.add(btnNewButton);

JPanel panel_1 = new JPanel();
// create a solic color image to both pad the GUI and
// provide visual indication of where it is.
BufferedImage bi = new BufferedImage(
400,200,BufferedImage.TYPE_INT_RGB);
JLabel padder = new JLabel(new ImageIcon(bi));
panel_1.add(padder);
panel_1.setBackground(Color.RED);
contentPane.add(panel_1, BorderLayout.CENTER);
pack();
setMinimumSize(getSize());
}
}

关于java - 在 BorderLayout 中向 NORTH 组件添加垂直间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21167133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com