1. JTextComponent的方法: public String getSelectedText() Returns the selected text contained in this TextComponent. If the selection is null or the document empty, returns null. Returns: the text Throws: IllegalArgumentException - if the selection doesn't have a valid mapping into the document for some reason2. JTextComponent: public void select(int selectionStart, int selectionEnd) Selects the text between the specified start and end positions.3.有剪贴板类:java.awt.datatransfer.Clipboard在Java中要从剪贴板中取得其中的内容也可以,不过这个有些麻烦.
JTextComponent的方法:
public String getSelectedText()
Returns the selected text contained in this TextComponent. If the selection is null or the document empty, returns null. Returns:
the text
Throws:
IllegalArgumentException - if the selection doesn't have a valid mapping into the document for some reason2.
JTextComponent:
public void select(int selectionStart, int selectionEnd)
Selects the text between the specified start and end positions.3.有剪贴板类:java.awt.datatransfer.Clipboard在Java中要从剪贴板中取得其中的内容也可以,不过这个有些麻烦.
加入的前提是你别太沉默了,别下完资料就跑了
欢迎大家加入
希望版主别删,我只是希望有一个非常棒的交流环境~
我用select咋没用啊?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.beans.*;
import javax.swing.event.*;
import java.util.*;class HelloJava extends JPanel implements Runnable {
private JComboBox comboBox;
JTextArea textArea;
static int times = 0;
public HelloJava() {
textArea = new JTextArea(10, 10);
textArea.setLineWrap(true);
//textArea.setSelectedTextColor(new Color(255, 0, 0));
//textArea.setSelectionColor(new Color(0, 255, 0));
JScrollPane scroller = new JScrollPane();
scroller.getViewport().setView(textArea);
this.add(scroller);
AbstractAction action = new AbstractAction("Select") {
public void actionPerformed(ActionEvent ae) {
textArea.select(0, textArea.getText().length());
System.out.println(textArea.getSelectedText());// 输出测试是否有文本被选中.
textArea.requestFocus(); // 关键就在这里,让textArea获得焦点,高亮显示被选中文本.
}
};
JButton button = new JButton(action);
this.add(button);
}
public void addMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem exitMenuItem = new JMenuItem("Exit");
fileMenu.add(exitMenuItem);
JFrame frame = (JFrame)SwingUtilities.getRoot(this);
frame.setJMenuBar(menuBar);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
GradientPaint gradient = new GradientPaint(0, 0, Color.BLACK,
0, this.getHeight(), Color.WHITE, true);
g2d.setPaint(gradient);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
}
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch(InterruptedException ie) {
}
}
}
private static void createUIAndShow() {
JFrame frame = new JFrame("Swing Example");
//JWindow frame = new JWindow();
frame.setUndecorated(false);
int width = 400;
int height = 400;
frame.setSize(width, height);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((int)((screenSize.getWidth() - width) / 2),
(int)((screenSize.getHeight() - height) / 2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setUndecorated(true);
//frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
HelloJava panel = new HelloJava();
frame.getContentPane().add(panel);
panel.addMenuBar();
Thread thread = new Thread((HelloJava)panel);
//thread.start();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
HelloJava.createUIAndShow();
}
});
/*GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontsFamily = genv.getAvailableFontFamilyNames();
for (int i = 0; i < fontsFamily.length; i++) {
System.out.println(fontsFamily[i]);
}*/
}}