⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginframe.java

📁 JAVA编写的聊天小程序!!程序默认需放到D:下
💻 JAVA
字号:
package group.client;

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

public class LoginFrame extends JFrame
{
	private Client client;

	private JLabel logIDLabel;
	private JLabel logPassLabel;
	private JTextField logIDText;
	private JPasswordField logPassText;
	private JButton loginButton;
	private JButton regButton;
	private JButton cancelButton;
	private JPanel logPane1;

	public LoginFrame(Client c)
	{
		super("用户登录");
		setSize(280, 190);

		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;

		int localX = (screenWidth - getSize().width) / 2;
		int localY = (screenHeight - getSize().height) / 2;
		setLocation(localX, localY);
		setResizable(false);						//把窗口设为不可编辑,主要是为了界面

		Font f = new Font("Serif", Font.BOLD, 14);
		logIDLabel = new JLabel("     I     D     : ");
		logIDLabel.setFont(f);
		logPassLabel = new JLabel("     密   码    : ");
		logPassLabel.setFont(f);
		logIDText = new JTextField(13);
		logIDText.setFont(f);
		logPassText = new JPasswordField(13);
		logPassText.setFont(f);
		loginButton = new JButton("登 录");
		loginButton.setFont(f);
		regButton = new JButton("注 册");
		regButton.setFont(f);
		cancelButton = new JButton("取 消");
		cancelButton.setFont(f);

		logPassText.addFocusListener(new FocusAdapter()
			{
				public void focusGained(FocusEvent e)
				{
					logPassText.setText("");
				}
			});

		LoginAction listener = new LoginAction();
		logIDText.addActionListener(listener);
		logPassText.addActionListener(listener);
		loginButton.addActionListener(listener);
		regButton.addActionListener(listener);
		cancelButton.addActionListener(listener);

		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();

		logPane1 = new JPanel();
		logPane1.setLayout(gbl);

		Icon icon = new ImageIcon("bird2.gif");
		add(logPane1, new JLabel(icon), gbc, 0, 0, 9, 1, 0, 20);
		add(logPane1, logIDLabel, gbc, 1, 1, 3, 1, 0, 5);
		add(logPane1, logIDText, gbc, 4, 1, 5, 1, 0, 0);
		add(logPane1, logPassLabel, gbc, 1, 2, 3, 1, 0, 5);
		add(logPane1, logPassText, gbc, 4, 2, 5, 1, 0, 0);
		add(logPane1, loginButton, gbc, 1, 3, 2, 1, 0, 10);
		add(logPane1, cancelButton, gbc, 4, 3, 2, 1, 0, 0);
		gbc.anchor = GridBagConstraints.EAST;
		add(logPane1, regButton, gbc, 7, 3, 2, 1, 0, 0);

		Container pane = getContentPane();
		pane.add(logPane1, BorderLayout.CENTER);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		client = c;
	}

	private void add(Container c, Component c2, GridBagConstraints gbc, int gridx, int gridy,
		int gridwidth, int gridheight, int weightx, int weighty)
	{
		gbc.gridx = gridx;
		gbc.gridy = gridy;
		gbc.gridwidth = gridwidth;
		gbc.gridheight = gridheight;
		gbc.weightx = weightx;
		gbc.weighty = weighty;
		c.add(c2, gbc);
	}

/*	public static void main(String[] args)
	{
		LoginFrame f = new LoginFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.show();

	}
*/
	private class LoginAction implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			JComponent tmp = (JComponent)e.getSource();

			if(tmp == logIDText)
			{
				logPassText.requestFocus();
			}
			else if((tmp == loginButton) || (tmp == logPassText))
			{
				client.loginInfo(logIDText.getText(), new String(logPassText.getPassword()));
				LoginFrame.this.hide();
			}
			else if(tmp == regButton)
			{
				LoginFrame.this.hide();
				client.startReg();
			}
			else if(tmp == cancelButton)
			{
				System.exit(0);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -