我将JPanel包装在JScrollPane中,并且我正在创建使用JPanel的项目实例,但是当我将这些放入JScrollPane中包含的JPanel时,JScrollPane将不允许我滚动。
图像:
这是我店铺商品的代码:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextPane;
public class ShopItem extends JPanel {
private JPanel shopItemPanel;
private JPanel panelPlaced;
private JButton btnPurchase;
private JLabel priceLabel;
private JTextPane descPane;
private JLabel attrLabel;
private TitledBorder titledBorder;
private int posX;
private int posY;
private int shopCatId;
private int itemId;
private String itemName;
private int price;
private String attribute;
private int owned;
private String description;
private JLabel ownedLabel;
public ShopItem(int posX, int posY, JPanel panelPlaced, int shopCatId, int itemId, String itemName, int price, String attribute, int owned, String description) {
this.setPosX(posX);
this.setPosY(posY);
this.shopCatId = shopCatId;
this.itemId = itemId;
this.itemName = itemName;
this.price = price;
this.attribute = attribute;
this.owned = owned;
this.description = description;
shopItemPanel = new JPanel();
titledBorder = new TitledBorder(UIManager.getBorder("TitledBorder.border"), this.itemName, TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0));
shopItemPanel.setBorder(titledBorder);
shopItemPanel.setBounds(posX, posY, 170, 253);
shopItemPanel.setLayout(null);
btnPurchase = new JButton("Purchase");
btnPurchase.setBounds(40, 219, 89, 23);
shopItemPanel.add(btnPurchase);
priceLabel = new JLabel("Price: $"+this.price);
priceLabel.setBounds(10, 21, 147, 14);
shopItemPanel.add(priceLabel);
descPane = new JTextPane();
descPane.setEditable(false);
descPane.setBounds(10, 93, 147, 115);
descPane.setText(this.description);
shopItemPanel.add(descPane);
attrLabel = new JLabel("Att: "+this.attribute);
attrLabel.setBounds(10, 46, 150, 14);
shopItemPanel.add(attrLabel);
ownedLabel = new JLabel("Owned: "+this.owned);
ownedLabel.setBounds(10, 71, 150, 14);
shopItemPanel.add(ownedLabel);
panelPlaced.repaint();
}
public void remove() {
Shop.getShopItems().remove(this);
this.panelPlaced.remove(this.shopItemPanel);
this.panelPlaced.repaint();
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAttribute() {
return attribute;
}
public void setAttribute(String attribute) {
this.attribute = attribute;
}
public int getOwned() {
return owned;
}
public void setOwned(int owned) {
this.owned = owned;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public int getShopCatId() {
return shopCatId;
}
public void setShopCatId(int shopCatId) {
this.shopCatId = shopCatId;
}
public JPanel getPanelPlaced() {
return panelPlaced;
}
public void setPanelPlaced(JPanel panelPlaced) {
this.panelPlaced = panelPlaced;
}
public JPanel getShopItemPanel() {
return shopItemPanel;
}
public void setShopItemPanel(JPanel shopItemPanel) {
this.shopItemPanel = shopItemPanel;
}
public int getPosX() {
return posX;
}
public void setPosX(int posX) {
this.posX = posX;
}
public int getPosY() {
return posY;
}
public void setPosY(int posY) {
this.posY = posY;
}
public TitledBorder getTitledBorder() {
return titledBorder;
}
public void setTitledBorder(TitledBorder titledBorder) {
this.titledBorder = titledBorder;
}
}
答案 0 :(得分:2)
我没有看到您在已发布的代码中向JScrollPane添加任何内容,但我确实看到了大量setBounds(...)
次调用以及{{ 1}},意味着您正在设置组件的绝对大小,这将阻止组件在JScrollPane内部正确扩展。解决方案:不要这样做。使用布局管理器,避免使用null布局。
你应该避免使用null布局,因为这会使非常不灵活的GUI,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕,并且很难更新和维护。 / p>
例如,考虑创建一个使用setLayout(null)
的内部JPanel - 它代表一行和可变列,并且我将我的测试商店JPanels添加到其中。然后我将这个内部JPanel添加到JScrollPane,并在需要时将我的测试商店JPanels添加到其中,在每次添加后调用GridLayout(1, 0)
和revalidate()
。
其他建议:
repaint()
字段。int shopCatId, int itemId, String itemName, int price, String attribute, int owned, String description
和setLineWrap(true)
,以便自动换行文字。