Stack JPanel cards from top to bottom inside a JPanel.

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

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:

但我无法使代码适应动态插入新的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);
}

结果是:

Stack JPanel cards from top to bottom inside a JPanel.

当我希望所有面板都位于蓝色面板的顶部。当我有更多的面板超出蓝色面板的容量时,滚动条会激活,我可以向下滚动。我也必须允许删除面板。

英文:

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(&quot;Arial&quot;, 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 &lt; 5; x++ ) {
  	addVesselPane(&quot;Btn &quot; + x);
}

The result is:

Stack JPanel cards from top to bottom inside a JPanel.

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(&quot;Add&quot;);
btnAddType.addActionListener( e -&gt; {
JLabel card = addNewCard(&quot;This is a new Card!&quot;);
// Store it for future reference (Ex. change label)
});
buttonPanel.add(btnAddType);
JButton btnDeleteField = new JButton(&quot;Del&quot;);
buttonPanel.add(btnDeleteField);
JButton btnSaveRule = new JButton(&quot;Save&quot;);
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(&quot;Arial&quot;, 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);

Stack JPanel cards from top to bottom inside a JPanel.

With scroll !! Just what I need !!

Stack JPanel cards from top to bottom inside a JPanel.

Note: You may want westContentPanel to be created local at addScrollFrame scope and passed as parameter to addNewCard(). You choose.

huangapple
  • 本文由 发表于 2023年4月11日 12:00:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982292.html
匿名

发表评论

匿名网友

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

确定