成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Swing系列之JTextField(單行文本框)

DevTalking / 2619人閱讀

摘要:介紹是一個(gè)輕量級(jí)組件,它允許編輯單行文本。把字段文本用作針對(duì)的命令字符串。右對(duì)齊尾部對(duì)齊在所需的字段文本尺寸小于為它分配的尺寸時(shí)使用。這是由和方法確定的。如果具有操作偵聽器,則導(dǎo)致偵聽器獲取一個(gè),并使用事件。

介紹

JTextField是一個(gè)輕量級(jí)組件,它允許編輯單行文本。

JTextField 具有建立字符串的方法,此字符串用作針對(duì)被激發(fā)的操作事件的命令字符串。java.awt.TextField 把字段文本用作針對(duì) ActionEvent 的命令字符串。如果通過 setActionCommand 方法設(shè)置的命令字符串不為 null,則 JTextField 將使用該字符串來保持與 java.awt.TextField 的兼容性,否則將使用字段文本來保持兼容性。

setEchoChargetEchoChar 方法不是直接提供的,以避免可插入的外觀的新實(shí)現(xiàn)意外公開密碼字符。為了提供類似密碼的服務(wù),多帶帶的類 JPasswordField 擴(kuò)展了 JTextField,從而通過可插入外觀獨(dú)立地提供此服務(wù)。

JTextField 的水平對(duì)齊方式可以設(shè)置為左對(duì)齊、前端對(duì)齊、居中對(duì)齊、右對(duì)齊或尾部對(duì)齊。右對(duì)齊/尾部對(duì)齊在所需的字段文本尺寸小于為它分配的尺寸時(shí)使用。這是由 setHorizontalAlignment 和 getHorizontalAlignment 方法確定的。默認(rèn)情況下為前端對(duì)齊。

文本字段如何使用 VK_ENTER 事件取決于文本字段是否具有任何操作偵聽器。如果具有操作偵聽器,則 VK_ENTER 導(dǎo)致偵聽器獲取一個(gè) ActionEvent,并使用 VK_ENTER 事件。這與 AWT 文本字段處理 VK_ENTER 事件的方式是兼容的。如果文本字段沒有操作偵聽器,則從 1.3 版本開始不使用 VK_ENTER 事件。而是處理祖先組件的綁定,這將啟用 JFC/Swing 的默認(rèn)按鈕特性。

Swing 不是線程安全的

構(gòu)造函數(shù)

JTextField() 構(gòu)造一個(gè)新的 TextField

JTextField(Document doc, String text, int columns) 構(gòu)造一個(gè)新的 JTextField,它使用給定文本存儲(chǔ)模型和給定的列數(shù)。

JTextField(int columns) 構(gòu)造一個(gè)具有指定列數(shù)的新的空 TextField。

JTextField(String text) 構(gòu)造一個(gè)用指定文本初始化的新 TextField。

JTextField(String text, int columns) 構(gòu)造一個(gè)用指定文本和列初始化的新 TextField。

常用的函數(shù)

get/setHorizontalAlignment(int alignment) 設(shè)置/得到文本的水平對(duì)齊方式。其中水平的對(duì)齊方式有:JTextField.LEFT

JTextField.CENTER

JTextField.RIGHT

JTextField.LEADING (the default)

JTextField.TRAILING

setFont(Font font) 設(shè)置字體

setScrollOffset(int scrollOffset) 獲取滾動(dòng)偏移量(以像素為單位)。

setDocument(Document doc) 將編輯器與一個(gè)文本文檔關(guān)聯(lián),這里的意思就是將此文本框與一個(gè)文本文檔關(guān)聯(lián),這將會(huì)保持內(nèi)容一致,如果一個(gè)改變了,另外一個(gè)也會(huì)改變。

setInputVerifier(verifier) 設(shè)置驗(yàn)證方式,如果此文本不能通過驗(yàn)證那么就不能將焦點(diǎn)聚焦到下一個(gè)組件上,就會(huì)一直聚焦到這個(gè)文本框上

setDragEnabled(boolean x) 設(shè)置在文本框中是否能夠拖放文本,為true則是能夠,這里的意思就是能夠?qū)⑽谋具x中后能不能將文本拖走

addActionListener(ActionListener action) 添加監(jiān)聽機(jī)制,輸入文本按回車即可觸發(fā),和按鈕的監(jiān)聽機(jī)制相同

write(InfileWriter writer) 將文本框中的內(nèi)容輸入到文件中

addKeyListener(KeyListener event) 添加鍵盤監(jiān)聽,在文本框中輸入內(nèi)容時(shí)會(huì)觸發(fā)鍵盤,其中有按下,釋放,鍵入的動(dòng)作,詳情見官方文檔

一個(gè)簡(jiǎn)單的實(shí)例
import javax.swing.*;
import java.awt.*;

class text extends JFrame {
    private JTextField textField1;
    private JTextField textField2;

    public static void main(String args[]) {
        text my = new text();
        my.setVisible(true);

    }

    public text() {
        //this.setBounds(100,100,300,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridLayout(2, 1));
        textField1 = new JTextField(10);
        textField2 = new JTextField();
        panel.add(textField1);
        panel.add(textField2);
        this.getContentPane().add(panel, BorderLayout.CENTER);
        this.pack();
        InputVerifier verifier = new InputVerifier() {    //添加驗(yàn)證方式
            @Override
            public boolean verify(JComponent input) {     //重載函數(shù)
                boolean value;
                textField1 = (JTextField) input;    //將input組件強(qiáng)制轉(zhuǎn)化為JTextField類型的單行文本框
                return textField1.getText().equals("pass");  //判斷是否輸入的時(shí)pass,如果不是就會(huì)驗(yàn)證錯(cuò)誤

            }
        };
        textField1.setInputVerifier(verifier);   //設(shè)置驗(yàn)證方式
        textField1.setHorizontalAlignment(JTextField.CENTER);   //設(shè)置水平對(duì)齊方式
        Font font = new Font("楷體", Font.BOLD + Font.ITALIC, 20);
        textField1.setFont(font);   //設(shè)置字體
        textField1.setDragEnabled(true);  //設(shè)置在單行文本框中能夠拖放文本,如果為false則不能夠拖放文本


    }
}
關(guān)聯(lián)文本文檔
import java.awt.Container;
import java.awt.GridLayout;
/*from   w  ww.jav  a  2s . co m*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.Document;

public class Main extends JFrame {
  JLabel nameLabel = new JLabel("Name:");
  JLabel mirroredNameLabel = new JLabel("Mirrored:");
  JTextField name = new JTextField(20);
  JTextField mirroredName = new JTextField(20);

  public Main() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLayout(new GridLayout(2, 0));

    Container contentPane = this.getContentPane();
    contentPane.add(nameLabel);
    contentPane.add(name);
    contentPane.add(mirroredNameLabel);
    contentPane.add(mirroredName);

    Document nameModel = name.getDocument();    //得到文本框的文本文檔,將之與第二個(gè)文本框關(guān)聯(lián)
    mirroredName.setDocument(nameModel);           //兩個(gè)文本框中的內(nèi)容相互關(guān)聯(lián),這樣只需要在一個(gè)里面輸入文本,同時(shí)也會(huì)在另外一個(gè)文本框中顯示
    
    pack();
    setVisible(true);    
  }

  public static void main(String[] args) {
    Main frame = new Main();

  }
}

說明:這里是將兩個(gè)文本框相關(guān)聯(lián),這樣就能達(dá)到一個(gè)文本框輸入的同時(shí),另外一個(gè)也會(huì)同時(shí)更新內(nèi)容

Action Listener(動(dòng)作監(jiān)聽機(jī)制)

輸入文本后按回車即可觸發(fā)

import java.awt.event.ActionEvent;
//from  w  w  w. ja va2s  .c o m
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main {

  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField jTextField1 = new JTextField();

    jTextField1.setText("jTextField1");
    //添加監(jiān)聽機(jī)制
    jTextField1.addActionListener(new   java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("action");
      }
    });
    frame.add(jTextField1);

    frame.setSize(300, 200);
    frame.setVisible(true);
  }

}
驗(yàn)證文本內(nèi)容

使用InputVerifier)驗(yàn)證

import java.awt.BorderLayout;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Verifier Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField textField1 = new JTextField();
    JTextField textField2 = new JTextField();
    InputVerifier verifier = new InputVerifier() {     //創(chuàng)建一個(gè)驗(yàn)證
      public boolean verify(JComponent comp) {
        boolean returnValue;
        JTextField textField = (JTextField) comp;      //強(qiáng)制轉(zhuǎn)換,將控件類型的comp轉(zhuǎn)換成JTextFiled類型的
        try {
          Integer.parseInt(textField.getText());    //將輸入的內(nèi)容轉(zhuǎn)化程int類型,如果輸入的字符串不是十進(jìn)制的話就會(huì)觸發(fā)                                                          //NumberFormateException錯(cuò)誤
          returnValue = true;
        } catch (NumberFormatException e) {   
          returnValue = false;
        }
        return returnValue;        //如果返回false的話,那么指針就會(huì)一直聚焦在此文本框中,不能移動(dòng)到其他的組件上
      }
    };
    textField1.setInputVerifier(verifier);
    frame.add(textField1, BorderLayout.NORTH);
    frame.add(textField2, BorderLayout.CENTER);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}

說明:如果返回false的話,那么指針就會(huì)一直聚焦在此文本框中,不能移動(dòng)到其他的組件上

將文本框中的內(nèi)容保存到文件中
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

class Main extends JFrame {
    private JTextField textField;
    private FileWriter writer;

    public static void main(String args[]) {
        Main my = new Main();
        my.setVisible(true);
    }

    public Main() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new BorderLayout());
        JButton button = new JButton("運(yùn)行");
        JLabel label = new JLabel("name");
        textField = new JTextField();
        panel.add(label, BorderLayout.WEST);
        panel.add(textField, BorderLayout.CENTER);
        String filename = "text.txt";
        button.addActionListener(new ActionListener() {    //添加一個(gè)按鈕觸發(fā)裝置,這里只要點(diǎn)擊一下anniu就會(huì)將文本框中的內(nèi)容輸入到文件中
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    writer = new FileWriter(filename, false);   //創(chuàng)建一個(gè)寫入文件的對(duì)象,這里的false表示不在文件的末尾添加
                    textField.write(writer);     //將單行文本中輸入的內(nèi)容寫入到文件中
                    writer.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                    System.out.println("false");
                }
            }
        });
        panel.add(button, BorderLayout.SOUTH);
        this.getContentPane().add(panel, BorderLayout.CENTER);
        this.pack();
    }

}

說明:這里使用的是FileWriter類將內(nèi)容寫入到文件中,詳情請(qǐng)看我的上一篇文章

復(fù)制、粘貼、剪切文本

這里使用的時(shí)copy()、paste()cut()函數(shù)

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class Main {
  public static void main(String args[]) {
    final JTextField textField = new JTextField(15);
    JButton buttonCut = new JButton("Cut");
    JButton buttonPaste = new JButton("Paste");
    JButton buttonCopy = new JButton("Copy");

    JFrame jfrm = new JFrame("Cut, Copy, and Paste");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(230, 150);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    buttonCut.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent le) {
        textField.cut();
      }
    });

    buttonPaste.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent le) {
        textField.paste();
      }
    });

    buttonCopy.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent le) {
        textField.copy();
      }
    });

    textField.addCaretListener(new CaretListener() {
      public void caretUpdate(CaretEvent ce) {
        System.out.println("All text: " + textField.getText());
        if (textField.getSelectedText() != null)
          System.out.println("Selected text: " + textField.getSelectedText());
        else
          System.out.println("Selected text: ");
      }
    });

    jfrm.add(textField);
    jfrm.add(buttonCut);
    jfrm.add(buttonPaste);
    jfrm.add(buttonCopy);
    jfrm.setVisible(true);
  }
}

說明:這里使用的時(shí)用三個(gè)按鈕監(jiān)聽操作,只需要按住對(duì)應(yīng)的按鈕就會(huì)觸發(fā)機(jī)制

添加鍵盤監(jiān)聽機(jī)制
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Main extends JFrame {
  public Main() throws HeadlessException {
    setSize(200, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JLabel usernameLabel = new JLabel("Username: ");
    JTextField usernameTextField = new JTextField();
    usernameTextField.setPreferredSize(new Dimension(100, 20));
    add(usernameLabel);
    add(usernameTextField);

    usernameTextField.addKeyListener(new KeyAdapter() {   //創(chuàng)建機(jī)制
      public void keyReleased(KeyEvent e) {        //重載函數(shù),釋放按鍵觸發(fā)
        JTextField textField = (JTextField) e.getSource();  //得到最初發(fā)生event的組件對(duì)象,既是文本框?qū)ο?        String text = textField.getText();
        textField.setText(text.toUpperCase());      //將所有的小寫字母轉(zhuǎn)換成大寫字母
      }
       public void keyTyped(KeyEvent e) {           //鍵入時(shí)觸發(fā)
      }

      public void keyPressed(KeyEvent e) {       //釋放按鍵時(shí)觸發(fā)的函數(shù)
      }   
    });
  }

  public static void main(String[] args) {
    new Main().setVisible(true);
  }
}
參考文檔

官方網(wǎng)站)

英文文檔

本人博客

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66968.html

相關(guān)文章

  • 初學(xué) Swing 組件

    摘要:包括了圖形用戶界面器件如文本框,按鈕,分隔窗格和表。按照指定布局限制添加組件。移除指定位置的組件。通常文本框用于接收用戶信息或其他文本信息的輸入。因此,組件也稱為密碼文本框。創(chuàng)建一個(gè)具有出事文本信息以及制定列數(shù)的文本框。 Swing 是一個(gè)為Java設(shè)計(jì)的GUI工具包。 Swing是JAVA基礎(chǔ)類的一部分。 Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。 S...

    codeGoogle 評(píng)論0 收藏0
  • 界面開發(fā)的步驟

    摘要:事件對(duì)象攜帶了動(dòng)作發(fā)生時(shí)的相關(guān)信息,比如通過事件對(duì)象獲取按鈕的字符串,通過字符串判斷后執(zhí)行不同的代碼。使用監(jiān)聽器的步驟自己創(chuàng)建一個(gè)類使用這個(gè)類創(chuàng)建一個(gè)對(duì)象,用按鈕對(duì)象的添加監(jiān)聽器方法添加這個(gè)對(duì)象。 ...

    MangoGoing 評(píng)論0 收藏0
  • Java 實(shí)現(xiàn)簡(jiǎn)單計(jì)算器

    摘要:對(duì)于理論算法不再這累贅了。在查閱資料的時(shí)候發(fā)現(xiàn)算法不管用棧還是正則等等,似乎只處理操作符是的數(shù),這是很不可取的。所以需要先將中綴表達(dá)式轉(zhuǎn)化成后綴并標(biāo)記多位數(shù)的操作符,然后在處理后綴表達(dá)式。 最后一次更新于2019/07/08 效果演示圖 showImg(https://segmentfault.com/img/bVbuIwj?w=388&h=290); 功能與流程 要制作一個(gè)簡(jiǎn)易計(jì)算器...

    thekingisalwaysluc 評(píng)論0 收藏0
  • Java窗口(JFrame)從零開始(8)——文本+文本域+密碼

    摘要:文本域構(gòu)造方法摘要構(gòu)造新的。構(gòu)造顯示指定文本的新的。密碼框構(gòu)造方法摘要構(gòu)造一個(gè)新,使其具有默認(rèn)文檔為的開始文本字符串和為的列寬度。登錄界面賬號(hào)密碼清除登錄觸發(fā)事件設(shè)置關(guān)閉方式,可以選擇多種關(guān)閉玄子選項(xiàng) 應(yīng)該最后一章了,前面有大神提到很少有人用Java做UI,這里就算是給像我這樣的初學(xué)者去了解窗體是怎么一回事的文章吧 文本框(JTextField) 構(gòu)造方法摘要 JTextField(...

    X1nFLY 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

DevTalking

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<