Java 文本小程序

Java 文本小程序_第1张图片

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);

}
}

你可能感兴趣的:(Java 文本小程序)