我需要动态地将面板堆叠在另一个面板中,但是我需要添加卡片(面板)而不是从中心填充它,它们会到达容器的顶部。
还有这个: GridBagLayout - 垂直定位组件
但我无法添加代码以允许动态插入新的 JPanel。
这是我目前所拥有的:
private JLabel addVesselPane(String text) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.weightx = 1.0;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel vesselLabel = new JLabel( text, JLabel.LEFT );
vesselLabel.setFont(new java.awt.Font("Arial", Font.PLAIN, 9));
vesselLabel.setOpaque(true);
vesselLabel.setBackground(Color.WHITE);
JPanel jp = new JPanel( new BorderLayout() );
jp.setBorder( BorderFactory.createEtchedBorder() );
jp.setPreferredSize(new Dimension(190,30));
jp.add( vesselLabel );
westContentPanel.add(jp, gbc);
this.frame.revalidate();
return vesselLabel;
}
地点:
GridBagLayout grid = new GridBagLayout();
westContentPanel = new JPanel( grid );
JScrollPane westScrollPanel = new JScrollPane(westContentPanel);
但是当我...
for( int x=0; x < 5; x++ ) {
addVesselPane("Btn " + x);
}
结果是:
当我期望所有面板都在蓝色面板的顶部时。我必须说,当我的面板多于蓝色面板可以容纳的面板时,滚动条将被激活,我可以向下滚动。我也必须被允许删除面板。
似乎有些人坚持指出我不应该做什么,而不是与社区合作并有效地展示我应该做什么。也许是因为懒得写代码,甚至是因为完全不知道如何做得更好。尽管我指出了两个实际上解决了我的问题的参考资料,只缺少一些指导方针,但我还是受到了关于如何禁止(由谁)固定组件位置的批评的轰炸。好吧,尽管有仇恨者,但这是我的结果,即使没有任何人的帮助很难实现,我还是与您分享,以便您可以以最正确的方式进行调整。对不起大家,如果它不是 100% 在“规范”之内,但这是我在没有帮助和对该主题一无所知的情况下只能通过反复试验所能做的最好的。
“谈话很便宜。给我看代码”:
public void addScrollFrame( JPanel toWho, JPanel buttonPanel ) {
JButton btnAddType = new JButton("Add");
btnAddType.addActionListener( e -> {
JLabel card = addNewCard("This is a new Card!");
// Store it for future reference (Ex. change label)
});
buttonPanel.add(btnAddType);
JButton btnDeleteField = new JButton("Del");
buttonPanel.add(btnDeleteField);
JButton btnSaveRule = new JButton("Save");
buttonPanel.add(btnSaveRule);
this.westContentPanel = new JPanel();
westContentPanel.setLayout( new BoxLayout( westContentPanel, BoxLayout.Y_AXIS) );
JScrollPane scroll = new JScrollPane(westContentPanel);
scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setViewportView(westContentPanel);
toWho.add(scroll, BorderLayout.CENTER);
}
private JLabel addNewCard(String text) {
JLabel vesselLabel = new JLabel( text, JLabel.LEFT );
vesselLabel.setFont(new Font("Arial", Font.BOLD, 10));
vesselLabel.setOpaque(true);
vesselLabel.setBackground(Color.WHITE);
vesselLabel.setCursor( new Cursor( Cursor.HAND_CURSOR ) );
vesselLabel.setBorder( BorderFactory.createEmptyBorder(0, 3, 0, 0) );
vesselLabel.setPreferredSize( new Dimension(200,35) );
JPanel jp = new JPanel( new BorderLayout() );
jp.setMaximumSize( new Dimension(200,35) );
jp.setBorder( BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(3, 0, 0, 0), BorderFactory.createLineBorder( Color.BLACK )
));
jp.add( vesselLabel );
this.westContentPanel.add(jp);
this.frame.revalidate();
return vesselLabel;
}
toWho
是用作容器的面板,buttonPanel
是用作按钮栏的面板。
JPanel westButtonsPanel = new JPanel();
westButtonsPanel.setPreferredSize(new Dimension(220,40));
westButtonsPanel.setBorder( BorderFactory.createEtchedBorder() );
JPanel west = new JPanel( );
west.setLayout( new BorderLayout(0, 0) );
west.setBorder( BorderFactory.createEtchedBorder() );
west.add(westButtonsPanel,BorderLayout.NORTH);
addScrollFrame(west, westButtonsPanel);
带卷轴!正是我需要的!!
注意:您可能希望在
westContentPanel
范围内本地创建addScrollFrame
并作为参数传递给addNewCard()
。你选择。