Swing常用組件之文本框和文本區(qū)
一、JTextField(文本框)的使用
JTextField是一個輕量級的組件,可以編輯單行文本,實現(xiàn)剪切,復制,粘貼,快捷鍵等工作,如果文本的長度超出顯示范圍,會自動滾動文本,JTextField類的構造方法
1.JTextField的常用構造方法:
JTextField() 構造一個新的 TextField。
JTextField(int columns) 構造一個具有指定列數(shù)的新的空 TextField。
JTextField(String text) 構造一個用指定文本初始化的新TextField。
JTextField(String text, int columns) 構造一個用指定文本和列初始化的新TextField。
2.JTextField的常用方法:
SetText(string) 設置文本域中的文本值
GetText()返回文本域中的輸入文本值
getColumns()返回文本域的列數(shù)
setEditable(Boolean) 設置文本域是否為只讀狀態(tài)
3.JTextField的使用示例:
package ch10;
import java.awt.event.*;
import javax.swing.*;
public class LoginTest extends JFrame implements ActionListener
{
private JPanel jp = new JPanel();
JLabel name = new JLabel("請輸入用戶名");
JLabel password = new JLabel("請輸入密碼");
JLabel show = new JLabel("");
private JLabel[] jl = new JLabel[]{name,password,show};
JButton login = new JButton("登錄");
JButton reset = new JButton("重置");
private JButton[] jb = new JButton[]{login,reset};
private JTextField jname= new JTextField();
private JPasswordField jpass = new JPasswordField();
public LoginTest()
{
jp.setLayout(null);
for(int i=0;i<2;i++)
{
jl[i].setBounds(30,20+40*i,180,20);
jb[i].setBounds(30+110*i,100,80,20);
jb[i].addActionListener(this);
jp.add(jl[i]);
jp.add(jb[i]);
}
jname.setBounds(130,15,100,20);
jp.add(jname);
jname.addActionListener(this);
jpass.setBounds(130,60,100,20);
jp.add(jpass);
jpass.addActionListener(this);
jpass.setEchoChar('*');
jl[2].setBounds(10,180,270,20);
jp.add(jl[2]);
this.add(jp);
this.setBounds(200,200,300,300);
this.setVisible(true);
this.setTitle("登錄窗口");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource()==jname)
{
jpass.requestFocus();
}
else if(a.getSource()==jb[1])
{
jl[2].setText("");
jname.setText("");
jpass.setText("");
jname.requestFocus();
}
else
{
if(jname.getText().equals("lixiang")&&String.valueOf(jpass.getPassword()).equals("201407239"))
{
jl[2].setText("登錄成功,歡迎您的到來!");
}
else
{
jl[2].setText("對不起,您的密碼或用戶名錯誤!");
}
}
}
public static void main(String args[])
{
new LoginTest();
}
}
二、JTextArea(文本區(qū))的使用
1.JTextArea的常用構造方法:
JTextArea() 構造新的 TextArea。
JTextArea(String text) 構造顯示指定文本的新的 TextArea。
JTextArea(int rows, int columns) 構造具有指定行數(shù)和列數(shù)的新的空 TextArea。
JTextArea(String text, int rows, int columns) 構造具有指定文本、行數(shù)和列數(shù)的新的 TextArea。
使用示例:
JTextArea t1 = new JTextArea();
JTextArea t2 = new JTextArea(2, 8);
JTextArea t3 = new JTextArea("JTextArea3");
JTextArea t4 = new JTextArea("JTextArea4", 5, 10);
2.JTextArea的常用方法:
使用示例:
t1.setText("JTextArea1");// setText()設置文本顯示的內容
t2.append("JTextArea2");// append()方法會將給定文本追加到文檔結尾。
t4.setLineWrap(true);// 設置文本區(qū)的換行策略。
t4.setFont(new Font("標楷體", Font.BOLD, 16)); //設置當前字體。
t4.setTabSize(2);//使用setTabSize()方法設置[Tab]鍵的跳離距離
將JTextArea放入JScrollPane中,這樣就能利用滾動的效果看到輸入超過JTextArea高度的文字.
3.JTextArea使用的案例:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//實現(xiàn)接口ActionListener
public class JTextAreaDemo3 implements ActionListener {
JFrame jf;
JPanel jpanel;
JButton jb1, jb2, jb3;
JTextArea jta = null;
JScrollPane jscrollPane;
public JTextAreaDemo3() {
jf = new JFrame("JTextArea案例3");
Container contentPane = jf.getContentPane();
contentPane.setLayout(new BorderLayout());
jta = new JTextArea(10, 15);
jta.setTabSize(4);
jta.setFont(new Font("標楷體", Font.BOLD, 16));
jta.setLineWrap(true);// 激活自動換行功能
jta.setWrapStyleWord(true);// 激活斷行不斷字功能
jta.setBackground(Color.pink);
jscrollPane = new JScrollPane(jta);
jpanel = new JPanel();
jpanel.setLayout(new GridLayout(1, 3));
jb1 = new JButton("復制");
jb1.addActionListener(this);
jb2 = new JButton("粘貼");
jb2.addActionListener(this);
jb3 = new JButton("剪切");
jb3.addActionListener(this);
jpanel.add(jb1);
jpanel.add(jb2);
jpanel.add(jb3);
contentPane.add(jscrollPane, BorderLayout.CENTER);
contentPane.add(jpanel, BorderLayout.SOUTH);
jf.setSize(400, 300);
jf.setLocation(400, 200);
jf.setVisible(true);
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// 覆蓋接口ActionListener的方法actionPerformed
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
jta.copy();
} else if (e.getSource() == jb2) {
jta.paste();
} else if (e.getSource() == jb3) {
jta.cut();
}
}
public static void main(String[] args) {
new JTextAreaDemo3();
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助。
相關文章
Mybatis-Plus中分頁插件PaginationInterceptor的使用
我們在開發(fā)的過程中,經常會遇到分頁操作,本文主要介紹了Mybatis-Plus中分頁插件PaginationInterceptor的使用,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06
IntelliJ IDEA(或者JetBrains PyCharm)中彈出"IntelliJ IDEA License
今天小編就為大家分享一篇關于IntelliJ IDEA(或者JetBrains PyCharm)中彈出"IntelliJ IDEA License Activation"的解決辦法,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
解決spring @ControllerAdvice處理異常無法正確匹配自定義異常
這篇文章主要介紹了解決spring @ControllerAdvice處理異常無法正確匹配自定義異常的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

