0

I want to have a jpanel with fixed size without layout manager in jscrollpane (which will be also without layout manager). I cant use any layout manager because i need to create an rectangle/circle at a location where user clicks (and allow drag & drop for all the created emelents)

    setLocationRelativeTo(null);
    setSize(300,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setName("dnd test");


    int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
    int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;


    JScrollPane scroll = new JScrollPane(panel,v,h);
    scroll.setLayout(null);
    scroll.setPreferredSize(new Dimension(1000, 1000));
    scroll.setBounds(0, 0, 1000, 1000);
    panel = buildComponentOnAbsoluteLayout(new JPanel(),scroll,0,0,500,500);
    panel.setBackground(Color.darkGray);
    panel.setSize(350,350);
    panel.setLayout(null);
    buildComponentOnAbsoluteLayout(new JButton("button 1"),panel,10,10,120,30);
    buildComponentOnAbsoluteLayout(new JButton("button 1"),panel,50,50,120,30);

    add(scroll);
    setVisible(true);

    public static  <T extends JComponent> T buildComponentOnAbsoluteLayout(T t,Container holder,int x, int y,int width, int heigth){
        t.setBounds(x,y,width,heigth);
        holder.add(t);
    return t;
}

Scrollbar will never pop up.

4

1 回答 1

0

我不能使用任何布局管理器,因为我需要在用户单击的位置创建一个矩形/圆形(并允许拖放所有创建的元素)

好吧,您不能使用 JDK 中的标准布局管理器,但是您应该使用布局管理器,因为布局管理器不仅仅是设置组件的位置。

滚动条永远不会弹出。

您的滚动不起作用,因为面板没有基于添加到面板的组件的适当首选大小。

查看Drag Layout,它是为此目的而设计的布局管理器。它允许随机放置,但仍会计算首选大小,因此滚动将起作用。

于 2014-10-20T18:59:13.963 回答