文本域

             文本域

  • 文本域允许用户在其中输入多行文本并进行编辑

  • 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按钮

  • 5、通过setEnabled()改变按钮的状态 btn1.setEnabled(false);//禁用btn1 btn1.setEnabled(true);//启用btn1

  • 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=new JButton("加粗");
btnita=new JButton("倾斜");;
btncolor=new JButton("红色");
btnexit=new JButton("退出");
labname =new JLabel("字体名:");
txtf1=new JTextField("宋体",10);
btnfname=new JButton("确定");
labsize =new JLabel("字号:");
txtf2=new JTextField("10",5);
btnfsize=new JButton("确定");
//txta.setBorder(BorderFactory.createLoweredBevelBorder());//设置边界
txta.setLineWrap(true);//设置换行
txta.setWrapStyleWord(true);//设置换行以单词而不是以字符为单位。
p.add(s);
p.add(btncut);
p.add(btncopy);
p.add(btnpaste);
p.add(btnsall);
p.add(btndel);
p.add(btnclear);
p.add(btnbold);
p.add(btnita);
p.add(btncolor);
p.add(btnexit);
p.add(labname);
p.add(txtf1);
p.add(btnfname);
p.add(labsize);
p.add(txtf2);
p.add(btnfsize);
btncut.addActionListener(new btnhandler());
btncopy.addActionListener(new btnhandler());
btnpaste.addActionListener(new btnhandler());
btnsall.addActionListener(new btnhandler());
btndel.addActionListener(new btnhandler());
btnclear.addActionListener(new btnhandler());
btnbold.addActionListener(new btnhandler());
btnita.addActionListener(new btnhandler());
btncolor.addActionListener(new btnhandler());
btnexit.addActionListener(new btnhandler());
btnfname.addActionListener(new btnhandler());
btnfsize.addActionListener(new btnhandler());
this.getContentPane().add(p);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(550,300);
this.setLocationRelativeTo(null);
//this.pack();
}
public static void main(String[] args)
{
Etxta fr=new Etxta();
fr.setTitle("简单记事本");
fr.setVisible(true);

}
class btnhandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btncopy)
{
txta.copy();
txta.requestFocus();
}
else if(e.getSource()==btncut)
{
txta.cut();
txta.requestFocus();
}
else if(e.getSource()==btnpaste)
{
txta.paste();
txta.requestFocus();
}
else if(e.getSource()==btnsall)
{
txta.selectAll();
txta.requestFocus();
}
else if(e.getSource()==btndel)
{
int start=txta.getSelectionStart();
int end=txta.getSelectionEnd();
txta.replaceRange("", start, end);
txta.requestFocus();
}
else if(e.getSource()==btnclear)
{
txta.setText("");
txta.requestFocus();
}
else if(e.getSource()==btnbold)
{
String s=txta.getFont().getName();
int size=txta.getFont().getSize();
int style=txta.getFont().getStyle();
   txta.setFont(new Font(s,style^Font.BOLD,size));    
   txta.requestFocus();
}
else if(e.getSource()==btnita)
{
String s=txta.getFont().getName();
int size=txta.getFont().getSize();
int style=txta.getFont().getStyle();
   txta.setFont(new Font(s,style^Font.ITALIC,size));    
   txta.requestFocus();
}
else if(e.getSource()==btnfname)
{
String s=txtf1.getText();
int size=txta.getFont().getSize();
int style=txta.getFont().getStyle();
   txta.setFont(new Font(s,style,size));    
   txta.requestFocus();
}
else if(e.getSource()==btnfsize)
{
String s=txta.getFont().getName();
int size=Integer.parseInt(txtf2.getText());
int style=txta.getFont().getStyle();
   txta.setFont(new Font(s,style,size));    
   txta.requestFocus();
}
else if(e.getSource()==btncolor)
{
txta.setForeground(Color.red);
}
else if(e.getSource()==btnexit)
{
System.exit(0);
}

}
}

}
posted @ 2022-05-21 21:50  zjw_rp  阅读(51)  评论(0编辑  收藏  举报