I'm having trouble getting JScrollPanes to actually display scroll bars

前端 未结 3 1509
小鲜肉
小鲜肉 2021-01-20 17:20

I\'ve found some examples scattered around the internet involving getting an image or a textbox to display scroll bars, but they all involve a program that basically display

3条回答
  •  有刺的猬
    2021-01-20 18:12

    In Swing, sizing and positioning of children is the exclusive job of a LayoutManager. Choose one that supports your requirements, as a last resort implement a highly specialized one. Children collaborate by reporting sizing hints, so implement any custom components to return something reasonable in the getXXSize methods.

    When you feel an irresistable urge to manually interfere, at least let the manager do as much as possible. In your context that might be to take over the positioning but let the manager handle the sizing, particularly calculating the sizing hints of the parent. Here's a code snippet using Rob's DragLayout:

    DragLayout layout = new DragLayout();
    JComponent field = new JPanel(layout);
    JComponent player = new JLabel("I'm moving around");
    field.add(player);
    player.setLocation(200, 200);
    frame.add(new JScrollPane(field));
    frame.pack();
    frame.setVisible(true);
    

提交回复
热议问题