Swing常用組件之文本框和文本區(qū)
一、JTextField(文本框)的使用
JTextField是一個(gè)輕量級(jí)的組件,可以編輯單行文本,實(shí)現(xiàn)剪切,復(fù)制,粘貼,快捷鍵等工作,如果文本的長(zhǎng)度超出顯示范圍,會(huì)自動(dòng)滾動(dòng)文本,JTextField類的構(gòu)造方法
1.JTextField的常用構(gòu)造方法:
JTextField() 構(gòu)造一個(gè)新的 TextField。
JTextField(int columns) 構(gòu)造一個(gè)具有指定列數(shù)的新的空 TextField。
JTextField(String text) 構(gòu)造一個(gè)用指定文本初始化的新TextField。
JTextField(String text, int columns) 構(gòu)造一個(gè)用指定文本和列初始化的新TextField。
2.JTextField的常用方法:
SetText(string) 設(shè)置文本域中的文本值
GetText()返回文本域中的輸入文本值
getColumns()返回文本域的列數(shù)
setEditable(Boolean) 設(shè)置文本域是否為只讀狀態(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("請(qǐng)輸入用戶名");
JLabel password = new JLabel("請(qǐng)輸入密碼");
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("登錄成功,歡迎您的到來(lái)!");
}
else
{
jl[2].setText("對(duì)不起,您的密碼或用戶名錯(cuò)誤!");
}
}
}
public static void main(String args[])
{
new LoginTest();
}
}
二、JTextArea(文本區(qū))的使用
1.JTextArea的常用構(gòu)造方法:
JTextArea() 構(gòu)造新的 TextArea。
JTextArea(String text) 構(gòu)造顯示指定文本的新的 TextArea。
JTextArea(int rows, int columns) 構(gòu)造具有指定行數(shù)和列數(shù)的新的空 TextArea。
JTextArea(String text, int rows, int columns) 構(gòu)造具有指定文本、行數(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()設(shè)置文本顯示的內(nèi)容
t2.append("JTextArea2");// append()方法會(huì)將給定文本追加到文檔結(jié)尾。
t4.setLineWrap(true);// 設(shè)置文本區(qū)的換行策略。
t4.setFont(new Font("標(biāo)楷體", Font.BOLD, 16)); //設(shè)置當(dāng)前字體。
t4.setTabSize(2);//使用setTabSize()方法設(shè)置[Tab]鍵的跳離距離
將JTextArea放入JScrollPane中,這樣就能利用滾動(dòng)的效果看到輸入超過(guò)JTextArea高度的文字.
3.JTextArea使用的案例:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//實(shí)現(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("標(biāo)楷體", Font.BOLD, 16));
jta.setLineWrap(true);// 激活自動(dòng)換行功能
jta.setWrapStyleWord(true);// 激活斷行不斷字功能
jta.setBackground(Color.pink);
jscrollPane = new JScrollPane(jta);
jpanel = new JPanel();
jpanel.setLayout(new GridLayout(1, 3));
jb1 = new JButton("復(fù)制");
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();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Java Swing中的工具欄(JToolBar)和分割面版(JSplitPane)組件使用案例
- Java Swing中的表格(JTable)和樹(shù)(JTree)組件使用實(shí)例
- Java Swing中的下拉式菜單(menu)、彈出式菜單(JPopupMenu)、選項(xiàng)卡窗體(JTabbedPane)組件使用案例
- Java Swing中的JButton、JComboBox、JList和JColorChooser組件使用案例
- java之swing單選框用法實(shí)例分析
- java之swing實(shí)現(xiàn)復(fù)選框的方法
- Swing常用組件之多行文本區(qū)JTextArea
- Swing常用組件之單選按鈕和復(fù)選框
相關(guān)文章
springboot如何獲取接口下所有實(shí)現(xiàn)類
Mybatis-Plus中分頁(yè)插件PaginationInterceptor的使用
Java模擬計(jì)算機(jī)的整數(shù)乘積計(jì)算功能示例
springboot整合quartz項(xiàng)目使用案例
java給釘釘郵箱發(fā)送郵件功能實(shí)現(xiàn)
IntelliJ IDEA(或者JetBrains PyCharm)中彈出"IntelliJ IDEA License
解決spring @ControllerAdvice處理異常無(wú)法正確匹配自定義異常
java 實(shí)現(xiàn)約瑟夫環(huán)的實(shí)例代碼

