首页 > 代码库 > java-swing-JTextComponent
java-swing-JTextComponent
package com.http;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingJTextComponent extends JFrame{ private static final int TEXTAREA_ROWS = 8; private static final int TEXTARER_COLUMNS= 20; public SwingJTextComponent() { //创建文本域实例 final JTextField textField = new JTextField(); final JPasswordField passwordField = new JPasswordField(); //定义一个Panel,设置了表格布局管理器并指定行与列 JPanel northPanel = new JPanel(); northPanel.setLayout(new GridLayout(2,2));// 添加文本域的标签 northPanel.add(new JLabel("User name:", SwingConstants.RIGHT));// 将文本域添加到panel northPanel.add(textField); northPanel.add(new JLabel("Password:", SwingConstants.RIGHT)); northPanel.add(passwordField);// 将pannel添加到frame add(northPanel,BorderLayout.NORTH); // 定义文本区实例,并设置大小,添加到frame的中心位置 final JTextArea textArea = new JTextArea(TEXTAREA_ROWS,TEXTARER_COLUMNS); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane, BorderLayout.CENTER); // 定义一个按钮,添加到frame下方,并定义监听事件,点击按钮,文本区显示用户名与密码 JPanel southPanel = new JPanel(); JButton insertButton = new JButton("Insert"); insertButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub textArea.append("User name:" + textField.getText() + " Password:" + new String(passwordField.getPassword()) + "\n"); } }); southPanel.add(insertButton); add(southPanel,BorderLayout.SOUTH); pack(); } public static void main(String[] argvs) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new SwingJTextComponent(); frame.setTitle("SwingJTextComponent"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } }
java-swing-JTextComponent
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。