英文:
Something similar to JFrame.pack() for JPanel
问题
我有一个 JPanel,我想设置它的宽度,但让高度由其内容确定。包含的 JPanel 应保持其大小不变。如果我在 "content" 中放入多行文本,当然会自动跨足更多的高度。如果所需高度超过容器的可用高度,滚动条是理想的(但这不太重要)。
这可能非常简单,但我就是想不出来...
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JPanelContentSizedDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(JPanelContentSizedDemo::new);
}
JPanelContentSizedDemo() {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setResizable(false);
frame.setLayout(null);
// 包含面板
JPanel panelContainer = new JPanel();
panelContainer.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panelContainer.setBounds(50, 50, 300, 200);
panelContainer.setBackground(Color.RED);
frame.add(panelContainer);
// 做什么...
// panelContainer.setLayout(new BoxLayout(panelContainer, BoxLayout.Y_AXIS));
panelContainer.setLayout(new BorderLayout());
// 我想根据其内容调整大小的面板
JPanel dialog = new JPanel(new BorderLayout());
dialog.setBackground(Color.GREEN);
dialog.setBorder(BorderFactory.createLineBorder(Color.BLACK));
dialog.add(new JLabel("这是一些内容!"));
panelContainer.add(dialog);
frame.setVisible(true);
}
}
英文:
I have a JPanel that I want to set the width of, but let the height be determined by its content. The containing JPanel should keep its size. If I put multiple lines of text in the "content" it should of course automatically span more height. Should the needed height ever exceed the available height of the container, a scrollbar would be ideal (but that's less important).
This is probably really simple, but I just can't figure it out...
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JPanelContentSizedDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(JPanelContentSizedDemo::new);
}
JPanelContentSizedDemo() {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setResizable(false);
frame.setLayout(null);
// the containing panel
JPanel panelContainer = new JPanel();
panelContainer.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panelContainer.setBounds(50, 50, 300, 200);
panelContainer.setBackground(Color.RED);
frame.add(panelContainer);
// what to do...
//panelContainer.setLayout(new BoxLayout(panelContainer, BoxLayout.Y_AXIS));
panelContainer.setLayout(new BorderLayout());
// the panel that I want to size to its content
JPanel dialog = new JPanel(new BorderLayout());
dialog.setBackground(Color.GREEN);
dialog.setBorder(BorderFactory.createLineBorder(Color.BLACK));
dialog.add(new JLabel("This is some content!"));
panelContainer.add(dialog);
frame.setVisible(true);
}
}
答案1
得分: 1
这可能有效:
// 做什么...
panelContainer.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.CENTER;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
...
panelContainer.add(dialog, c);
英文:
This might work:
// what to do...
panelContainer.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.CENTER;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
...
panelContainer.add(dialog, c);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论