gpt4 book ai didi

java - JTextPane 和前景色

转载 作者:行者123 更新时间:2023-12-02 05:59:39 25 4
gpt4 key购买 nike

希望有一个 JTextPane,其内容始终可以由用户选择。因此,我创建了自己的 JTextPane 子类,并始终在方法“isEnabled()”中返回 true。另外,我引入了一个新成员 m_enabled,它负责返回正确的前景色(启用/禁用)。

它按预期工作,但如果我设置默认前景色(例如 Color.RED)并在启用和禁用之间切换,前景色将不再是红色。

你能帮我解决这个问题吗?

public class StylesExample1 {
public static final String text = "Lorem ipsum dolor...";
public static boolean m_enabled = true;

public static void main(String[] args) throws BadLocationException {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {
}

JFrame f = new JFrame("Frame");
final JTextPane pane = new MyPane();
pane.setText(text);

pane.setPreferredSize(new Dimension(200, 200));
f.getContentPane().add(pane);
JButton b = new JButton("Toggle Enabled state");
b.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
m_enabled = !m_enabled;
System.err.println("setting textpane enabled to " + m_enabled);
pane.setEnabled(m_enabled);
}
});

pane.setForeground(Color.red);

f.getContentPane().add(b);
f.getContentPane().setLayout(new FlowLayout());
f.setSize(400, 300);
f.setVisible(true);
}
}

class MyPane extends JTextPane {

private static final long serialVersionUID = 1L;
private boolean m_enabled = true;
private Color defaultForegroundColor, disabledTextColor;

public MyPane() {
defaultForegroundColor = getForeground();
disabledTextColor = getDisabledTextColor();
}

public void setEnabled(boolean enabled) {
m_enabled = enabled;

if (m_enabled) {
setForeground(defaultForegroundColor);
} else {
setForeground(disabledTextColor);
}
}

@Override
public boolean isEnabled() {
return true;
}
}

最佳答案

  • 您的类 MyPane 扩展 JTextPane 不会覆盖 WindowsLookAndFeel 所需的细节和正确方法,

  • DeafultHightLigter/Painter/Caret/Selection 可能存在其他问题

  • (我建议)在 Windows(Classic)LookAndFeel 的 UIManager 中使用并覆盖键

  • 编辑 UIManager by @camickr 中的键列表

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;

public class StylesExample1 {

private final String text = "Lorem ipsum dolor...";
private boolean m_enabled = true;

public StylesExample1() {
final JTextPane pane = new MyPane();
pane.setText(text);
pane.setPreferredSize(new Dimension(200, 200));
JButton b = new JButton("Toggle Enabled state");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
m_enabled = !m_enabled;
System.err.println("setting textpane enabled to " + m_enabled);
pane.setEnabled(m_enabled);
}
});
pane.setDisabledTextColor(Color.red);
pane.setForeground(Color.red);
JFrame f = new JFrame("Frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(pane);
f.add(b);
f.getContentPane().setLayout(new FlowLayout());
f.setSize(400, 300);
f.setVisible(true);
}

public static void main(String[] args) throws BadLocationException {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new StylesExample1();
}
});
}
}

class MyPane extends JTextPane {

private static final long serialVersionUID = 1L;
private boolean m_enabled = true;
private Color defaultForegroundColor, disabledTextColor;

public MyPane() {
defaultForegroundColor = getForeground();
disabledTextColor = getDisabledTextColor();
}

@Override
public void setEnabled(boolean enabled) {
m_enabled = enabled;
if (m_enabled) {
UIManager.put("TextPane.disabledBackground", Color.RED);
UIManager.put("TextPane.foreground", Color.RED);
UIManager.put("TextPane.inactiveForeground", Color.RED);
SwingUtilities.updateComponentTreeUI(this); // don't to use this
} else {
UIManager.put("TextPane.disabledBackground", Color.RED);
UIManager.put("TextPane.foreground", Color.RED);
UIManager.put("TextPane.inactiveForeground", Color.RED);
SwingUtilities.updateComponentTreeUI(this); // don't to use this
}
}

@Override
public boolean isEnabled() {
return true;
}
}

关于java - JTextPane 和前景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22782483/

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