jTextPane1.selectAll();
使用正确共享的事件,该命令允许突出显示JTextPane
区域中的文本(我有点生疏,我不要忘记分享"良好的事件焦点优先级" ;谢谢你:MadProgrammer)
答案 0 :(得分:4)
由于selectAll
是JTextComponent
的一种方法,JTextPane
从{I}延伸,我会猜测并说,可能是的。
五分钟的编码可能会让你自己得到同样的答案......
突出显示似乎没有出现在jTextPane区域(注意:我使用的是Java 7)
这可能是因为JTextPane
没有焦点,请尝试使用requestFocusInWindow
将键盘焦点恢复到JTextPane
。
JTextComponent
并不总是在他们没有焦点时呈现选择突出显示。
例如......
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestTextPane {
public static void main(String[] args) {
new TestTextPane();
}
public TestTextPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JTextPane tp = new JTextPane();
JButton withFocus = new JButton("Select with focus");
withFocus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tp.selectAll();
tp.requestFocus();
}
});
JButton withOutFocus = new JButton("Select without focus");
withFocus.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tp.selectAll();
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(tp));
JPanel panel = new JPanel();
panel.add(withFocus);
panel.add(withOutFocus);
frame.add(panel, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
您也可以使用
进行测试textPane.selectAll();
System.out.println(textPane.getSelectedText());
例如......
现在双击
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestTextPane {
public static void main(String[] args) {
new TestTextPane();
}
public TestTextPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JTextPane tp = new JTextPane();
JButton withFocus = new JButton("Select with focus");
tp.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
tp.selectAll();
}
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(tp));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}