文本域
-
文本域允许用户在其中输入多行文本并进行编辑
-
JTextArea txtArea=new JTextArea();
-
可以在创建时指定文本域的行数和列数
-
当输入的文本到达列边界后,不会自动换行;虽然可以继续输入文本,但是超出显示范围的部分将变得不可见。可以有两种方法解决换行问题:
-
一种是可以使用文本域对象的setLineWrap(true)方法来将文本域设置为自动换行;
-
另一种是在快要到达显示边界时,按下Enter键进行硬换行
如果想让文本域提供滚动条JScrollPane,则必须把该文本域放到一个滚动面板中,再把滚动面板放到内容面板中,给文本域加上滚动条:
-
JTextArea txtArea=new JTextArea(); JScrollPane scroll=new JScrollPane(txtArea);
-
copy 将选中的文本复制到剪切板 例:txtArea.copy (); 6、cut 将选中的文本剪切到剪切板 例:txtArea.cut (); 7、paste 粘贴 例:txtArea.paste (); 8、setText() 文本域里面默认值 txtArea.setText("abc");
-
-
获取文本域中的文本String str=txtArea. getText();
-
10、 selectAll(),选项中所有文本
-
11、requestFocus(),为控件获取焦点,即置光标
-
12、setSelectionStart(int i),设置文本选择起点。 getSelectionStart(),获得选择文本的起点,没选择文本的情况下光标的位置。
-
13、setSelectionEnd(i),设置文本选择终点。 getSelectionEnd(),获得文本选择终点。
-
14,setFont(Font f);设置字体 txtArea.setFont(new Font(“宋体”, Font.BOLD,12));
-
15、setForeground(Color c),设置组件的前景色。
-
setForeground(Color.red);
-
16、setBackground(Color c),设置此文本组件的背景色。 setForeground(Color.red);
JButton按钮
-
-
6、setMnemonic()方法可以为按钮设置快捷键
-
setEnabled()改变按钮的状态
-
btn1.setEnabled(false);//禁用btn1
-
btn1.setEnabled(true);//启用btn1
-
btn1.setMnemonic(KeyEvent.VK_S); //设置btn1的快捷键为s(alt+s)**
package src;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Etxta extends JFrame
{
JTextArea txta=null;
JPanel p=null;
JScrollPane s=null;
JButton btncut=null;
JButton btncopy=null;
JButton btnpaste=null;
JButton btnsall=null;
JButton btndel=null;
JButton btnclear=null;
JButton btnbold=null;
JButton btnita=null;
JButton btncolor=null;
JButton btnexit=null;
JLabel labname =null;
JTextField txtf1=null;
JButton btnfname=null;
JLabel labsize =null;
JTextField txtf2=null;
JButton btnfsize=null;
public Etxta()
{
init();
}
public void init()
{
txta=new JTextArea(10,48);
p=new JPanel();
s=new JScrollPane(txta);
btncut=new JButton("剪切");
btncopy=new JButton("复制");
btnpaste=new JButton("粘贴");
btnsall=new JButton("全选");
btndel=new JButton("删除");
btnclear=new JButton("清空");
btnbold