[英]Java swing, scrollbar does not working with absolute layout
我想在jscrollpane中有一個不帶布局管理器的固定大小的jpanel(也將不帶布局管理器)。 我不能使用任何布局管理器,因為我需要在用戶單擊的位置創建一個矩形/圓形(並允許拖放所有創建的潤飾劑)
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;
}
滾動條將永遠不會彈出。
我不能使用任何布局管理器,因為我需要在用戶單擊的位置創建一個矩形/圓形(並允許拖放所有創建的潤飾劑)
好的,您不能使用JDK中的標准布局管理器,但是可以,您應該使用布局管理器,因為布局管理器不僅可以設置組件的位置。
滾動條將永遠不會彈出。
滾動不起作用,因為基於添加到面板的組件,面板沒有合適的首選尺寸。
查看Drag Layout ,這是為此目的而設計的布局管理器。 它允許隨機放置,但仍會計算首選大小,因此滾動將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.