Java 文本小程序


import java.awt.BorderLayout;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;




public class TextComponentTest 
{


/**
* @param args
*/
public static void main(String[] args) 
{
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
public void run()
{
TextComponentFrame textComponentFrame = new TextComponentFrame();
textComponentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textComponentFrame.setVisible(true);
}
});
}


}


class TextComponentFrame extends JFrame 
{
private static final int DEFAULT_WIDTH =  300;
private static final int DEFAULT_HEIGHT =  200;

public TextComponentFrame()
{
setTitle("TextComponentFrame");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

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 insertBtn = new JButton("Insert");
southPanel.add(insertBtn);

insertBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
textArea.append("User Name: " + textField.getText() + " Password: "
+ new String(passwordField.getPassword()) + "\n");
}
});

add(southPanel, BorderLayout.SOUTH);

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jun-H

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值