gpt4 book ai didi

Java Swing - 在 Motif L&F 的自定义组合框渲染器中消失文本

转载 作者:行者123 更新时间:2023-11-30 09:18:19 25 4
gpt4 key购买 nike

我目前正在为 JComboBox 编写自定义 ListCellRenderer。为此,我使用一个系统在 L&F 更改时获取新的渲染器,并将方法委托(delegate)给它。这适用于所有 L&F。但是,当我将此组件放在面板中时(这样我可以添加更多组件),它在 Metal 和 Windows L&F 中运行良好,但 JComboBox 文本在 Motif 中消失了。看截图和代码:

Problem with the third renderer

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class TextDemo extends JPanel implements ActionListener {

private static JFrame frame;

public TextDemo() {
super(new GridBagLayout());

JComboBox correct = new JComboBox(new String[]{"One", "Two", "Three"});

JComboBox alsoCorrect = new JComboBox(new String[]{"One", "Two", "Three"});
alsoCorrect.setRenderer(new MyRenderer());

JComboBox incorrect = new JComboBox(new String[]{"One", "Two", "Three"});
incorrect.setRenderer(new NotWorkingRenderer());

JButton button = new JButton("Change LnF");
button.addActionListener(this);

add(correct, getConstraints(0));
add(alsoCorrect, getConstraints(1));
add(incorrect, getConstraints(2));
add(button, getConstraints(3));
}

private GridBagConstraints getConstraints(int y) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; c.gridy = y;
c.insets = new Insets(4,8,4,8);
c.weightx = 1.0; c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.FIRST_LINE_START;
return c;
}

@Override
public void actionPerformed(ActionEvent ev) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception ex) {
ex.printStackTrace();
}
}

private static void createAndShowGUI() {
frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TextDemo());
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

class MyRenderer implements ListCellRenderer {
protected static ListCellRenderer delegate;
static {
refreshRenderers();
UIManager.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("lookAndFeel")) {
refreshRenderers();
}
}
});
}
protected static void refreshRenderers() {
delegate = new JComboBox().getRenderer();
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
return delegate.getListCellRendererComponent(list, value,
index, isSelected, cellHasFocus);
}
}

class NotWorkingRenderer extends MyRenderer {
private JPanel panel = new JPanel();
public NotWorkingRenderer() {
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
c.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
panel.removeAll();
panel.add(c);
return panel;
}
}

如果能帮助理解为什么会发生这种情况,我们将不胜感激!

最佳答案

不是答案,

  • 但看看会发生什么,使用 JPanel 作为 JComboBox 的渲染器 JComponents

  • 您确定带有 String 值的 JPanel 是正确的方法吗,请问目标是什么,

  • 用默认的JLabel, (J)Component代替JPanel有没有一样的效果

enter image description here enter image description here enter image description here

来自代码

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;

public class TextDemo extends JPanel implements ActionListener {

private static JFrame frame;

public TextDemo() {
super(new GridBagLayout());
JComboBox correct = new JComboBox(new String[]{"One", "Two", "Three"});
JComboBox alsoCorrect = new JComboBox(new String[]{"One", "Two", "Three"});
alsoCorrect.setRenderer(new MyRenderer());
JComboBox incorrect = new JComboBox(new String[]{"One", "Two", "Three"});
incorrect.setRenderer(new NotWorkingRenderer());
JButton button = new JButton("Change LnF");
button.addActionListener(this);
add(incorrect, getConstraints(0));
add(correct, getConstraints(1));
add(alsoCorrect, getConstraints(2));
add(button, getConstraints(3));
}

private GridBagConstraints getConstraints(int y) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = y;
c.insets = new Insets(4, 8, 4, 8);
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.FIRST_LINE_START;
return c;
}

@Override
public void actionPerformed(ActionEvent ev) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception ex) {
ex.printStackTrace();
}
}

private static void createAndShowGUI() {
frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TextDemo());
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
//SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception ex) {
ex.printStackTrace();
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}

class MyRenderer implements ListCellRenderer {

protected static ListCellRenderer delegate;

static {
refreshRenderers();
UIManager.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("lookAndFeel")) {
refreshRenderers();
}
}
});
}

protected static void refreshRenderers() {
delegate = new JComboBox().getRenderer();
}

@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
return delegate.getListCellRendererComponent(list, value,
index, isSelected, cellHasFocus);
}
}

class NotWorkingRenderer extends MyRenderer {

private JPanel panel = new JPanel();

public NotWorkingRenderer() {
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
}

@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
c.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
panel.add(c);
return panel;
}
}

EDIT_1st。

  • 除 ModifL&F 外的所有标准 L&F 均正确显示

  • 向前一步???,代码行incorrect.setEditable(true);生成

enter image description here

  • 我不知道 ModifL&F 和不可编辑的 JComboBox 的正确方法

EDIT_2nd。

  • 我是盲人 class NotWorkingRenderer extends MyRenderer {, pip... pip... pip...

  • phaaa 我从未参与过 add/remove/modify JComponentXxx(Xxx)Renderer 中,但是关于是否可能,

enter image description here

class NotWorkingRenderer extends BasicComboBoxRenderer {

private JPanel panel = new JPanel();

public NotWorkingRenderer() {
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
//panel.setOpaque(false);
}

@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel c = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
c.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
panel.removeAll();
panel.add(c);
panel.revalidate();
panel.repaint();
return panel;
}
}

关于Java Swing - 在 Motif L&F 的自定义组合框渲染器中消失文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18536082/

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