📄 customerapplet.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.applet.*;
class Customer implements Serializable
{
String custName;
String custPassword;
}
public class CustomerApplet extends JApplet
{
JPanel panelObject;
JLabel labelCustName;
JLabel labelCustPassword;
JTextField textCustName;
JPasswordField textCustPassword;
JButton buttonLogin;
GridBagLayout gb;
GridBagConstraints gbc;
public CustomerApplet()
{
gb=new GridBagLayout();
gbc=new GridBagConstraints();
panelObject=(JPanel)getContentPane();
panelObject.setLayout(gb);
labelCustName=new JLabel("Customer Login Name");
labelCustPassword=new JLabel("Password");
textCustName=new JTextField(15);
textCustPassword=new JPasswordField(15);
buttonLogin=new JButton("Login");
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=5;
gb.setConstraints(labelCustName,gbc);
panelObject.add(labelCustName);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=5;
gb.setConstraints(textCustName,gbc);
panelObject.add(textCustName);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=9;
gb.setConstraints(labelCustPassword,gbc);
panelObject.add(labelCustPassword);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=9;
gb.setConstraints(textCustPassword,gbc);
panelObject.add(textCustPassword);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=2;
gbc.gridy=13;
gb.setConstraints(buttonLogin,gbc);
panelObject.add(buttonLogin);
LoginAction loginrequest=new LoginAction();
buttonLogin.addActionListener(loginrequest);
}
class LoginAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==buttonLogin)
{
Customer data=new Customer();
data.custName=textCustName.getText();
data.custPassword=new String(textCustPassword.getPassword());
try
{
Socket toServer=new Socket("127.0.0.1",8080);
BufferedReader fromServer=new BufferedReader(new InputStreamReader(toServer.getInputStream()));
ObjectOutputStream streamToServer=new ObjectOutputStream(toServer.getOutputStream());
streamToServer.writeObject((Customer)data);
String status=fromServer.readLine();
getAppletContext().showStatus(status);
streamToServer.close();
fromServer.close();
}
catch(InvalidClassException e)
{
System.out.println("The Customer class is invalid"+e);
}
catch(NotSerializableException e)
{
System.out.println("The object is not serializable"+e);
}
catch(IOException e)
{
System.out.println("Cannot write to the server"+e);
}
}
}
}
public static void main(String args[])
{
CustomerApplet obj=new CustomerApplet();
obj.setSize(400,300);
obj.setVisible(true);
}
}
//<applet code="CustomerApplet.class" height=300 width=300>
//</applet>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -