Java中滚动时消失_关于java:使用JPanel构建JScrollPane时,滚动条消失

我要做的是将图像加载到JScrollPane中,并由其他组件包围,并且当窗口小于图像时,它应该被滚动条包围,以便您可以通过滚动查看整个图像。

我通过使用我自己的类ImagePanel构造一个JScrollPane来做到这一点,该类扩展了绘制图像的JPanel。 图像已正确加载并显示,但是滚动条消失了。 我是否误解了JComponents的某些部分或出了什么问题?

public class ImagePanel extends JPanel {

private BufferedImage img;

public ImagePanel () {

}

public void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(img, 0, 0, this);

}

public void setImage(BufferedImage img) {

this.img = img;

}

}

public class MainGUI extends JPanel {

private ImagePanel imgP;

private JScrollPane pane;

public MainGUI () {

setLayout(new GridBagLayout());

//Create local components

//JComponents

JButton hideButton = new JButton("Hide");

JButton hidecategoryButton = new JButton ("Hide Category");

JButton removeButton = new JButton("Remove");

JButton searchButton = new JButton("Search");

JButton whatishereButton = new JButton("What is here?");

JComboBox placeComboBox;

JLabel categoriesLabel = new JLabel("Categories");

JLabel newLabel = new JLabel("New:");

JTextArea categoriesArea = new JTextArea();

JTextField searchField = new JTextField(10);

//Other

String[] placeTypes = {"Named Place","Described Place"};

//Initialize and add behaviour

searchButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String s = searchField.getText();

}

});

categoriesArea.setEditable(false);

placeComboBox = new JComboBox<>(placeTypes);

searchField.setToolTipText("Search...");

searchField.setMinimumSize(new Dimension(5, 1));

//Just fileloading test

BufferedImage bi2 = null;

try {

bi2 = ImageIO.read(new File("dafoe.jpg"));

}

catch (Exception ex) {

}

imgP = new ImagePanel();

imgP.setImage(bi2);

pane = new JScrollPane(imgP);

//Add to panel

GridBagConstraints gc;

//Top row

JPanel toprowPanel = new JPanel();

toprowPanel.add(newLabel);

toprowPanel.add(placeComboBox);

toprowPanel.add(searchField);

toprowPanel.add(searchButton);

toprowPanel.add(hideButton);

toprowPanel.add(removeButton);

toprowPanel.add(whatishereButton);

gc = new GridBagConstraints();

gc.gridx = 0;

gc.gridy = 0;

gc.weightx = 1;

//gc.gridwidth = 6;

add(toprowPanel, gc);

//Hide category

gc = new GridBagConstraints();

gc.anchor = GridBagConstraints.NORTH;

gc.gridx = 7;

gc.gridy = 3;

gc.weightx = 0;

gc.weighty = 0;

add(hidecategoryButton, gc);

//Category Label

gc = new GridBagConstraints();

gc.anchor = GridBagConstraints.BELOW_BASELINE;

gc.gridx = 7;

gc.gridy = 1;

gc.weightx = 0;

gc.weighty = 0;

add(categoriesLabel, gc);

//categoriesarea

gc = new GridBagConstraints();

gc.anchor = GridBagConstraints.BASELINE_TRAILING;

gc.fill = GridBagConstraints.BOTH;

gc.gridx = 7;

gc.gridy = 2;

gc.ipadx = 5;

gc.ipady = 70;

gc.weightx = 0;

gc.weighty = 0;

add(categoriesArea, gc);

//Image

gc = getImageConstraints();

add(pane,  gc);

}

public void updateImage(BufferedImage bi) {

imgP.setImage(bi);

//imgP.repaint();

pane.repaint();

}

private GridBagConstraints getImageConstraints() {

GridBagConstraints gc = new GridBagConstraints();

gc.fill = GridBagConstraints.BOTH;

gc.gridheight = 3;

gc.gridwidth = 1;

gc.gridx = 0;

gc.gridy = 1;

gc.weightx = 1;

gc.weighty = 1;

return gc;

}

}

my own class ImagePanel, which extends JPanel, which paints the image. The image is loaded and displayed correctly, but the scrollbars disappear.

当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,滚动条将自动显示。

您的自定义组件的首选大小为(0,0),因此滚动条将永远不会出现。

您需要重写ImagePanel类的getPreferredSize()以返回图像的大小。

甚至更简单,只需将JLabel与ImageIcon一起使用,并将标签添加到滚动窗格即可。 JLabel已经正确实现了getPreferredSize()方法。

谢谢,这对我有很大帮助。

当布局为GridBagLayout时,不能使用滚动条。 您必须使用BorderLayout或FlowLayout

用jtable链接示例(类似于图像):

http://myquestionjava.blogspot.com/2016/04/jscrollbars-and-jtable-in-java.html

所以我改为BorderLayout,我仍然没有任何滚动条。 更新:遵循示例后仍然没有运气。

(1-)当然,您可以将滚动窗格与GridBagLayout一起使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值