gpt4 book ai didi

java - 根据其中存在的组件调整 jpanel 的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:39 25 4
gpt4 key购买 nike

在下面的例子中,在边框布局的西侧,有一个父面板,里面有一个 BoxLayout 和几个面板。问题是西面板从上到下覆盖了整个区域。用于父面板内部子面板的 FlowLayout 占用了大量区域。是否可以根据组件压缩每个JPanel?另外,即使窗口最大化,它也应该保持不变?

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;

public class Sample extends JFrame {

private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;

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

/**
* Create the frame.
*/
public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
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);

JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);

JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.WEST);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));

JPanel panel_2 = new JPanel();
panel_1.add(panel_2);

textField = new JTextField();
panel_2.add(textField);
textField.setColumns(2);

textField_1 = new JTextField();
panel_2.add(textField_1);
textField_1.setColumns(2);

JPanel panel_3 = new JPanel();
panel_1.add(panel_3);

JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_3.add(chckbxNewCheckBox);

JPanel panel_4 = new JPanel();
panel_1.add(panel_4);

JButton btnNewButton = new JButton("New");
panel_4.add(btnNewButton);

JButton btnNewButton_1 = new JButton("New");
panel_4.add(btnNewButton_1);
}

}

最佳答案

一种方法是将 panel_1 添加到封闭面板。当您pack() 封闭的Window 时,默认的FlowLayout 符合封闭组件的首选大小。我在 CENTER 中添加了一个灰色面板作为占位符;调整框架大小以查看效果。

JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);

image

经测试:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;

public class Sample extends JFrame {

private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;

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

/**
* Create the frame.
*/
public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);

JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);

JPanel panel_1 = new JPanel();
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));

JPanel panel_2 = new JPanel();
panel_1.add(panel_2);

textField = new JTextField();
panel_2.add(textField);
textField.setColumns(2);

textField_1 = new JTextField();
panel_2.add(textField_1);
textField_1.setColumns(2);

JPanel panel_3 = new JPanel();
panel_1.add(panel_3);

JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_3.add(chckbxNewCheckBox);

JPanel panel_4 = new JPanel();
panel_1.add(panel_4);

JButton btnNewButton = new JButton("New");
panel_4.add(btnNewButton);

JButton btnNewButton_1 = new JButton("New");
panel_4.add(btnNewButton_1);

JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);
contentPane.add(flowPanel, BorderLayout.WEST);
contentPane.add(new JPanel(){

@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}

@Override
public Color getBackground() {
return Color.lightGray;
}

}, BorderLayout.CENTER);
}

}

关于java - 根据其中存在的组件调整 jpanel 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22745286/

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