JTextArea比较详细的使用

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.Graphics;

import java.awt.Image;

import javax.swing.GrayFilter;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

/***

* 给JTextArea添加背景色

* @author Administrator

*

*/

public class BackgroundSample {

public static void main(String args[]) {

JFrame frame = new JFrame("Background Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final ImageIcon imageIcon = new ImageIcon("draft.gif");

JTextArea textArea = new JTextArea() {

Image image = imageIcon.getImage();

Image grayImage = GrayFilter.createDisabledImage(image);

{

setOpaque(false);

}

public void paint(Graphics g) {

g.drawImage(grayImage, 0, 0, this);

super.paint(g);

}

};

JScrollPane scrollPane = new JScrollPane(textArea);

Container content = frame.getContentPane();

content.add(scrollPane, BorderLayout.CENTER);

frame.setSize(250, 250);

frame.setVisible(true);

}

}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* 处理JTextArea的换行操作
* @author Administrator
*
*/
public class TextAreaFrame extends JFrame implements ActionListener{
private JButton insertButton = new JButton("Insert");
private JButton wrapButton = new JButton("Wrap");
private JButton noWrapButton = new JButton("No wrap");
private JTextArea textArea = new JTextArea(8, 40);
private JScrollPane scrollPane = new JScrollPane(textArea);
public TextAreaFrame() {
JPanel p = new JPanel();
p.add(insertButton);
insertButton.addActionListener(this);
p.add(wrapButton);
wrapButton.addActionListener(this);
p.add(noWrapButton);
noWrapButton.addActionListener(this);
getContentPane().add(p, "South");
getContentPane().add(scrollPane, "Center");
setTitle("TextAreaTest");
setSize(300, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == insertButton)
textArea
.append("textArea The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.");
else if (source == wrapButton) {
//常用方法有对两个方法介绍
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
scrollPane.validate();
} else if (source == noWrapButton) {
textArea.setLineWrap(false);
textArea.setWrapStyleWord(false);
scrollPane.validate();
}
}
public static void main(String[] args) {
JFrame f = new TextAreaFrame();
f.show();
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.util.Hashtable;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.DefaultEditorKit;
/**
* 使用按钮做剪切、复制、粘贴操作
* @author Administrator
*
*/
public class CutPasteSample {
public static void main(String args[]) {
JFrame frame = new JFrame("Cut/Paste Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = frame.getContentPane();
JTextField textField = new JTextField();
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
content.add(textField, BorderLayout.NORTH);
content.add(scrollPane, BorderLayout.CENTER);
Action actions[] = textField.getActions();
Action cutAction = TextUtilities.findAction(actions,
DefaultEditorKit.cutAction);
Action copyAction = TextUtilities.findAction(actions,
DefaultEditorKit.copyAction);
Action pasteAction = TextUtilities.findAction(actions,
DefaultEditorKit.pasteAction);
JPanel panel = new JPanel();
content.add(panel, BorderLayout.SOUTH);
JButton cutButton = new JButton(cutAction);
cutButton.setText("Cut");
panel.add(cutButton);
JButton copyButton = new JButton(copyAction);
copyButton.setText("Copy");
panel.add(copyButton);
JButton pasteButton = new JButton(pasteAction);
pasteButton.setText("Paste");
panel.add(pasteButton);
frame.setSize(250, 250);
frame.setVisible(true);
}
}
class TextUtilities {
private TextUtilities() {
}
public static Action findAction(Action actions[], String key) {
Hashtable commands = new Hashtable();
for (int i = 0; i < actions.length; i++) {
Action action = actions[i];
commands.put(action.getValue(Action.NAME), action);
}
return (Action) commands.get(key);
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值