zoukankan      html  css  js  c++  java
  • 常用的几种文本组件(JTextComponent)

    一: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>


  • 相关阅读:
    js基础:关于Boolean() 与 if
    @@cursor_rows变量解析
    SQL Prompt
    google android sdk下载hoosts
    java环境配置
    Linux grep用法整理
    bash调试执行
    Vim常见快捷键汇总
    Linux查看磁盘块大小
    Linux Bash终端快捷键小结
  • 原文地址:https://www.cnblogs.com/emoji/p/4436808.html
Copyright © 2011-2022 走看看