用java做出界面这是Java人员必须要会的,毕竟这是最基础的知识点了,可能有些新手对这块还不是太熟悉,没关系,今天我们就给大家分享一些java做出界面的方法。

在Eclipse中

import一些创建窗体所需要的包import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

继承JFrame类(用来创建窗体所需要的做的必须操作)

LoadingFrame是我自己创建的类public class LoadingFrame extends JFrame{}

复写一个LoadingFrame的方法public LoadingFrame(){}

窗体中所需要的控件就在这个方法里面创建就好了

设置窗体的标题“Load”

设置自定义的布局

设置窗体的大小super("Load");

setLayout(null);

setSize(400, 400);

创建两个标签的实例JLabel username = new JLabel();

username.setText("用户名:"); //设置标签的名字

username.setBounds(100, 150, 60, 30); //设置标签的在窗体中的坐标位置和大小

JLabel password = new JLabel();

password.setText("密码:");

password.setBounds(100, 200, 60, 30);

然后再把这两个标签加入到窗体里面(使用add方法加入)add(username);

add(password);

没错,Java创建窗体的步骤就是比较繁琐的过程,要自己通过代码设定坐标位置、大小,而且还要记得用add()方法加入窗体中,不然的话运行的时候是看不到的(所以后面的这些文本框和按钮之类的也是要用add()方法加入的)

接下来新建文本框JTextField user_name = new JTextField(20);

user_name.setBounds(170, 150, 100, 20);

JPasswordField pass_word = new JPasswordField(20);

pass_word.setBounds(170, 200, 100, 20);

新建按钮JButton btnLoad = new JButton("登录");

btnLoad.setBounds(100, 250, 60, 30);

JButton btnReset = new JButton("重置");

btnReset.setBounds(170, 250, 60, 30);

这样基本的界面就成型了,不过还有一些要设置一下setVisible(true); //设置窗体为可见(不然运行看不到)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗体退出形式

关于按钮的话,就是要设置监听事件(使得按钮在按下之后,能够有反应)

这里我先写了两个按钮监听的实例//登录按钮的监听事件

ActionListener loadAction = new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

// TODO Auto-generated method stub

String name = user_name.getText();

String pass = pass_word.getText();

//判断用户名和密码输入是否正确

if ("admin".equals(name) && "123456".equals(pass))

{

//如果密码正确,则弹出登陆成功的消息框

JOptionPane.showMessageDialog(null, "管理员登录成功", "成功", JOptionPane.PLAIN_MESSAGE);

}

else

{

JOptionPane.showMessageDialog(null, "登录失败!请重试", "失败", JOptionPane.ERROR_MESSAGE);

}

}

};

//重置按钮的监听事件,按下之后把那两个文本框清空

ActionListener resetAction = new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

// TODO Auto-generated method stub

user_name.setText("");

pass_word.setText("");

}

};

然后再加入进去btnLoad.addActionListener(loadAction);

btnReset.addActionListener(resetAction);

所有代码如下:package next.test;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class LoadingFrame extends JFrame

{

public LoadingFrame()

{

super("Load");

setLayout(null);

setSize(400, 400);

//设置标签

JLabel username = new JLabel();

username.setText("用户名:"); //设置标签的名字

username.setBounds(100, 150, 60, 30); //设置标签的在窗体中的坐标位置和大小

JLabel password = new JLabel();

password.setText("密码:");

password.setBounds(100, 200, 60, 30);

//设置文本框

JTextField user_name = new JTextField(20);

user_name.setBounds(170, 150, 100, 20);

JPasswordField pass_word = new JPasswordField(20);

pass_word.setBounds(170, 200, 100, 20);

//设置按钮

JButton btnLoad = new JButton("登录");

btnLoad.setBounds(100, 250, 60, 30);

JButton btnReset = new JButton("重置");

btnReset.setBounds(170, 250, 60, 30);

//设置按钮监听事件

ActionListener loadAction = new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

// TODO Auto-generated method stub

String name = user_name.getText();

String pass = pass_word.getText();

if ("admin".equals(name) && "123456".equals(pass))

{

JOptionPane.showMessageDialog(null, "管理员登录成功", "成功", JOptionPane.PLAIN_MESSAGE);

}

else

{

JOptionPane.showMessageDialog(null, "登录失败!请重试", "失败", JOptionPane.ERROR_MESSAGE);

}

}

};

ActionListener resetAction = new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

// TODO Auto-generated method stub

user_name.setText("");

pass_word.setText("");

}

};

add(username);

add(password);

add(user_name);

add(pass_word);

add(btnLoad);

add(btnReset);

btnLoad.addActionListener(loadAction);

btnReset.addActionListener(resetAction);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args)

{

// TODO Auto-generated method stub

new LoadingFrame();

}

}

这些就是做图形界面的全部过程了,可以看出,做界面还是要花点功夫的,每一步都不能有错哦!最后大家如果想要了解更多java实例知识,敬请关注奇Q工具网。

推荐阅读:

更多推荐

java做界面_java怎么做出界面?实例讲解