Java - 在外部函数中更改 JLabel Frontcolor
Java - Changing JLabel Frontcolor in extern function
作为 Java 的初学者,我尝试在 windowsbuilder(eclipse 插件)的帮助下创建一个 Window 应用程序 "Game"。
现在,当我尝试更改外部函数 (allblack()) 中的标签时,当我按下特定按钮时,控制台发出以下错误(window 正确显示):
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Leiterspiel.allblack(Leiterspiel.java:81)
at Leiterspiel.mouseClicked(Leiterspiel.java:234)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Leiterspiel.allblack(Leiterspiel.java:81)
at Leiterspiel.mouseClicked(Leiterspiel.java:234)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
如果有人能向我解释它,因为 "allblack()" 中的相同代码在 "inizialize()" 中有效,我将不胜感激。
谢谢
[Pre-sampled import]
public void allblack()
{
l1.setForeground(Color.black);
l2.setForeground(Color.black);
}
public void initialize() {
frmDasLeiterspiel = new JFrame();
frmDasLeiterspiel.setResizable(false);
frmDasLeiterspiel.setTitle("Das Leiterspiel");
frmDasLeiterspiel.setBounds(100, 100, 420, 420);
frmDasLeiterspiel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmDasLeiterspiel.getContentPane().setLayout(null);
JLabel l1 = new JLabel("1");
l1.setBackground(Color.LIGHT_GRAY);
l1.setBounds(340, 190, 50, 50);
l1.setHorizontalAlignment(SwingConstants.CENTER);
frmDasLeiterspiel.getContentPane().add(l1, BorderLayout.CENTER);
JLabel l2 = new JLabel("2");
l2.setBackground(Color.LIGHT_GRAY);
l2.setHorizontalAlignment(SwingConstants.CENTER);
l2.setBounds(278, 190, 50, 50);
frmDasLeiterspiel.getContentPane().add(l2);
[....]
JButton btnNewButton = new JButton("Roll the dice!");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
allblack();
}
});
btnNewButton.setBounds(12, 294, 378, 25);
frmDasLeiterspiel.getContentPane().add(btnNewButton);
[...]
}
看起来变量l1和l2只存在于initialize方法中。
试试这个:
public void allblack() {
l1.setForeground(Color.black);
l2.setForeground(Color.black);
}
private JLabel l1,l2;
public void initialize() {
frmDasLeiterspiel = new JFrame();
frmDasLeiterspiel.setResizable(false);
frmDasLeiterspiel.setTitle("Das Leiterspiel");
frmDasLeiterspiel.setBounds(100, 100, 420, 420);
frmDasLeiterspiel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmDasLeiterspiel.getContentPane().setLayout(null);
l1 = new JLabel("1");
l1.setBackground(Color.LIGHT_GRAY);
l1.setBounds(340, 190, 50, 50);
l1.setHorizontalAlignment(SwingConstants.CENTER);
frmDasLeiterspiel.getContentPane().add(l1, BorderLayout.CENTER);
l2 = new JLabel("2");
l2.setBackground(Color.LIGHT_GRAY);
l2.setHorizontalAlignment(SwingConstants.CENTER);
l2.setBounds(278, 190, 50, 50);
frmDasLeiterspiel.getContentPane().add(l2);
[....]
JButton btnNewButton = new JButton("Roll the dice!");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
allblack();
}
});
btnNewButton.setBounds(12, 294, 378, 25);
frmDasLeiterspiel.getContentPane().add(btnNewButton);
[...]
}
作为 Java 的初学者,我尝试在 windowsbuilder(eclipse 插件)的帮助下创建一个 Window 应用程序 "Game"。 现在,当我尝试更改外部函数 (allblack()) 中的标签时,当我按下特定按钮时,控制台发出以下错误(window 正确显示):
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Leiterspiel.allblack(Leiterspiel.java:81)
at Leiterspiel.mouseClicked(Leiterspiel.java:234)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Leiterspiel.allblack(Leiterspiel.java:81)
at Leiterspiel.mouseClicked(Leiterspiel.java:234)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
如果有人能向我解释它,因为 "allblack()" 中的相同代码在 "inizialize()" 中有效,我将不胜感激。 谢谢
[Pre-sampled import]
public void allblack()
{
l1.setForeground(Color.black);
l2.setForeground(Color.black);
}
public void initialize() {
frmDasLeiterspiel = new JFrame();
frmDasLeiterspiel.setResizable(false);
frmDasLeiterspiel.setTitle("Das Leiterspiel");
frmDasLeiterspiel.setBounds(100, 100, 420, 420);
frmDasLeiterspiel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmDasLeiterspiel.getContentPane().setLayout(null);
JLabel l1 = new JLabel("1");
l1.setBackground(Color.LIGHT_GRAY);
l1.setBounds(340, 190, 50, 50);
l1.setHorizontalAlignment(SwingConstants.CENTER);
frmDasLeiterspiel.getContentPane().add(l1, BorderLayout.CENTER);
JLabel l2 = new JLabel("2");
l2.setBackground(Color.LIGHT_GRAY);
l2.setHorizontalAlignment(SwingConstants.CENTER);
l2.setBounds(278, 190, 50, 50);
frmDasLeiterspiel.getContentPane().add(l2);
[....]
JButton btnNewButton = new JButton("Roll the dice!");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
allblack();
}
});
btnNewButton.setBounds(12, 294, 378, 25);
frmDasLeiterspiel.getContentPane().add(btnNewButton);
[...]
}
看起来变量l1和l2只存在于initialize方法中。 试试这个:
public void allblack() {
l1.setForeground(Color.black);
l2.setForeground(Color.black);
}
private JLabel l1,l2;
public void initialize() {
frmDasLeiterspiel = new JFrame();
frmDasLeiterspiel.setResizable(false);
frmDasLeiterspiel.setTitle("Das Leiterspiel");
frmDasLeiterspiel.setBounds(100, 100, 420, 420);
frmDasLeiterspiel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmDasLeiterspiel.getContentPane().setLayout(null);
l1 = new JLabel("1");
l1.setBackground(Color.LIGHT_GRAY);
l1.setBounds(340, 190, 50, 50);
l1.setHorizontalAlignment(SwingConstants.CENTER);
frmDasLeiterspiel.getContentPane().add(l1, BorderLayout.CENTER);
l2 = new JLabel("2");
l2.setBackground(Color.LIGHT_GRAY);
l2.setHorizontalAlignment(SwingConstants.CENTER);
l2.setBounds(278, 190, 50, 50);
frmDasLeiterspiel.getContentPane().add(l2);
[....]
JButton btnNewButton = new JButton("Roll the dice!");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
allblack();
}
});
btnNewButton.setBounds(12, 294, 378, 25);
frmDasLeiterspiel.getContentPane().add(btnNewButton);
[...]
}