Java小程序之GUI开发简单前台登录界面

主要的类,做个了继承了JFrame类的新类

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class TextComponentFrame  extends JFrame {
    public static final int TEXTAREA_ROWS = 8;
    public static final int TEXTAREA_COLUMNS = 20;

    public TextComponentFrame() {
        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("PassWorld: ",SwingConstants.RIGHT));
        northPanel.add(passwordField);

        add(northPanel,BorderLayout.NORTH);

        final JTextArea textArea = new JTextArea(TEXTAREA_ROWS,TEXTAREA_COLUMNS);
        JScrollPane scrollPane = new JScrollPane(textArea);

        this.add(scrollPane,BorderLayout.CENTER);

        JPanel southPanel = new JPanel();

        JButton insertButton = new JButton("Insert");
        southPanel.add(insertButton);

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

            }
        });
        add(southPanel,BorderLayout.SOUTH);
        pack();

    }

}

启动类

import java.awt.*;
import javax.swing.*;

public class Hi {

    public static void main(String[] args) {

        //JFrame frame = new JFrame();
        TextComponentFrame frame = new TextComponentFrame();
        frame.setTitle("Hi Java");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

你可能感兴趣的:(Java/Java,Web)