📄 jtextfielddemo.java
字号:
package components;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class JTextFieldDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea textArea;
private JTextField[] fields;
private JPasswordField[] pwds;
public JTextFieldDemo() {
Container container = getContentPane();
container.setLayout(new FlowLayout());
JPanel textPanel = new JPanel();
JPanel pwdPanel = new JPanel();
fields = new JTextField[6];
fields[0] = new JTextField();
fields[1] = new JTextField("String");
fields[2] = new JTextField(10);
fields[3] = new JTextField("String", 10);
fields[3].setBackground(Color.RED);
fields[3].setActionCommand("i love you");
pwds = new JPasswordField[6];
pwds[0] = new JPasswordField();
pwds[1] = new JPasswordField("String");
pwds[2] = new JPasswordField(10);
pwds[3] = new JPasswordField("String", 10);
TextActionListener listener = new TextActionListener();
for (int i = 0; i < 4; i++) {
fields[i].addActionListener(listener);
pwds[i].addActionListener(listener);
textPanel.add(fields[i]);
pwdPanel.add(pwds[i]);
}
container.add(textPanel);
container.add(pwdPanel);
textArea = new JTextArea(14, 22);
container.add(textArea);
setSize(400, 400);
setVisible(true);
}
class TextActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fields[0]) {
textArea.setText("fields[0] :\n text: " + fields[0].getText()
+ "\n selected text " + fields[0].getSelectedText());
} else if (e.getSource() == fields[1]) {
textArea.setText("fields[1] :\n text: " + fields[1].getText()
+ "\n selected text " + fields[1].getSelectedText());
} else if (e.getSource() == fields[2]) {
textArea.setText("fields[2] :\n text: " + fields[2].getText()
+ "\n selected text " + fields[2].getSelectedText());
} else if (e.getSource() == fields[3]) {
textArea.setText("fields[3] :\n text: " + e.getActionCommand()
+ "\n selected text " + fields[3].getSelectedText());
} else if (e.getSource() == pwds[0]) {
textArea.setText("pwds[0] :\n text: " + new String(pwds[0].getPassword())
+ "\n selected text " + pwds[0].getSelectedText());
} else if (e.getSource() == pwds[1]) {
textArea.setText("pwds[1] :\n text: " + new String(pwds[1].getPassword())
+ "\n selected text " + pwds[1].getSelectedText());
} else if (e.getSource() == pwds[2]) {
textArea.setText("pwds[2] :\n text: " + new String(pwds[2].getPassword())
+ "\n selected text " + pwds[2].getSelectedText());
} else if (e.getSource() == pwds[3]) {
textArea.setText("pwds[3] :\n text: " + e.getActionCommand()
+ "\n selected text " + pwds[3].getSelectedText());
}
}
}
public static void main(String[] args) {
JTextFieldDemo application = new JTextFieldDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -