码迷,mamicode.com
首页 > 其他好文 > 详细

常用的几种文本组件(JTextComponent)

时间:2015-04-07 12:04:55      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

一:JTextField  ,最简单的文本组件

技术分享


<span style="font-size:18px;">//source code
import java.awt.GridLayout ;
import javax.swing.JTextField ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.JLabel ;
class Tester
{
   public static void main(String args[])
   {
      JFrame frame = new JFrame("文本测试样例") ;
      JTextField name = new JTextField(30) ;
      JTextField noed = new JTextField("测试信息",30) ;
      JButton button = new JButton("登陆") ;
      JLabel namelabel = new JLabel("输入用户姓名") ;
      JLabel noedlabel = new JLabel("不可编辑文本") ;
      name.setColumns(1) ;
      noed.setColumns(30) ;
      noed.setEnabled(false) ;   //不可编辑文本
      frame.setLayout(new GridLayout(3,2)) ;
      frame.add(namelabel) ;
      frame.add(name) ;
      frame.add(noedlabel) ;
      frame.add(noed) ;
      frame.add(button) ;
      frame.setSize(300,100) ;
      frame.setLocation(400,400) ;
      frame.setVisible(true) ;
     
   }
}

这样写会发现文本无法锁定
改用绝对位置
import java.awt.GridLayout ;
import javax.swing.JTextField ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.JLabel ;
class Tester
{
   public static void main(String args[])
   {
      JFrame frame = new JFrame("文本测试样例") ;
      JTextField name = new JTextField(30) ;
      JTextField noed = new JTextField("测试信息",30) ;
      //JButton button = new JButton("登陆") ;
      JLabel namelabel = new JLabel("输入用户姓名") ;
      JLabel noedlabel = new JLabel("不可编辑文本") ;
      name.setColumns(1) ;
      noed.setColumns(30) ;
      noed.setEnabled(false) ;   //不可编辑文本
      namelabel.setBounds(10,10,100,20) ;
      noedlabel.setBounds(10,40,100,20) ;
      name.setBounds(110,10,80,20) ;
      noed.setBounds(110,40,50,20) ;
      //frame.setLayout(new GridLayout(3,2)) ;使用布局管理器会带来忽略默认参数的问题
      frame.add(namelabel) ;
      frame.add(name) ;
      frame.add(noedlabel) ;
      frame.add(noed) ;
      //frame.add(button) ;
      frame.setSize(300,100) ;
      frame.setLocation(400,400) ;
      frame.setVisible(true) ;
     
   }
}</span>


二:密码框 JPasswordField
这个有意思!
可以设置默认回显字符比如最常见的 ‘*’   也可以自定义字符,如下图

技术分享

源代码:

<span style="font-size:18px;">import java.awt.Color ;
import javax.swing.JFrame ;
import javax.swing.JPasswordField ;
import javax.swing.JLabel ;
class Tester
{
    public static void main(String args[])
    {
       JFrame frame = new JFrame("密码框") ;
       JLabel defaultEcho = new JLabel("默认回显字符") ;
       JLabel newEcho = new JLabel("自定义回显$") ;
       JPasswordField jpf1 = new JPasswordField();
       JPasswordField jpf2 = new JPasswordField();
       jpf2.setEchoChar('$') ;
       defaultEcho.setBounds(10,10,100,20) ;
       newEcho.setBounds(10,40,100,20) ;
       jpf1.setBounds(110,10,80,20) ;
       jpf2.setBounds(110,40,50,20) ;
       frame.setLayout(null) ;
       frame.add(defaultEcho) ;
       frame.add(jpf1) ;
       frame.add(newEcho) ;
       frame.add(jpf2) ;
       frame.setBackground(Color.orange) ;
       frame.setSize(300,100) ;
       frame.setLocation(400,400) ;
       frame.setVisible(true) ;
    }
}</span>


三:多行文本 JTextArea

原谅我这句话实在是背不下来,只能指望复制文档
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
反正加上这句,就可以加上拖动条了 -_-|||

技术分享

源代码:

<span style="font-size:18px;">import java.awt.GridLayout ;
import javax.swing.JFrame ;
import javax.swing.JLabel ;
import javax.swing.JTextArea ;
import javax.swing.JScrollPane ;  //神奇的拖动条
class Tester
{
   public static void main(String args[])
   {
      JFrame frame = new JFrame("多行文本") ;
      JTextArea ta = new JTextArea(20,10) ;
      JLabel label = new JLabel("多行文本:",JLabel.CENTER) ;
      JScrollPane sc = new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS ) ;
      frame.setLayout(new GridLayout(2,1)) ;
      frame.add(label) ;
      frame.add(sc) ; 
      frame.setSize(400,200) ;
      frame.setLocation(400,400) ;
      frame.setVisible(true) ;
   }
}
</span>


常用的几种文本组件(JTextComponent)

标签:

原文地址:http://blog.csdn.net/u012566693/article/details/44916671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!