gpt4 book ai didi

java - PLAF 更改为 Motif 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 23:51:40 24 4
gpt4 key购买 nike

任何人都可以看到我做了什么导致了NullPointerException(下面的堆栈跟踪)吗?

面板显示正常:

Visually working layout

文本输出

os.name:    Windows Vista
os.version: 6.1
java.version: 1.6.0
java.vm.version: 1.6.0-b105
java.vendor: Sun Microsystems Inc.

那个(1.6.0-b105)很古老,它是这里Netbeans的默认运行时环境。必须更新到版本 7 JRE。

代码

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.GroupLayout.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.EmptyBorder;

class GroupLayoutHelper {

/**
* Provides a JPanel with two columns (labels & fields) laid out using
* GroupLayout. The arrays must be of equal size.
*
* @param labels The first column contains labels.
* @param fields The last column contains fields.
* @param addMnemonics Add mnemonic by next available letter in label text.
* @return JComponent A JPanel with two columns of the components provided.
*/
public static JComponent getTwoColumnLayout(
JLabel[] labels,
JComponent[] fields,
boolean addMnemonics) {
JComponent panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
// Turn on automatically adding gaps between components
layout.setAutoCreateGaps(true);
// Create a sequential group for the horizontal axis.
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
Group yLabelGroup = layout.createParallelGroup(Alignment.TRAILING);
hGroup.addGroup(yLabelGroup);
Group yFieldGroup = layout.createParallelGroup();
hGroup.addGroup(yFieldGroup);
layout.setHorizontalGroup(hGroup);
// Create a sequential group for the vertical axis.
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
layout.setVerticalGroup(vGroup);

int p = GroupLayout.PREFERRED_SIZE;
// add the components to the groups
for (JLabel label : labels) {
yLabelGroup.addComponent(label);
}
for (Component field : fields) {
yFieldGroup.addComponent(field, p, p, p);
}
for (int ii = 0; ii < labels.length; ii++) {
vGroup.addGroup(layout.createParallelGroup().
addComponent(labels[ii]).
addComponent(fields[ii], p, p, p));
}

if (addMnemonics) {
Map<Character, Object> m = new HashMap<Character, Object>();
for (int ii = 0; ii < labels.length; ii++) {
labels[ii].setLabelFor(fields[ii]);
String lwr = labels[ii].getText().toLowerCase();
for (int jj = 0; jj < lwr.length(); jj++) {
char ch = lwr.charAt(jj);
if (m.get(ch) == null) {
m.put(ch, ch);
labels[ii].setDisplayedMnemonic(ch);
break;
}
}
}
}

return panel;
}

/**
* Provides a JPanel with two columns (labels & fields) laid out using
* GroupLayout. The arrays must be of equal size.
*
* @param labelStrings Strings that will be used for labels.
* @param fields The corresponding fields.
* @return JComponent A JPanel with two columns of the components provided.
*/
public static JComponent getTwoColumnLayout(
String[] labelStrings,
JComponent[] fields) {
JLabel[] labels = new JLabel[labelStrings.length];
for (int ii = 0; ii < labels.length; ii++) {
labels[ii] = new JLabel(labelStrings[ii]);
}
return getTwoColumnLayout(labels, fields);
}

/**
* Provides a JPanel with two columns (labels & fields) laid out using
* GroupLayout. The arrays must be of equal size.
*
* @param labels The first column contains labels.
* @param fields The last column contains fields.
* @return JComponent A JPanel with two columns of the components provided.
*/
public static JComponent getTwoColumnLayout(
JLabel[] labels,
JComponent[] fields) {
return getTwoColumnLayout(labels, fields, true);
}

public static void main(String[] args) {
Runnable r = new Runnable() {

@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
final JComboBox plafCombo = new JComboBox(
UIManager.getInstalledLookAndFeels());
plafCombo.setRenderer(new DefaultListCellRenderer() {

@Override
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
if (c instanceof JLabel) {
JLabel l = (JLabel) c;
String s = value.toString();
s = s.substring(
s.lastIndexOf(".") + 1, s.length() - 12);
l.setText(s);
return l;
}
return c;
}
});
plafCombo.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {
Object o = plafCombo.getSelectedItem();
LookAndFeelInfo plafi = (LookAndFeelInfo) o;
try {
UIManager.setLookAndFeel(plafi.getClassName());
Container c = plafCombo.getTopLevelAncestor();
SwingUtilities.updateComponentTreeUI(c);
if (c instanceof Window) {
Window w = (Window) c;
w.pack();
}
} catch (Exception ignoreAndContinue) {
}
}
});
String longText = getProperty("os.name") +
getProperty("os.version") +
getProperty("java.version") +
getProperty("java.vm.version") +
getProperty("java.vendor") +
"";
JComponent[] c = {
new JTextField(5),
new JTextField(20),
plafCombo,
new JPasswordField(8),
new JScrollPane(new JTextArea(longText, 5, 38))
};

String[] ls = {"ID", "Name", "Look & Feel", "Password", "Postcript"};
JLabel[] l = new JLabel[ls.length];
JComponent gui = getTwoColumnLayout(ls, c);

gui.setBorder(new EmptyBorder(4, 8, 4, 8));

JFrame f = new JFrame("Groupie");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);

// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};

// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}

public static String getProperty(String name) {
return name + ": \t" +
System.getProperty(name) +
System.getProperty("line.separator");
}
}

堆栈跟踪

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicComboBoxUI.selectNextPossibleValue(BasicComboBoxUI.java:1072)
at javax.swing.plaf.basic.BasicComboBoxUI$Actions.actionPerformed(BasicComboBoxUI.java:1450)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2844)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2890)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2807)
at javax.swing.JComboBox.processKeyEvent(JComboBox.java:1399)
at java.awt.Component.processEvent(Component.java:5815)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:693)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:958)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:830)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:657)
at java.awt.Component.dispatchEventImpl(Component.java:4282)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicComboBoxUI.selectNextPossibleValue(BasicComboBoxUI.java:1072)
at javax.swing.plaf.basic.BasicComboBoxUI$Actions.actionPerformed(BasicComboBoxUI.java:1432)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2844)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2890)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2807)
at javax.swing.JComboBox.processKeyEvent(JComboBox.java:1399)
at java.awt.Component.processEvent(Component.java:5815)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:693)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:958)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:830)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:657)
at java.awt.Component.dispatchEventImpl(Component.java:4282)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

最佳答案

  • 我在 1 秒内添加了所有 L&F 的 Swing Timer 30 次。期间,没问题,

Win8(64b 企业版)

  • jdk1.6.0_22

enter image description here

  • jdk1.7.0_11

enter image description here

<小时/>

WinXP

  • jdk1.6.0_22

enter image description here

<小时/>

Win7(64b 企业版)

  • jdk1.7.0_11(注意渲染,刷新太懒,无法为 SwingTimer 使用 1 秒周期)

enter image description here

  • jdk1.6.0_22(L&F切换后渲染没有问题,不明白,JDK7和JDK6两个窗口同时测试了三次,phaaa我需要安装最后一个JDK7(不知道版本): -)

enter image description here

  • jdk1.7.0_21 渲染没有问题。与另一个WinOS版本上的另一个JDK相同,奇怪的是这个jdk1.7.0_11,问题仍然存在,可重现,我多么讨厌在jdk1.7.0_11上编译的Win7,Swing,L&F

关于java - PLAF 更改为 Motif 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16233512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com