Development of SimQQ interface

Development of SimQQ interface

LoginUI.Java

First of all to write an interface, we need to use the form.
Here we will use the previously mentioned classes and objects.
1. First create a package with the name SimQQ
Insert picture description here
2. Then create the first LoginUI class
Insert picture description here
3. Define a method to display the form in the class.
public void showUI(){ }
4. Develop a good habit, write the main method below, and call our method, then you will use the class and object.

public static void main (String[] args) {
  LoginUI ui = new LoginUI();
  ui.showUI();
  
}

5. Now that the foundation is ready, we can start a real performance.
In our showUI, we first create a form, where we use the built-in class in Java.
JFrame loginFrame = new JFrame();
At this time, our program already has an interface , Click to run, we will be shocked to find that nothing appears.
Don't be nervous, this is because we haven't set the form visible.

   //设置窗体可见
 	loginFrame.setVisible(true);
  加上这一句,窗体就出现了,既然有设置窗体可见与否的方法,那么联想一下,当然就会有设置窗体的大小啊,布局啊等等方法。
  这一句,放在最后,我们把其他的操作,放在它之前,等我们把所有的操作写完之后,再使窗体可见,才会出现我们理想中的效果,如果不这样做,会出现什么呢?
   我们知道,Java的程序执行,一般是从上到下执行,如果你在创建窗体之后立即使窗体可见,你就会立即见到一个干净的窗体,但是你之后进行的操作,就不会再出现,因为它们“错过了”窗体可见的这句方法。
   之后啊我们就可以对这个窗体进行随便的操作啦。
   比如
//设置窗体大小
loginFrame.setSize(500, 900);
//设置窗体的关闭
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   //设置窗体出现的位置
   loginFrame.setLocationRelativeTo(null);
   这里为什么要写null呢,因为我们可以看到RelativeTo这个词组,也就是和什么有联系的意思,null自然是和什么都没联系的意思,和什么都没联系,那么窗体就居中了。

6. Set the form layout. Here we use the simplest flow layout. It
is the same to create the flow layout object, and then use

//设置布局
   FlowLayout layout = new FlowLayout();
   loginFrame.setLayout(layout);
   

7. Add pictures to the form

//设置图片
	ImageIcon image = new ImageIcon("Bronzeware.png");
	JLabel iconLable = new JLabel(image);
	loginFrame.add(iconLable);

"Bronzeware.png" here is the location of a picture in our computer. Of course, we can directly drag the picture into eclipse so that we don't have to find the location so much. Of course, you can also directly copy the file location.

8. Take a look at our QQ interface, what's on it? Account box, password box, login button, etc.

These classes to be used can be found in our API documentation

//设置账号框
	JTextField name = new JTextField();
	Dimension inputSize = new Dimension(350,40);
	name.setPreferredSize(inputSize);
	name.setText("请输入账号");
	loginFrame.add(name);
	
	
	//设置标签
		JLabel registerLabel2 = new JLabel("注册账号");
		Font font = new Font("宋体", Font.BOLD, 20);
		registerLabel2.setForeground(Color.BLUE);
		registerLabel2.setBackground(Color.WHITE);
		registerLabel2.setFont(font);
	 	loginFrame.add(registerLabel2);
	
	 	
	//设置密码框
	JPasswordField word = new JPasswordField();
	Dimension inputSize2 = new Dimension(350,40);
	word.setPreferredSize(inputSize2);
	word.setText("请输入密码");
	loginFrame.add(word);
	
	
	//设置标签
	JLabel registerLabel = new JLabel("注册账号");
	Font font1 = new Font("宋体", Font.BOLD, 20);
	registerLabel.setForeground(Color.BLUE);
	registerLabel.setBackground(Color.WHITE);
	registerLabel.setFont(font1);
 	loginFrame.add(registerLabel);
 	
 	
 	//设置复选框
 	JCheckBox box = new JCheckBox("记住密码");
 	loginFrame.add(box);
 	JCheckBox box1 = new JCheckBox("自动登陆");
 	loginFrame.add(box1);
 	
 	//设置按钮
 	JButton btn = new JButton("登陆");
 	loginFrame.add(btn);

After the above operation, you can probably see a common login interface, but we will find that clicking the button inside, nothing happens. This is because our mouse actions have not been obtained. Here we will introduce a concept called the mouse listener. .

I wo n’t go into more details here. The following article will introduce that as long as you know it roughly means getting our mouse actions, such as clicking, releasing, etc.

LoginListener.Java

9. Add mouse listener

Here we create a new class
Insert picture description here
and

public class LoginListener implements ActionListener{

}

The implementation here is the meaning of the interface. As for what the interface is, I won't go into details. The same article will mention later that it is simply used to connect to the mouse listener ActionListener.

Next we start to write our content

public class LoginListener implements ActionListener{
java.lang.String  s="abc";
		//获取输入框的内容
	    String name = input.getText();
	    String word = psd.getText();
	    if(name.equals("123456")&&word.equals("123")) {
	    	System.out.println("登陆成功");
         	Frame.dispose();
//	    	Frame.setVisible(false);
	    	
	    }else {
	    	JOptionPane.showMessageDialog(null, "登陆失败",

	     			"登陆失败", JOptionPane.INFORMATION_MESSAGE);
			System.out.println("登录失败!");
		}
		
	}
}

10. Next we add our listener object in our interface, which is LoginUI

//创建监听器
 	LoginListener loginL = new LoginListener();
 	btn.addActionListener(loginL);
 	loginL.input = name;
 	loginL.psd = word;
 	loginL.Frame=loginFrame;

Click Run, our simple login interface is complete ~

Now to sort out the ideas, here we define two classes, one is the interface class, the other is the listener class, we set the style of our interface in the interface class, then write our listening method in the listener class, and finally in Add the listener class object to the interface class and call it, and you are done.

over

Published 13 original articles · Like1 · Visits 306

Guess you like

Origin blog.csdn.net/Alagagaga/article/details/103091836