繁体   English   中英

java 中带有“卡片”的可滚动 JPanel

[英]Scrollable JPanel with “cards” in it in java

我正在为我的学校项目开发 java 应用程序。 我需要在容器中显示“卡片”列表。 如果卡的数量超过 3,由于容器的大小,以下卡将不可见: 有问题的卡列表:

https://i.stack.imgur.com/ZMhxj.png

所以我正在尝试使用 JScrollPane 因为它似乎是解决方案。 但我没有成功使面板可滚动。 任何想法?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class FenetrePrincipale extends JFrame implements ActionListener,MouseListener{

  LinkedList<Carte> cartesFidelite; //Liste des cartes de fidélités
  
  JPanel conteneurCarte;
  JPanel monConteneurMain;
  JScrollPane scrollPane;

  public FenetrePrincipale(LinkedList<Carte> c){
    cartesFidelite=c; //List of cards

    //Window
    setTitle("Gestionnaire de cartes");
    setBounds(0, 0, 1195,722);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);

    //CONTAINER Main
    containerMain = new JPanel();
    containerMain.setLayout(null);
    containerMain.setBackground(Color.white);

    //CONTAINER List of cards
    conteneurCarte = new JPanel();
    conteneurCarte.setLayout(null);
    conteneurCarte.setBackground(new Color(242,242,242));
    conteneurCarte.setBorder(BorderFactory.createLineBorder(new Color(199,199,199)));
  
    scrollPane = new JScrollPane(conteneurCarte);
    scrollPane.setBounds(34, 90, 377,550);
    containerMain.add(scrollPane);

这是显示卡的方法:

public void refreshListCard (){
conteneurCarte.removeAll();
  int i = 0;
  for(Carte e : cartesFidelite){
    e.setLocation(15,(15+i*(172+15)));
    conteneurCarte.add(e);
    e.addMouseListener(this);
    i++;
  }
  conteneurCarte.updateUI();
}

谢谢你的帮助:)

不要使用 null 布局..

Swing 旨在与布局管理器一起使用。

如果您不使用布局管理器,特别是 JScrollPane 将不起作用。

另外,不要调用 updateUI()。 如果您从面板中添加/删除组件,则使用:

panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint(); 

这将反过来调用布局管理器。

阅读有关布局管理器的 Swing 教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM