0

我在 JScrollPane 中有一个 JPanel。创建 Panel 和 ScrollPane 后,我用 JButtons 填充 Panel。但我没有让 ScrollPane 工作。它只是不会滚动我的面板。

也许这很容易,但我在 GUI 编程方面没有这种经验。我已经搜索并尝试了一些我在那里发现的东西......但我没有让我的 GUI 工作。

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.CardLayout;
import java.util.ArrayList;

import javax.swing.JLabel;

public class GUI extends JFrame {
    private Container pinnwand;

    private JPanel Team = new JPanel();
    private JPanel MonsterListe = new JPanel();

    private JScrollPane cntAvailableMonster = new JScrollPane();
    private ArrayList<JLabel> AvailableMonstersLabels = new ArrayList<JLabel>();


    public GUI() {
        this.pinnwand = getContentPane();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 530, 443);
        this.pinnwand.setLayout(new CardLayout(0,0));


        this.Team.setBackground(new Color(44,44,44));
        this.Team.setLayout(null);


        this.MonsterListe.setMinimumSize(new Dimension(200,400));
        this.cntAvailableMonster.setBounds(300, 0, 200, 400);
        this.cntAvailableMonster.setPreferredSize(new Dimension(200,1000));
        this.cntAvailableMonster.createVerticalScrollBar();
        this.cntAvailableMonster.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.cntAvailableMonster.setViewportView(this.MonsterListe);    
        this.MonsterListe.setLayout(null);


        this.Team.add(this.cntAvailableMonster);


        this.pinnwand.add(this.Team,"Team");

        this.setAvailableMonsters();
    }

    private void setAvailableMonsters() {
        for(int i = 0,amount = 20;i < amount;i++) {
            this.AvailableMonstersLabels.add(new JLabel("monster"+i));
            this.AvailableMonstersLabels.get(i).setBounds(0, i*30, 300, 30);
            this.AvailableMonstersLabels.get(i).setBackground(new Color(255,0,0));
            this.AvailableMonstersLabels.get(i).setOpaque(true);
            this.MonsterListe.add(this.AvailableMonstersLabels.get(i));
            this.MonsterListe.setBackground(new Color(0,0,0));
        }
    }

    public static void main(String[] args) {
        new GUI().setVisible(true);
    }
}

提前致谢 :)

4

1 回答 1

-1

啊,好的,我找到了问题。我必须将 PreferredSize() 设置为 MonsterListe 而不是 cntAvailableMonster。

于 2013-06-30T19:37:52.337 回答