簡體   English   中英

使用反射在運行時更改JLabel的背景

[英]Change background of JLabel in runtime using reflection

我需要動態更改JLabels的背景。

我班上有70個JLabel。 所有JLabel代表一些特定項目。 項名稱與JLabel的變量相同。 已售商品名稱保存在數據庫中。 如果我運行一個將返回已售商品數組的查詢。 與JLabel相同的已售商品應更改背景。 休息不會改變。

我有這樣的所有字段的變量:

  Field fld[] = BlueLine.class.getDeclaredFields();  
   for (int i = 0; i < fld.length; i++)  
   {
   System.out.println("Variable Name is : " + fld[i].getName());
   }

當特定情況遇到時,如何將我的fld轉換為JLabel並更改JLabel背景? 例如:

   if(fld[i] == label5){
   label5.setBackground.(Color.red);
   } // or something like this. ?

任何大綱都會有幫助。

目前,您只是查看字段本身 - 您對這些字段的感興趣。 例如:

Object value = fld[i].get(target); // Or null for static fields
if (value == label5) {
    ...
}

這里target是對要從其獲取值的字段的對象的引用。 對於靜態字段,請根據注釋使用null

然而,並不是很清楚所有這些都是一個好主意 - 可以用反射解決的問題通常可以用不同的方式更好地解決。 我們目前沒有足夠的背景建議您具體細節,但我建議您至少嘗試考慮更清潔的設計。

嘗試使用Jcomponent.putClientProperty()Jcomponent.getClientProperty()

要遵循的步驟:

  • 首先將JLabel的名稱設置為與其變量名稱相同
  • 將它作為JPanel客戶端屬性添加JLabel
  • 拿回來使用客戶財產JPanel使用的名稱JLabel

注意:您可以使用問題中定義的Field.getName()來訪問它。

示例代碼:

    final JFrame frame = new JFrame();
    final JPanel panel = new JPanel();
    panel.addContainerListener(new ContainerListener() {

        @Override
        public void componentRemoved(ContainerEvent e) {
            String name = e.getChild().getName();
            if (name != null) {
                System.out.println(name + " removed");
                panel.putClientProperty(name, null);
            }
        }

        @Override
        public void componentAdded(ContainerEvent e) {
            String name = e.getChild().getName();
            if (name != null) {
                System.out.println(name + " added");
                panel.putClientProperty(name, e.getChild());
            }
        }
    });

    MyLabels myLabels = new MyLabels();
    panel.add(myLabels.getProduct1());
    panel.add(myLabels.getProduct2());
    panel.add(myLabels.getProduct3());

    JButton btn = new JButton("Product1 and Product3 are sold");
    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String[] soldItems = new String[] { "Product1", "Product3" };

            for (String soldItem : soldItems) {
                Object obj = panel.getClientProperty(soldItem);
                if (obj instanceof JLabel) {
                    ((JLabel) obj).setForeground(Color.RED);
                }
            }
        }
    });
    panel.add(btn);

    frame.add(panel);
    frame.setSize(400, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

MyLabels.java:

class MyLabels {
    private JLabel Product1;
    private JLabel Product2;
    private JLabel Product3;

    public MyLabels() {
        Product1 = new JLabel("Product1");
        Product1.setName(Product1.getText());

        Product2 = new JLabel("Product2");
        Product2.setName(Product2.getText());

        Product3 = new JLabel("Product3");
        Product3.setName(Product3.getText());
    }

    public JLabel getProduct1() {
        return Product1;
    }

    public void setProduct1(JLabel product1) {
        Product1 = product1;
    }

    public JLabel getProduct2() {
        return Product2;
    }

    public void setProduct2(JLabel product2) {
        Product2 = product2;
    }

    public JLabel getProduct3() {
        return Product3;
    }

    public void setProduct3(JLabel product3) {
        Product3 = product3;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM