更改JLabel前景以获取多个标签

时间:2015-01-21 21:53:00

标签: java arrays swing colors jlabel

我有点试图改变我的一些JLabel的前景色。

我有42个不同的项目,我可以在我的应用程序中激活。每个项目都有自己的JLabel。我做了一个布尔数组来检查项目是否被激活,另一个String数组来检查列表中的项目位置。

现在问题是每个项目都有自己的JLabel,我想改变它的颜色。 JLabel称为char0,char1,...,char41。我试图使用地点编号来获取特定标签,但遗憾的是,这不起作用,所以我为我的标签实现了一个数组。

import javax.swing.JLabel;    
JLabel[] label = setLabel();    
    public JLabel[] setLabel(){
        JLabel[] labels = {char0,char1,...,char41};
        return labels;
    }

所以基本上我得到了一个JLabel类型的数组,它应该包含我的42个标签。现在让他们回来改变颜色,这就是我尝试过的:

getLabel(place).setForeground(Color.green);
    public JLabel getLabel(int place){
        return label[place];
    }

可悲的是,我得到了这个例外:

"AWT-EventQueue-0" java.lang.NullPointerException
at werwolf.GUI.setCharacter(GUI.java:1068)
at werwolf.GUI.jButton1MouseClicked(GUI.java:1040)
at werwolf.GUI.access$100(GUI.java:17)
at werwolf.GUI$2.mouseClicked(GUI.java:199)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6528)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

导致此错误的原因是什么?如何解决?

1 个答案:

答案 0 :(得分:1)

我发现这里的问题可能是我改变标签颜色的实现方式。您需要将标签设置为opaque true。

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;    
public class Main
{
    JFrame myframe = new JFrame("labelframe");
    Container contents = myframe.getContentPane();
    JLabel[] label = setLabel();  
    JButton change = new JButton("change color");
    public Main()
    {
        contents.setLayout(new GridLayout(5,9));
        for(JLabel thislabel : label)
        {
            thislabel.setEnabled(true);
            thislabel.setOpaque(true);
            contents.add(thislabel);            
        }
        change.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)
            {
               changeLabelColor();
            }
        });     
        contents.add(change);
        myframe.setVisible(true);
        myframe.pack();
    }
     private void changeLabelColor()
     {
         for(JLabel thislabel : label)
         {

             thislabel.setBackground(Color.red);
             thislabel.setForeground(Color.GREEN);
         }
     }
    public JLabel[] setLabel()
    {
        JLabel[] labels = new JLabel[42];
        for(int x = 0; x < labels.length; x++)
        {
            labels[x] = new JLabel("char" + x);
        }
        return labels;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new Main();
    }