java登录跳转页面
java文件的编辑方式,任意文本加.java后缀即可,一个java文件只能有一个主类,用public调用,如果要打包成程序包,每个文件前先声明包命,如com.QQH,调试单个文件可以直接java *.java,也可先 javac *.java -encoding UTF-8 生成.class文件后,java *,不需要加class后缀即可自动识别
主类名必须大写并与文件名一致,调试单个文件需注释包名
#javac Window.java
#ls
Window.java Window.class
#java Window
每个文件的类里一般有两个public方法,一个是主方法如initWindow,一个public static void main(String[] args) {}用来初始化类并调用方法(初始化类并调用主方法,,如class Window:Window w1=new Window(),w1.initWindow)若主方法与类名相同,可省略w1.initWindow()
下图是一个简单的窗口实现
//package com.QQH;
import javax.swing.JFrame;
public class Window extends JFrame{
public void initWindow(){
this.setTitle("test window");
this.setSize(600,400);
this.setLocation(0,0);
this.setVisible(true);
}
public static void main(String[] args){
Window w = new Window();
w.initWindow();
}
}
用this.set来设置继承JFrame的Window窗口
接下来来实现一个略为复杂的界面,编辑Main.java
//package com.QQH;
import java.awt.event.ActionEvent; //event
import java.awt.event.ActionListener; //listener
import javax.swing.JButton;
import javax.swing.JFrame; //frame
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
//import tcp.WindowClient;
public class Main {
public static void main(String[] args) {
LoginWindow LW=new LoginWindow();
}
}
class LoginWindow extends JFrame{
private String username="root";
private String password="root";
JFrame window;
JTextField user;
JTextField pwd;
JButton Login;
public LoginWindow(){
window=new JFrame("Login");
window.setLayout(null);
window.setSize(600, 600);//设置大小
window.setLocationRelativeTo(null);//设置居中
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(null);
window.setResizable(false);//设置窗口不可拉伸改变大小
JLabel username_label =new JLabel("user:");
username_label.setBounds(100,100,100,50);
window.add(username_label);
JLabel password_label =new JLabel("password");
password_label.setBounds(100,200,100,50);
window.add(password_label);
user=new JTextField();
user.setBounds(150, 100, 300, 50);
window.add(user);
pwd=new JPasswordField();//隐藏密码
pwd.setBounds(150, 200, 300, 50);
window.add(pwd);
Login=new JButton("Login in");
Login.setBounds(250, 300, 100, 50);
window.add(Login);
window.setVisible(true);//设置面板可见
}
}
可以看到比起上个文本,这个有两个类,主类是Main初始化了继承JFrame的loginWindow,是com.QQH的主页面,先在loginWindow类里对窗口,文本和按钮进行了声明,并private赋值用户名与密码变量,
LoginWindow调用JFrame新建了一个名为Login的窗口,添加了JLabel,user初始化一个JTextField类,密码需要初始化JPasswordField类,步骤都是先初始化一个类,再往window窗口添加,并设置范围,最后一定要加上显示面板可见的setVisible(true)
参考以下博客
https://blog.csdn.net/qs17809259715/article/details/88936619ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160706214819726885871262%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=160706214819726885871262&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_click~default-1-88936619.first_rank_v2_pc_rank_v29&utm_term=java%E4%BA%8B%E4%BB%B6%E7%9B%91%E5%90%AC
可以把下面的直接运行试试
编辑ActionListenerSelf.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ActionListenerSelf implements ActionListener{
public static void main(String[] args) {
ActionListenerSelf als = new ActionListenerSelf();
als.init();
}
public void init(){
JFrame f = new JFrame();
JPanel p = new JPanel();
JButton btn = new JButton("click me");
btn.addActionListener(this);
p.add(btn);
f.add(p);
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
System.out.println("event 1");
}
}
编辑ActionListenerInner.java
//package com.QQH
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ActionListenerInner {
public static void main(String[] args) {
ActionListenerInner ali = new ActionListenerInner();
ali.init();
}
public void init(){
JFrame f = new JFrame();
JPanel p = new JPanel();
JButton btn = new JButton("click me");
InterfaceDemoB b = new InterfaceDemoB();
btn.addActionListener(b);
p.add(btn);
f.add(p);
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public class InterfaceDemoB implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("event 2");
}
}
}
编辑AnonymousInner.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AnonymousInner {
public static void main(String[] args) {
AnonymousInner ani = new AnonymousInner();
ani.init();
}
public void init(){
JFrame f = new JFrame();
JPanel p = new JPanel();
JButton btn = new JButton("click me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("AnonymousInner");
}
});
p.add(btn);
f.add(p);
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
接下来实现一个引用了外部类的事件监听方式,可以把引用类打包成jar文件放到java的决定包的jre的libext里方便引用,也可放一个文件夹下
编辑ExternelclassListener.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ExternelclassListener {
public static void main(String[] args) {
JFrame f = new JFrame(); //创建窗体
JPanel p = new JPanel(); //创建面板
JButton btn = new JButton("点我"); //创建一个按钮
ExternelInterface a = new ExternelInterface(); //初始化外部类当接口事件
btn.addActionListener(a);//给bnt添加监听并把新建的事件当触发事件
p.add(btn);
f.add(p);
f.setSize(400,300); //设置窗体的大小
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗体的后台
f.setVisible(true); //设置窗体可见
}
}
编辑ExternelInterface.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ExternelInterface implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("execternelListener");
}
}
一般直接直接编译ExternelclassListener.java文件就能直接调用,接口文件与类名正确的话,会把ExternelInterface.java文件一起编译成class文件
接着实现一个带跳转的页面如调用LoginWindow.java,在接口类加入LoginWindow LW=new LoginWindow();
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ExternelInterface implements ActionListener {
public void actionPerformed(ActionEvent e) {
LoginWindow LW=new LoginWindow();
}
}
成功跳转