「经典java界面」JAVA界面设计
本篇文章给大家谈谈经典java界面,以及JAVA界面设计对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java 求一个好的登录界面
效果如图:
代码如下:
不懂的可以继续问我
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import entity.*;
import dao.*;
public class Login extends JFrame implements ActionListener {
userBean user=new userBean();
String name;
String password;
private JPanel jp = new JPanel();
private JLabel label=new JLabel();
private JLabel[] jlArr = { new JLabel("用户名:"), new JLabel("密码:"),new JLabel("") };
private JButton[] jbArr = { new JButton("登录"), new JButton("重置") };
private JTextField jt = new JTextField();
private JPasswordField pwd = new JPasswordField();
public Login() {
jp.setLayout(null);
for (int i = 0; i 2; i++) {
jlArr[i].setBounds(30, 20 + i * 50, 80, 26);
jbArr[i].setBounds(50 + i * 100, 130, 80, 26);
jp.add(jlArr[i]);
jp.add(jbArr[i]);
jbArr[i].addActionListener(this);
}
jt.setBounds(80, 20,180,30);
jp.add(jt);
jt.addActionListener(this);
pwd.setBounds(80,70,180,30);
jp.add(pwd);
pwd.setEchoChar('*');
pwd.addActionListener(this);
jlArr[2].setBounds(10,180,300,30);
jp.add(jlArr[2]);
String url="1.png";
label.setIcon(new ImageIcon(url));
this.add(jp);
this.setTitle("库存管理用户登录");
this.setResizable(false);
this.setBounds(100,100,300,250);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource()==jt){
jt.requestFocus();
}else if(e.getSource()==jbArr[1]){
jlArr[2].setText("");
jt.setText("");
pwd.setText("");
jt.requestFocus();
}else{
name=jt.getText();
password=String.valueOf(pwd.getPassword());
user.setName(name);
user.setPassword(password);
try {
if(daofactory.getuserInstance().login(user)){//这里是我自己的查询数据库用户名和密码是否正确的方法
jlArr[2].setText("登录成功!");
}else{jlArr[2].setText("请输入正确的用户名和密码!");}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args){
new Login();
}
}
java GUI 登陆界面
界面代码写的不错 其实连数据库也不复杂。 如果希望不转换界面,登陆和登陆成功或失败后的信息在同意界面 可以设置个int类型的变量 来控制显示信息
下面是按你的要求写的 现实界面的转换 如果用户名和密码正确进入MyFrame1窗口 如果不正确进入MyFrame2窗口。
连数据库的时候先添加数据库驱动包,如果连接数据库有问题 多打印一下相关数据 很容易发现错误之处的
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class 登陆界面 extends JFrame {
private static final long serialVersionUID = 1L;
private JPasswordField passwordField;
private JTextField textField;
public static void main(String args[]) {
try {
登陆界面 frame = new 登陆界面();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public 登陆界面() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String dir = "com.mysql.jdbc.Driver";//数据库驱动类,我这是MySQL的 ,连数据库记得导入驱动的jar包
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try{
Class.forName(dir);
String url = "jdbc:mysql://localhost:3306/hospital?user=rootpassword=root";//数据库名 hospital用户名和密码都是root
conn = DriverManager.getConnection(url);
stat = conn.createStatement();
//得到用户名
String name = textField.getText().trim();
//得到密码
char[] pass = passwordField .getPassword();
String password = new String(pass);
//输出用户名密码
System.out.println(name);
System.out.println(password);
//数据库表user,字段用户名为username,密码password,都是varchar型
String sql = "select username,password from user where username = '" + name + "'";
rs = stat.executeQuery(sql.toString());
if(rs.next()){
//获得数据库里面的用户名和密码
String sname = rs.getString("username");
String spass = rs.getString("password");
//对输入的用户名和密码和数据库里的进行判断
if((rs != null ) (name.equals(sname)) (password.equals(spass))) {
//登陆成功释放登陆界面资源,显示a窗口
登陆界面.this.dispose();
new MyFrame1().launch();
System.out.println("chenggong");
} else {
//密码错误,显示b窗口
登陆界面.this.dispose();
new MyFrame2().launch();
System.out.println("mimacuowu");
}
} else {
//用户名错误和其他情况,显示b窗口
登陆界面.this.dispose();
new MyFrame2().launch();
System.out.println("yonghumingcuowu");
}
}catch(Exception e1){
//数据库连接失败,显示b窗口
登陆界面.this.dispose();
new MyFrame2().launch();
System.out.println("数据库连接错误");
}finally{
try{
if(rs!=null) rs.close();
if(stat!=null) stat.close();
if(conn!=null) conn.close();
}catch(Exception e1){
}
}
}
});
button.setText("确定");
button.setBounds(62, 273, 101, 25);
getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {System.exit(0);
}
});
button_1.setText("取消");
button_1.setBounds(325, 273, 101, 25);
getContentPane().add(button_1);
final JLabel label = new JLabel();
label.setText("用户名");
label.setBounds(23, 60, 60, 15);
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setText("密码");
label_1.setBounds(23, 172, 60, 15);
getContentPane().add(label_1);
textField = new JTextField();
textField.setBounds(48, 115, 90, 21);
getContentPane().add(textField);
passwordField = new JPasswordField();
passwordField.setBounds(42, 216, 96, 25);
getContentPane().add(passwordField);
}
}
class MyFrame1 extends Frame {
int orgX1;
int orgY1;
int orgX2;
int orgY2;
public void launch() {
setBounds(500,500,500,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
System.exit(0);
}
});
}
public void paint(Graphics g) {
//Color c =g.getColor();
g.setColor(Color.red);
g.drawLine(100, 50, 100, 80);
}
}
class MyFrame2 extends Frame {
int orgX1;
int orgY1;
int orgX2;
int orgY2;
public void launch() {
setBounds(500,500,500,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
System.exit(0);
}
});
}
public void paint(Graphics g) {
//Color c =g.getColor();
g.setColor(Color.red);
g.draw3DRect(100, 100, 100, 100,true);
}
}
怎么用java写一个界面怎么用java写一个界面
新建一个窗口,然后实现一个关闭按钮”窗口的功能
import java.awt.*;
import java.awt.event.*;
public class TestWindowEvent {
public static void main (String[] args) {
new Frame88 ("WindowAdapter");
}
}
class Frame88 extends Frame {
Frame88 (String s) {
super(s);
setBounds (300,300,200,70);
setLayout (null);
setVisible (true);
addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible (false);
System.exit(0);
}
} );
}
}
java图形界面的布局
没用过SWT的人路过,提供一个SWING:
窗口用JFrame(frame),布局为BorderLayout.
顶部为JLabel(labelTop),带图片和文字,用frame.add(labelTop, BorderLayout.NORTH)加入
左面为JPanel(panelLeft),布局为BoxLayout,参数Y_AXIS.需要在创建JPanel时设置Layout为NULL,然后再创建BoxLayout时需要把已经建立的panelLeft作为参数传进去,然后再设置panelLeft的Layout为那个BoxLayout。用frame.add(panelLeft, BorderLayout.WEST)加入
中间为一堆不同功能的JPanel,布局根据功能而定了,在点击按钮时,移除frame此时中间的panel(记录在一个自己的变量panelCenter里),然后加入按钮对应的panel,再把加入的panel存入变量panelCenter.
Java 界面
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class InfoManagementGUI extends JFrame {
public InfoManagementGUI() {
getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 250, 86);
getContentPane().add(panel);
panel.setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel = new JLabel("\u4FE1\u606F\u7BA1\u7406\uFF08\u5355\u7EAF\u6587\u5B57\uFF09");
lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 12));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(lblNewLabel, BorderLayout.CENTER);
JPanel panel_1 = new JPanel();
panel_1.setBounds(10, 106, 250, 255);
getContentPane().add(panel_1);
panel_1.setLayout(null);
JButton btnNewButton = new JButton("\u529F\u80FD\u4E00\u6309\u94AE");
btnNewButton.setBounds(41, 29, 167, 31);
panel_1.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u529F\u80FD\u4E8C\u6309\u94AE");
btnNewButton_1.setBounds(41, 70, 167, 31);
panel_1.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("\u529F\u80FD\u4E09\u6309\u94AE");
btnNewButton_2.setBounds(41, 111, 167, 31);
panel_1.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("\u529F\u80FD\u56DB\u6309\u94AE");
btnNewButton_3.setBounds(41, 152, 167, 31);
panel_1.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("\u529F\u80FD\u4E94\u6309\u94AE");
btnNewButton_4.setBounds(41, 193, 167, 31);
panel_1.add(btnNewButton_4);
JPanel panel_2 = new JPanel();
panel_2.setBounds(270, 10, 156, 213);
getContentPane().add(panel_2);
panel_2.setLayout(null);
JButton btnNewButton_5 = new JButton("\u6587\u5B57\u6309\u94AE\u4E00");
btnNewButton_5.setBounds(10, 10, 136, 89);
panel_2.add(btnNewButton_5);
JButton btnNewButton_6 = new JButton("\u6587\u5B57\u6309\u94AE\u4E8C");
btnNewButton_6.setBounds(10, 109, 136, 94);
panel_2.add(btnNewButton_6);
JPanel panel_3 = new JPanel();
panel_3.setBounds(270, 236, 156, 125);
getContentPane().add(panel_3);
panel_3.setLayout(null);
JButton btnNewButton_7 = new JButton("\u5173\u4E8E");
btnNewButton_7.setBounds(31, 10, 93, 28);
panel_3.add(btnNewButton_7);
JButton btnNewButton_8 = new JButton("\u5E2E\u52A9");
btnNewButton_8.setBounds(31, 48, 93, 28);
panel_3.add(btnNewButton_8);
JButton btnNewButton_9 = new JButton("\u9000\u51FA");
btnNewButton_9.setBounds(31, 86, 93, 29);
panel_3.add(btnNewButton_9);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setSize(452, 408);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public static void main(String[] args) {
new InfoManagementGUI();
}
}
运行结果:
关于经典java界面和JAVA界面设计的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-30,除非注明,否则均为
原创文章,转载请注明出处。