在我的项目中,我有一个滚动窗格,可以包含各种面板,具体取决于用户操作。 顺便说一下,我想让每个面板都有一个固定的高度,然后再添加它们,它们应该显示在前面的下面。相反,他们根据现有面板的数量改变高度。 我写了一个主要课程,目的是重现当前的行为,以及一个应该解释我的目标的图像。
图像:
代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
public class MyClass {
static JScrollPane myScrollPaneContent;
static Box panelContainer;
static int i=0;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run(){
int i=0;
//mainWindow
JFrame mainWindow = new JFrame("MyFrame");
mainWindow.setLayout(new BorderLayout());
mainWindow.setMinimumSize(new Dimension(1050,300));
//panel to be placed in mainwindow (borderlayout.Center)
//contains a menu + scrollpane
JPanel centerPanel = new JPanel();
//Box for myScrollPaneContent
panelContainer = Box.createVerticalBox();
//button for centerPanel
JButton button = new JButton("addPanel");
button.setBackground(Color.green);
button.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
//nothing
}
@Override
public void mousePressed(MouseEvent e) {
//nothing
}
@Override
public void mouseExited(MouseEvent e) {
//nothing
}
@Override
public void mouseEntered(MouseEvent e) {
//nothing
}
@Override
public void mouseClicked(MouseEvent e) {
addPanel();
}
});
centerPanel.add(button);
myScrollPaneContent = new JScrollPane(panelContainer);
myScrollPaneContent.setPreferredSize(new Dimension(600,250));
myScrollPaneContent.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//all panels are added to the Box
//the box do not need to be added to scroll pane since it has been given into it s constructor
//the scrollpane is added to the centralpanel
centerPanel.add(myScrollPaneContent);
mainWindow.add(centerPanel,BorderLayout.CENTER);
mainWindow.add(Box.createVerticalGlue(),BorderLayout.SOUTH);
mainWindow.setVisible(true);
}
});
}
public static void addPanel(){
switch(i++){
case 0:
//panel 1 for Box
JPanel one = new JPanel();
JLabel labOne = new JLabel("hello");
one.add(labOne);
one.setPreferredSize(new Dimension(1000,25));
one.setBackground(Color.white);
panelContainer.add(one);
break;
case 1:
//panel 2 for Box
JPanel two = new JPanel();
JLabel labTwo = new JLabel("i am a label");
two.add(labTwo);
two.setPreferredSize(new Dimension(1000,30));
two.setBackground(Color.yellow);
panelContainer.add(two);
break;
case 2:
//panel 3 for Box
JPanel three = new JPanel();
JLabel labThree = new JLabel("Bye bye");
three.add(labThree);
three.setPreferredSize(new Dimension(1000,30));
three.setBackground(Color.gray);
panelContainer.add(three);
break;
case 3:
//panel 4 for Box
JPanel four = new JPanel();
JLabel labFour = new JLabel("boyz");
four.add(labFour);
four.setPreferredSize(new Dimension(1000,30));
four.setBackground(Color.blue);
panelContainer.add(four);
break;
default:
i=0;
break;
}
myScrollPaneContent.revalidate();
myScrollPaneContent.repaint();
}
}
答案 0 :(得分:1)
你想要这样的东西:
JPanel scrollPaneContainer = new JPanel( new BorderLayout() );
Box panelContainer = Box.createVerticalBox();
scrollPaneContainer.add(panelContainer, BorderLayout.PAGE_START);
JScrollPane myScrollPaneContent = new JScrollPane(scrollPaneContainer);
现在将尊重panelContainer的首选高度。如果有任何额外的空间,它将进入中心。如果空间不足,则会出现滚动条。
此外,您不应该使用静态变量。这是设计不佳的GUI的标志。您不应该在main()方法中创建GUI组件。看看Swing tutorial的演示。它们向您展示了如何创建一个面板来保持Swing组件,然后将面板添加到框架中。