与JFrame.pack()类似的功能,适用于JPanel。

huangapple go评论67阅读模式
英文:

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).

与JFrame.pack()类似的功能,适用于JPanel。

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);

与JFrame.pack()类似的功能,适用于JPanel。

英文:

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);

与JFrame.pack()类似的功能,适用于JPanel。

huangapple
  • 本文由 发表于 2020年9月12日 20:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63860222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定