📄 components1.java
字号:
/*
* @(#)Components1.java 2007-4-27
* Copyright 2007 AIC. All rights reserved.
*/
package components;
import java.awt.event.*;
import javax.swing.*;
/**
* Demonstrating JLabel, JTextField, JPasswordField, JTextArea
*
* Note:
* Their construtors and methods, listeners.
*
* @author Henry Zhu
*/
public class Components1 extends AbstractComponents {
private static final long serialVersionUID = 1L;
private JLabel label;
private JTextField textField;
private JPasswordField passwordField;
private JTextArea textArea;
protected void initComponents(Box box) {
// Icon
Icon image = new ImageIcon( "image\\user.gif" );
/* =================JLabel================== */
// # JLabel constructors
label = new JLabel();
// label = new JLabel("user");
// label = new JLabel(image);
label = new JLabel("user", image, SwingConstants.RIGHT);
// # JLabel methods
label.setText("user");
label.setIcon(image);
// label.setHorizontalTextPosition(SwingConstants.RIGHT);
// label.setVerticalTextPosition(SwingConstants.BOTTOM);
box.add(label);
box.add(Box.createVerticalStrut(20));
/* ================JTextField================ */
// # JTextField constructors
// textField = new JTextField();
// textField = new JTextField(10);
textField = new JTextField("root");
// textField = new JTextField("root", 10);
// # JTextField methods
textField.setText("root");
// textField.setEditable(false);
// getText, getSelectedText : reference to event handling.
// # JTextField Event Listener (anonymous inner class)
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = textField.getText();
textArea.append("Text = " + str + "\n");
str = textField.getSelectedText();
textArea.append("Selected text = " + str + "\n");
}
});
box.add(textField);
box.add(Box.createVerticalStrut(20));
/* ==============JPasswordField============== */
// # JPasswordField constructors
// passwordField = new JPasswordField();
// passwordField = new JPasswordField(10);
passwordField = new JPasswordField("aic");
// passwordField = new JPasswordField("aic", 10);
// # JPasswordField methods
// getPassword : reference to event handling.
// # JPasswordField Event Listener
passwordField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = new String(passwordField.getPassword());
textArea.append("Password = " + str + "\n");
}
});
box.add(passwordField);
box.add(Box.createVerticalStrut(20));
/* ================JTextArea=================== */
// # JTextArea constructors
// textArea = new JTextArea();
// textArea = new JTextArea("This is a text area.");
textArea = new JTextArea(5, 10);
// textArea = new JTextArea("This is a text area.", 5, 10);
// # JTextArea methods
textArea.setLineWrap(true);
textArea.setWrapStyleWord(false);
// append(String str), insert(String str, int pos)
// : reference to above event handling.
box.add(textArea);
box.add(Box.createVerticalStrut(20));
}
/**
*
* @param title
*/
public Components1(String title) {
// invoke super constructor
super(title);
}
public static void main(String args[]) {
// create Components1 object
String title = "Components: JLabel, JTextField, " +
"JPasswordField, JTextArea";
new Components1(title);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -