简单登陆程序窗口 验证成功跳转计算器
代码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);
}
}