英文:
Stack JPanel cards from top to bottom inside a JPanel
问题
I need to dynamically stack panels inside another one, but instead of filling it from the center, I need to add cards (panels) and have them positioned at the top of the container.
这个问题要求将面板动态堆叠在另一个面板内,而不是从中心填充,我需要添加卡片(面板),并将它们放在容器的顶部。
You can achieve a layout similar to the examples provided in these links:
-
你可以实现类似这些链接中提供的示例布局:
和
https://stackoverflow.com/questions/49330716/gridbaglayout-vertically-positioning-components
但我无法使代码适应动态插入新的JPanels。
这是我目前的代码:
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);
}
结果是:
当我希望所有面板都位于蓝色面板的顶部。当我有更多的面板超出蓝色面板的容量时,滚动条会激活,我可以向下滚动。我也必须允许删除面板。
英文:
I need to dynamically stack panels inside another one, but instead filling it from center, I need to add cards ( panels ) and they going to top of the container.
Much more like this:
http://www.java2s.com/Tutorials/Java/Swing_How_to/Layout/Force_components_to_be_stacked_from_top_not_vertically_centered_with_GridBagLayout.htm
and this:
https://stackoverflow.com/questions/49330716/gridbaglayout-vertically-positioning-components
But I can't addapt the code to allow dynamically inserts of new JPanels.
This is what I have so far:
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;
}
where:
GridBagLayout grid = new GridBagLayout();
westContentPanel = new JPanel( grid );
JScrollPane westScrollPanel = new JScrollPane(westContentPanel);
but when I ...
for( int x=0; x < 5; x++ ) {
addVesselPane("Btn " + x);
}
The result is:
When I expect all panels being at the top of the blue panel. I must say when I have more panels than the blue panel can hold, the scroller is activated and I can scroll down. I must be allowed to delete panels too.
答案1
得分: -1
以下是代码部分的翻译:
public void addScrollFrame(JPanel toWho, JPanel buttonPanel) {
JButton btnAddType = new JButton("添加");
btnAddType.addActionListener(e -> {
JLabel card = addNewCard("这是一个新卡片!");
// 为将来的引用存储(例如更改标签)
});
buttonPanel.add(btnAddType);
JButton btnDeleteField = new JButton("删除");
buttonPanel.add(btnDeleteField);
JButton btnSaveRule = new JButton("保存");
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"是用作按钮栏的面板。
英文:
Solved:
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
is a Panel to use as container and buttonPanel
a panel to use as button bar.
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);
With scroll !! Just what I need !!
Note: You may want westContentPanel
to be created local at addScrollFrame
scope and passed as parameter to addNewCard()
. You choose.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论