[英]Java method for getting width of swing object of any type?
我有這種擺放物體的方法。 目前,它僅支持按鈕(或具有定義方法的任何其他對象)。 我需要一種適用於任何對象的方法,而不必為每種類型的對象(按鈕,文本區域,面板等)定義一個方法。
這是我的代碼:
// Visual Methods for placing visual objects:::
static class Layout{
static class Button{ // buttons
static void PlaceUnder(JButton target,JButton src){
int x = src.getLocation().x;
int y = src.getLocation().y + src.getSize().height+2;
target.setLocation(x,y);
}
static void PlaceOver(JButton target,JButton src){
int x = src.getLocation().x;
int y = src.getLocation().y - target.getSize().height-2;
target.setLocation(x,y);
}
} // end of buttons
}
// done....
使用javax.swing.JComponent
(或java.awt.Component
)基類。 每個Swing組件都從這些組件擴展。
static void placeUnder(JComponent target, JComponent src) {
int x = src.getLocation().x;
int y = src.getLocation().y + src.getSize().height+2;
target.setLocation(x,y);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.