如何在JScrollPanel中添加JPanel,然后再次添加JPanel?

时间:2018-11-16 09:37:09

标签: java swing jpanel jbutton jscrollpane

现在,我已经在stackoverflow周围进行了搜索,找不到任何人问同样的问题。

我有一个JPanel,里面有一个JScrollPane,里面有一个JPanel。 我试图在JScrollPane内的JPanel中添加一个JButton,并且在将JScrollPane添加到代码中之前,它可以正常工作。

问题在于JButton不再显示。

该部分的代码是:

//----- Second Panel -----
JPanel secondPanel = new JPanel();
secondPanel.setBorder(new LineBorder(Color.black, 1, false));
secondPanel.setLayout(new GridLayout(2,1));

//----- Upgrade Scroll Pane -----
JScrollPane upgradeScrollPane = new JScrollPane();
upgradeScrollPane.setBorder(new LineBorder(Color.black,1,false));

//----- Upgrade Panel -----
JPanel upgradePanel = new JPanel();
upgradePanel.setLayout(new GridLayout(44,5));

//----- Reinforced index finger -----
Upgrades.rifButton = new JButton();
loadImage(Upgrades.rifButton, "resources/Upgrades/Cursor/Reinforced index finger.png");
Upgrades.rifButton.addActionListener(e -> Upgrades.rif());
Upgrades.rifButton.setToolTipText("Test");
upgradePanel.add(Upgrades.rifButton);

//Adding Upgrade Panel in Upgrade Scroll Pane
upgradeScrollPane.add(upgradePanel);

//Adding Upgrade Scroll Pane in Second Panel
secondPanel.add(upgradeScrollPane);

我真的不知道我在做什么错。

是的,它是cookie单击器的副本,我只是想通过:D

来学习

1 个答案:

答案 0 :(得分:1)

upgradeScrollPane.add(upgradePanel); 

这是错误的方法。而是设置JScrollPane的查看端口或在构造上交替添加组件。