希望拥有一个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;
}
}
答案 0 :(得分:2)
您的班级MyPane extends JTextPane
不会覆盖WindowsLookAndFeel
所需的具体方法和正确方法,
DeafultHightLigter/Painter/Caret/Selection
(我建议)在UIManager中使用和覆盖键Windows(Classic)LookAndFeel
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;
}
}
答案 1 :(得分:0)
在“MyPane”的构造函数中,您将默认前景色设置为JTextPane的默认前景,但是在构造函数之后调用setForeground(Color.red)。问题是MyPane中的“defaultForegroundColor”成员不知道它是红色的。进行以下调整:
public MyPane(Color defaultForegroundColor)
{
this.defaultForegroundColor = defaultForegroundColor;
this.disabledTextColor = getDisabledTextColor();
}
并使用以下代码创建它:
final JTextPane pane = new MyPane(Color.red);
如果要在施工后更改前景,请执行以下操作:
public void setNewForeground(Color c)
{
this.defaultForegroundColor = c;
}
答案 2 :(得分:0)
希望拥有一个JTextPane,其内容始终可由用户选择。
然后我会用:
setEditable( false );
而不是
setEnabled( false );