[英]Why do Java swing components show as NOT lightweight even though they should be?
我很好奇... 為什么JComponent.isLightweightComponent(Component c) 方法在傳遞JLabel、JButton 等swing 組件時會返回false? 根據我閱讀的所有內容,這些組件應該是輕量級的。
這是否表明他們實際上是重量級的(絕對不應該是)?
或者 isLightweightComponent() 方法是否以某種方式被破壞?
還是我不了解 isLightweightComponent() 方法?
試試下面的代碼,看看我的意思...
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SwingLightweightTest {
public static void main(String[] args) {
// why do swing components show that they not Lightweight?
// does this mean that they are Heavywight?
// or is the .isLightweightComponent() method broken?
JLabel jLabel = new JLabel();
testLightweight(jLabel);
JButton jButton = new JButton();
testLightweight(jButton);
JPanel jPanel = new JPanel();
testLightweight(jPanel);
}
private static void testLightweight(JComponent comp) {
String isIsnot;
isIsnot = JComponent.isLightweightComponent(comp) ? "IS ": "IS NOT ";
System.out.println(comp.getUIClassID() + " \t" + isIsnot + "a lightweight component");
}
}
它返回以下內容:
LabelUI 不是輕量級組件
ButtonUI 不是輕量級組件
PanelUI 不是輕量級組件
isLightweightComponent
方法實際上不是檢查組件本身是否是輕量級的,而是檢查組件的“對等體”是否是輕量級的。 (因此,也許這個方法沒有很好地命名?)
該方法在內部檢查組件的對等方是否是LightweightPeer
的實例: c.getPeer() instanceof LightweightPeer
。 而且看起來只有NullComponentPeer
和NullEmbeddedFramePeer
實現了這個接口。 此外, isLightweightComponent
的 JavaDoc 有這樣的說法:
如果此組件是輕量級的,即如果它沒有本機窗口系統對等體,則返回 true。
據我所知,peers 是一種獲取原生 OS 事件並將它們路由到組件的幕后手段。
更新
經過進一步調查,我發現在組件可見之前它沒有對等節點( c.getPeer()
返回 null),因此檢查c.getPeer() instanceof LightweightPeer
將返回false
(似乎表明該組件不是輕量級的是誤導)。 一旦一個組件變得可見,它就會被分配一個對等體(例如,我的測試表明 JLabel 獲取一個 NullComponentPeer 實例作為它的對等體),因此將從對isLightweightComponent
的調用返回正確的信息。
總結: isLightweightComponent
僅檢查組件的對等方是否是LightweightPeer
的實例,並且當它無法實際確定對等方是否為LightweightPeer
,它返回false
(而不是返回null
或拋出異常)。
的方法(約AWT /擺動GUI系統屬性,尺寸,isXxxXxx等)部分所需可見頂層容器,分別true
從isEventDispatchThread
,例如
import java.awt.EventQueue;
import javax.swing.*;
import java.awt.event.*;
public class Test {
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private JLabel label = new JLabel("JLabel");
private JButton button = new JButton("JButton");
private String[] list = {"1", "2", "3", "4",};
private JComboBox comb = new JComboBox(list);
final JPopupMenu pop = new JPopupMenu();
private Boolean bol = false;
public Test() {
comb.setLightWeightPopupEnabled(true);
panel.add(label);
panel.add(button);
panel.add(comb);
//
System.out.println("before visible, out of EDT ---> "
+ (bol = pop.isLightweight()));
System.out.println("before visible, out of EDT ---> "
+ (bol = pop.isLightweightComponent(comb)));
pop.setLightWeightPopupEnabled(true);
pop.add(comb);
System.out.println("before visible, out of EDT ---> "
+ (bol = comb.isLightweight()));
System.out.println("before visible, out of EDT ---> "
+ (bol = JComponent.isLightweightComponent(comb)));
System.out.println("before visible, out of EDT ---> "
+ (bol = button.isLightweight()));
System.out.println("before visible, out of EDT ---> "
+ (bol = JComponent.isLightweightComponent(button)));
System.out.println("before visible, out of EDT ---> "
+ (bol = label.isLightweight()));
System.out.println("before visible, out of EDT ---> "
+ (bol = JComponent.isLightweightComponent(label)));
//
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
pop.show(e.getComponent(), e.getX(), e.getY());
bol = false;
System.out.println("JComponents are visible, on EDT ---> "
+ (bol = pop.isLightweight()));
System.out.println("JComponents are visible, on EDT ---> "
+ (bol = pop.isLightweightComponent(comb)));
System.out.println("JComponents are visible, on EDT ---> "
+ (bol = comb.isLightweight()));
System.out.println("JComponents are visible, on EDT ---> "
+ (bol = JComponent.isLightweightComponent(comb)));
System.out.println("JComponents are visible, on EDT ---> "
+ (bol = button.isLightweight()));
System.out.println("JComponents are visible, on EDT ---> "
+ (bol = JComponent.isLightweightComponent(button)));
System.out.println("JComponents are visible, on EDT ---> "
+ (bol = label.isLightweight()));
System.out.println("JComponents are visible, on EDT ---> "
+ (bol = JComponent.isLightweightComponent(label)));
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test();
}
});
}
}
帶輸出
run:
before visible, out of EDT ---> false
before visible, out of EDT ---> false
before visible, out of EDT ---> false
before visible, out of EDT ---> false
before visible, out of EDT ---> false
before visible, out of EDT ---> false
before visible, out of EDT ---> false
before visible, out of EDT ---> false
mousePressed
JComponents are visible, on EDT ---> true
JComponents are visible, on EDT ---> true
JComponents are visible, on EDT ---> true
JComponents are visible, on EDT ---> true
JComponents are visible, on EDT ---> true
JComponents are visible, on EDT ---> true
JComponents are visible, on EDT ---> true
JComponents are visible, on EDT ---> true
BUILD SUCCESSFUL (total time: 7 seconds)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.