我的代码有JPanel
,其中包含三个可折叠的JPanel
。外JPanel
使用BoxLayout
垂直堆叠三个JPanel
。但是,当我折叠JPanel
时,顶部JPanel
将始终展开以填充该区域(即使我setMaximumSize()
等),而我希望较低的JPanel
s向上扩展。这通常是毛病。我在看GridBagLayout
,那会更适合这种努力吗?
感谢。
这是我梦寐以求的梦想中的VB图像(标题为“垂直面板”的图像):
答案 0 :(得分:2)
我强烈建议MigLayout。它非常强大且易于使用。它也被广泛使用。
答案 1 :(得分:2)
我不知道可折叠面板是什么。它是一直折叠到0,还是有最小高度?
如果您将最大尺寸设置为始终等于首选尺寸,那么您应该可以使用BoxLayout。请确保您也使用:
panel.add( Box.createVerticalGlue() );
在面板底部,允许胶水使用额外的空间。
我需要永远地从我面前散布的500行垃圾中删除一个可编辑的片段。
这就是创建SSCCE并忘记垃圾代码的原因。您只需要一个带有3个可折叠面板的面板。然后添加一个按钮以折叠面板,看看会发生什么。最好从演示代码开始,然后编写500行代码并发现它不起作用。
答案 2 :(得分:2)
或基于GridBagLayout
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ExpansiblePanel {
public static void main(String[] args) {
CollapsablePanel cp = new CollapsablePanel("test", buildPanel());
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(cp));
f.setPreferredSize(new Dimension(360, 200));
f.setLocation(150, 150);
f.pack();
f.setVisible(true);
}
public static JPanel buildPanel() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2, 1, 2, 1);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
JPanel p1 = new JPanel(new GridBagLayout());
gbc.gridwidth = GridBagConstraints.RELATIVE;
p1.add(new JButton("button 1"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
p1.add(new JButton("button 2"), gbc);
gbc.gridwidth = GridBagConstraints.RELATIVE;
p1.add(new JButton("button 3"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
p1.add(new JButton("button 4"), gbc);
p1.setBackground(Color.blue);
return p1;
}
private ExpansiblePanel() {
}
}
class CollapsablePanel extends JPanel {
private static final long serialVersionUID = 1L;
private boolean selected;
private JPanel contentPanel_;
private HeaderPanel headerPanel_;
private class HeaderPanel extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
private String text_;
private Font font;
private BufferedImage open, closed;
final int OFFSET = 30, PAD = 5;
HeaderPanel(String text) {
addMouseListener(this);
text_ = text;
font = new Font("sans-serif", Font.PLAIN + Font.BOLD, 12);
// setRequestFocusEnabled(true);
setPreferredSize(new Dimension(200, 25));
setBackground(Color.black);
setForeground(Color.red);
int w = getWidth();
int h = getHeight();
/*try {
open = ImageIO.read(new File("images/arrow_down_mini.png"));
closed = ImageIO.read(new File("images/arrow_right_mini.png"));
} catch (IOException e) {
e.printStackTrace();
}*/
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int h = getHeight();
/*if (selected)
g2.drawImage(open, PAD, 0, h, h, this);
else
g2.drawImage(closed, PAD, 0, h, h, this);
*/ // Uncomment once you have your own images
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = font.getLineMetrics(text_, frc);
float height = lm.getAscent() + lm.getDescent();
float x = OFFSET;
float y = (h + height) / 2 - lm.getDescent();
g2.drawString(text_, x, y);
}
@Override
public void mouseClicked(MouseEvent e) {
toggleSelection();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
CollapsablePanel(String text, JPanel panel) {
super(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(1, 3, 0, 3);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
selected = false;
headerPanel_ = new HeaderPanel(text);
setBackground(Color.orange);
contentPanel_ = panel;
add(headerPanel_, gbc);
add(contentPanel_, gbc);
contentPanel_.setVisible(false);
JLabel padding = new JLabel();
gbc.weighty = 1.0;
add(padding, gbc);
}
public void toggleSelection() {
selected = !selected;
if (contentPanel_.isShowing()) {
contentPanel_.setVisible(false);
} else {
contentPanel_.setVisible(true);
}
revalidate();
headerPanel_.repaint();
}
}