就像我们有像
这样的gui界面import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class login1 extends JFrame implements ActionListener{
JTextField username = new JTextField(15);
JTextField password = new JTextField(15);
JButton login1 = new JButton("login1");
JLabel status = new JLabel("Status:Not logged in");
public login1(){
super("login1:");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
GridBagLayout g = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(g);
c.gridx = 0;
c.gridy = 0;
add(username,c);
c.gridx = 0;
c.gridy = 1;
add(password,c);
c.gridx = 0;
c.gridy = 2;
add(login1,c);
c.gridx = 0;
c.gridy = 3;
add(status,c);
login1.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == login1){
String user = username.getText();
String pass = password.getText();
}
}
public static void main(String[] args){
login1 l = new login1();
}
}
我们可以使用按钮将输入指向与jtextarea相同的面板空间 在输入详细信息并使用按钮提交后,该面板的框架为500,500,我们可以在同一个地方使用500,500 JTextArea输出,并使用jscrolllpane