当前位置 : 主页 > 编程语言 > java >

《Java 核心技术 卷1》 笔记 第九章 Swing 用户界面组件(4)

来源:互联网 收集:自由互联 发布时间:2022-07-13
9.3.3 密码文本框 为了避免进行投屏展示时,密码在输入过程中发生泄露,需要在页面显示上屏蔽密码的实际输入内容。 import javax . swing . * ; import java . awt . * ; import java . awt . event . * ;


9.3.3 密码文本框

为了避免进行投屏展示时,密码在输入过程中发生泄露,需要在页面显示上屏蔽密码的实际输入内容。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

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

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TextFrame f = new TextFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}

class TextFrame extends JFrame{
public static final int W = 300;
public static final int H = 200;

public TextFrame(){
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
setBounds(((int)screenSize.getWidth()-W)/2,((int)screenSize.getHeight()-H)/2,W,H);

add(new TextPanel());
}
}

class TextPanel extends JPanel implements ActionListener{
JButton b1;
JButton b2;
JTextField nameField;
JPasswordField passField;

JPanel formPanel;
JPanel operatePanel;
public TextPanel(){
setLayout(new BorderLayout());

formPanel = new JPanel();
addFormPanelItem();
add(formPanel,BorderLayout.CENTER);

operatePanel = new JPanel();
addOperatePanelItem();
add(operatePanel,BorderLayout.SOUTH);


}

private void addFormPanelItem(){
setLayout(new GridLayout(2,2));
addLabelEdit("姓名:",false);
addLabelEdit("密码:",true);
}

private void addLabelEdit(String name,boolean isSecret){
if(!isSecret){
JLabel label = new JLabel(name,SwingConstants.RIGHT);
formPanel.add(label);
nameField = new JTextField(20);
nameField.setEditable(true);
formPanel.add(nameField);
}else{
JLabel label = new JLabel(name,SwingConstants.RIGHT);
formPanel.add(label);
passField = new JPasswordField(20);
passField.setEchoChar('*');
passField.setEditable(true);
formPanel.add(passField);
}
}

private void addOperatePanelItem(){
b1 = new JButton("启用");
b2 = new JButton("停用");
operatePanel.add(b1);
operatePanel.add(b2);
b1.addActionListener( this);
b2.addActionListener( this);


}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
nameField.setEditable(true);
}else if(e.getSource()==b2){
nameField.setEditable(false);
}
}
}

《Java 核心技术 卷1》 笔记 第九章 Swing 用户界面组件(4)_开发语言

9.3.4 多行文本

可以设置显示多行内容:

JTextArea textArea = new JTextArea(8,40);

超过的列数进行截断:

textArea.setLineWrap(true);

9.3.5 滚动窗格

JScrollPane scrollPanel = new ScrollPane(textArea);

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

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

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TextComponentFrame frame = new TextComponentFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

class TextComponentFrame extends JFrame{
public TextComponentFrame(){
setTitle("TextComponentTest");
setSize(300,200);

final JTextField textField = new JTextField();
final JPasswordField passwordField = new JPasswordField();

JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(2,2));
northPanel.add(new JLabel("User name:",SwingConstants.RIGHT));
northPanel.add(textField);
northPanel.add(new JLabel("Password:",SwingConstants.RIGHT));
northPanel.add(passwordField);

add(northPanel,BorderLayout.NORTH);

final JTextArea textArea = new JTextArea(8,40);
JScrollPane scrollPane = new JScrollPane(textArea);

add(scrollPane,BorderLayout.CENTER);

JPanel southPanel = new JPanel();
JButton insertButton = new JButton("Insert");
southPanel.add(insertButton);
insertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.append("User name: "+textField.getText()+"Password:"+
new String(passwordField.getPassword())+"\n");
}
});
add(southPanel,BorderLayout.SOUTH);
}
}

《Java 核心技术 卷1》 笔记 第九章 Swing 用户界面组件(4)_公众号_02

相关内容:选择 《Java核心技术 卷1》查找相关笔记

喜欢的话,点个赞吧~!平时做题,以及笔记内容将更新到公众号。

关注公众号,互相学习:钰娘娘知识汇总

《Java 核心技术 卷1》 笔记 第九章 Swing 用户界面组件(4)_java_03

网友评论