简单登陆程序窗口 验证成功跳转计算器

简单登陆程序窗口 验证成功跳转计算器
代码1
要放入一个包里面小白一枚欢迎提出更好更简单代码

package day04zuoyan;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class admin {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Login i=new Login(" 登陆");

        }

    }
    class Login extends JFrame{

        private JLabel lab1;
        private JLabel lab2;
        private JLabel lab3;
        private JPanel jp1;
        private JPanel jp2;
        private JPanel jp3;
        private JPanel jp4;
        private JTextField jf1;
        private JPasswordField jf2;
        private JButton jb1;
        private JButton jb2;
        //Monitor m=new Monitor();
        Login(String s){
            super(s);
            lab1=new JLabel("系统管理员登录");
            lab2=new JLabel("用户名");
            lab3=new JLabel("密码    ");
            jf1=new JTextField(12);
            jf2=new JPasswordField(12);
            jp1=new JPanel();
            jp2=new JPanel();
            jp3=new JPanel();
            jp4=new JPanel();
            jb1=new JButton("确定");
            jb1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    String username=jf1.getText();
                    String pwd=new String(jf2.getPassword());
                    if(username.equals("admin")&&pwd.equals("password")){
                        dispose();
                        new zuoyan();
                        setSize(280,300);
                        setResizable(false);
                    }
                    else
                        JOptionPane.showMessageDialog(null, "再好好想想吧", "账户或密码不对", JOptionPane.ERROR_MESSAGE);

                }
            });
            jb2=new JButton("取消");
            jb2.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    jf1.setText(" ");
                    jf2.setText("");
                }
            });
            this.setLayout(new GridLayout(4,1));
            add(jp1);
            add(jp2);
            add(jp3);
            add(jp4);
            jp1.add(lab1);
            jp2.add(lab2);
            jp3.add(lab3);
            jp2.add(jf1);
            jp3.add(jf2);
            jp4.add(jb1);
            jp4.add(jb2);
            this.setBounds(0,0,420,310);
            setLocationRelativeTo(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }



    }


代码2

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Operators;


public class zuoyan extends JFrame implements ActionListener {
    private final String[]KEYS={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
    private final String[]Command={"delete","C"};
    private JButton[] keys=new JButton[KEYS.length];
    private JButton[] coms=new JButton[Command.length];
    private JTextField text=new JTextField("0");
    private boolean Firstdigit=true;
    private double result=0.0;
    private String operator = "=";//默认操作为"="
    private boolean operateValidFlag = true;//操作是否合法
    public zuoyan() {
        init();
        this.setTitle("计算器");
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //this.setSize(width, height)
        this.pack();
    }
    private void init() {
        // TODO Auto-generated method stub
        text.setHorizontalAlignment(JTextField.RIGHT);
        text.setBackground(Color.white);
            text.setEditable(false);
            JPanel keyspanel=new JPanel();//装按键的画板
            keyspanel.setLayout(new GridLayout(4,4,3,3));//布局为网格布局4行4列 水平竖直间距都为3像素
            for(int i=0;i<keys.length;i++){//创建按钮
                keys[i]=new JButton(KEYS[i]);
                keyspanel.add(keys[i]);//在画板上添加按钮
                keys[i].setForeground(Color.blue);//按键前景色蓝色
        }
        //运算符按键前景色为红
        keys[3].setForeground(Color.red);
        keys[7].setForeground(Color.red);
        keys[11].setForeground(Color.red);
        keys[14].setForeground(Color.black);
        keys[15].setForeground(Color.red);
        JPanel comspanel=new JPanel();
        comspanel.setLayout(new GridLayout(1,2,3,3));
        for(int i=0;i<coms.length;i++){
            coms[i]=new JButton(Command[i]);
            comspanel.add(coms[i]);
        }
        JPanel panel1=new JPanel();
        panel1.setLayout(new BorderLayout(3,3));
        panel1.add("North", comspanel);
        panel1.add("Center", keyspanel);
        JPanel top = new JPanel();  //装文本框的画板
        top.setLayout(new BorderLayout()); //边界布局
        top.add("Center", text);
        getContentPane().setLayout(new BorderLayout(3,5));
        getContentPane().add("North", top);  //北  放置top画板 里面是文本框
        getContentPane().add("Center", panel1);  //中心  放置panel1画板 装的是数字按键和命令按键
        for (int i = 0; i < KEYS.length; i++) {
            keys[i].addActionListener(this);
        }
        for (int i = 0; i < Command.length; i++) {
            coms[i].addActionListener(this);
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String label=e.getActionCommand();
        if(label==Command[0]){
            handleDelete();
        }
        else if(label==Command[1]){
            handLeC();
        }
        else if("0123456789.".indexOf(label)>=0){
            handlenumber(label);
        }
        else{
            handleOperator(label);
        }
    }
    private void handleOperator(String label) {
        // TODO Auto-generated method stub
        if(operator.equals("/")){
            if(text.getText()=="0"){
                text.setText("除数不能为0");
                operateValidFlag=false;
                handLeC();
            }
            else{
                result/=Double.valueOf(text.getText());
            }
        }
        else if(operator.equals("*")){
            result*=Double.valueOf(text.getText());
        }
        else if(operator.equals("+")){
            result+=Double.valueOf(text.getText());
        }
        else if(operator.equals("-")){
            result-=Double.valueOf(text.getText());
        }
        else if(operator.equals("=")){
            result=Double.valueOf(text.getText());
        }
        if(operateValidFlag){
            text.setText(String.valueOf(result));
        }
        operator=label;
        Firstdigit = true;//按完运算符后 在输入数字为第一次输入
        operateValidFlag=true;
    }
    private void handlenumber(String label) {
        // TODO Auto-generated method stub
        if(Firstdigit){
            if(label.equals(".")){
                text.setText(text.getText()+".");
            }
            else{
                text.setText(label);
            }
        }
        //按的是"."并且字符串里没"."
        else if(label.equals(".")&&text.getText().indexOf(".")<0){
            text.setText(text.getText()+".");
        }
        //按得是数字
        else if(!label.equals(".")){
            text.setText(text.getText()+label);
        }
        Firstdigit=false;//输过就不是第一次输入了
    }
    private void handLeC() {
        // TODO Auto-generated method stub
        text.setText("0");
        Firstdigit=true;
        operator="=";
    }
    private void handleDelete() {
        // TODO Auto-generated method stub
        if(text.getText().length()>1){
            text.setText(text.getText().substring(0, text.getText().length()-1));
        }
        else if(text.getText().length()==1){
            text.setText("0");
        }
    }
    public static void main(String args[]) {
        zuoyan calculator1 = new zuoyan();
        calculator1.setSize(280,300);
        calculator1.setResizable(false);
//       zuoyan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

一个简易而美观的网页登陆注册页面 部分代码 Login Html: <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="user.css"> </head> 0802_0103080303_非思帆_web 计算器实验报告 <body class="body_L"> <form action ="login.php" method="POST"/> <h1 align="center">用户登录界面</h1> <hr> <table align="center"> <tr> <td class="words" >username</td><td><input type="text" value="" id="username" name="username" /> </td> </tr> <tr> <td class="words"> password</td><td><input type="password" value="" id="password" name="password"/></td> </tr> <tr> <td class="words"><input type="submit" style="width: 65px;height: 25px;background-color: salmon"class="bt_words"value="login" /></td> <td class="words"><a href="register.html"> 注册新用户 </a></td> </tr> </table> </body> </html> login php: <?php $connstr = "host=127.0.0.1 port=5432 dbname=student user=postgres password=1990123"; $conn = pg_connect($connstr); //if(!$conn){ // echo "can't connect to database"; //} else { 0802_0103080303_非思帆_web 计算器实验报告 // echo "connect success"; //} $username = $_POST['username']; $password = $_POST['password']; $sql = "select count(1) from st_user where username=$1 and password=$2"; $params = array(); $params[0] = $username; $params[1] = $password; $result = pg_query_params($conn, $sql, $params); if (!$result) { echo pg_last_error($conn); } $row = pg_fetch_row($result); if ($row[0] == 1) { echo "login success!"; echo "<Script>alert('login success');window.self.location='caculate.html';</Script>"; } else { echo "login failed!please return back!"; echo " <script> alert( '您的用户名密码有错误,请重新登陆。 ') ; location.href= 'login.html '; </script> " ; } pg_free_result($result); ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

左魇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值