java写出图形界面

1. 做出简单的窗体

package javaGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame; //导入包  JFrame类在swing包中
import javax.swing.JPanel;

import java.awt.Container;


public class face extends JFrame{
	
	public face(){ //定义了一个构造函数
		this.setTitle("汽车销售信息管理系统");
		Container con = this.getContentPane();
		con.setLayout(new BorderLayout());
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.GRAY);
		JButton bt = new JButton("click");
		
		panel.add(bt);
		
		con.add(panel, BorderLayout.SOUTH);
		
		this.setVisible(true);
		this.setSize(600, 450);
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		new face(); //使用构造生成
		
	}

}

 

2. 登陆界面

package CarGUI;

import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
 
/**
 *登录界面 
 * @author jiang
 */
public class GUIcar extends JFrame {
	
	String admin="admin";
	String pw="admin";
    //用户名
    private JTextField username;//用户名的文本框
    //密码
    private JPasswordField password;//密码的文本框
    //小容器
    private JLabel jl1;
    private JLabel jl3;
    private JLabel jl4;
    
    //小按钮
    private JButton bu1;
    private JButton bu2;
    
    /*
     * 构造方法
     */
    public GUIcar() {
        // 设置窗口标题
        this.setTitle("用户登录");
        // 窗体组件初始化
        init();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置布局方式为绝对定位
        this.setLayout(null);
         
        this.setBounds(0, 0, 355, 265);
         
        // 窗体大小不能改变
        this.setResizable(false);
         
        // 居中显示
        this.setLocationRelativeTo(null);
 
        // 窗体可见
        this.setVisible(true);
    }
 
    /*
     * 初始化方法
     */
    public void init() {
        // 创建一个容器
        Container con = this.getContentPane();
        jl1 = new JLabel();
        // 设置背景图片
        jl1.setBounds(0, 0, 355, 265); 
        
        
        jl3 = new JLabel("用户名");
        jl3.setBounds(50, 70, 70, 20);
        // 用户号码登录输入框
        username = new JTextField(15);
        username.setBounds(100, 70, 150, 20);
        // 用户号码登录输入框旁边的文字
        
        jl4 = new JLabel("密码");
        jl4.setBounds(55, 100, 70, 20);
        // 密码输入框
        password = new JPasswordField(15);
        password.setBounds(100, 100, 150, 20);
 
 
        // 按钮设定
        bu1 = new JButton("登录");
        bu1.setBounds(80, 200, 65, 20);
        // 给按钮添加1个事件
        bu1.addActionListener(new ActionListener() {
             
            public void actionPerformed(ActionEvent e) {
                String str=e.getActionCommand();
                if("登录".equals(str)){
                	
                    String getName =username.getText().trim();
                    String getPw =password.getText().trim();
                    
                    System.out.println(getName);
                    System.out.println(getPw);
                    
                    
                    if( getName.equals("admin")&&getPw.equals("admin") ){//此处应该是执行一个SQL语句查询是否是否正确
                    	JOptionPane.showMessageDialog(null, "登录成功");
                    	//跳转执行 主程序页面
                    }else{
                    	System.out.println(getPw);
                    	JOptionPane.showMessageDialog(null, "登录失败 重新尝试");
                    }
                }
            }
        });
        
        bu2 = new JButton("退出");
        bu2.setBounds(190, 200, 65, 20);
        
        bu2.addActionListener(new ActionListener() {
        	
        	public void actionPerformed(ActionEvent e) {
        		//关闭登录界面
        		System.exit(0);//退出程序
        	}
        });
        
        // 所有组件用容器装载
        jl1.add(jl3);
        jl1.add(jl4);
        jl1.add(bu1);
        jl1.add(bu2);
        con.add(jl1);
        con.add(username);
        con.add(password);
    }
    
    public static void main( String[] args) {
        //实例化对象
        GUIcar qq = new GUIcar();
        
    }
}
原文地址:https://www.cnblogs.com/yspworld/p/4813861.html