现在,我已经在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
来学习答案 0 :(得分:1)
upgradeScrollPane.add(upgradePanel);
这是错误的方法。而是设置JScrollPane
的查看端口或在构造上交替添加组件。