求JAVA窗口小程序

求JAVA窗口小程序,第1张

这是我以前的课程设计,我把代码摘录一部分出来,希望对你有帮助!

/**

*MyFrame.java

*author:the fourth group

*version: 2007/1/27

*说明:该程序负责系统登陆框的设置和实

*该应用程序为首先显示的程序

*/

package com.java

import java.awt.Container

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.ItemEvent

import java.awt.event.ItemListener

import javax.swing.ButtonGroup

import javax.swing.ImageIcon

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JOptionPane

import javax.swing.JPasswordField

import javax.swing.JRadioButton

import javax.swing.JTextField

//import java.util.*

public class MyFrame implements ActionListener ,ItemListener{

//登陆界面属性

String userName

Container con

JLabel lab1,lab2,lab3,lab4

JTextField tf1

JPasswordField tf2

JRadioButton cb1,cb2,cb3

JButton bu1,bu2,bu3

JFrame fr1

public MyFrame(){

fr1=new JFrame()

con=fr1.getContentPane()

con.setLayout(null)

JLabel lab1=new JLabel("账号")

lab1.setBounds(20,70,50,20)

con.add(lab1)

lab2=new JLabel("密码")

lab2.setBounds(20,100,50,20)

con.add(lab2)

tf1=new JTextField()

tf1.setBounds(80,70,160,20)

con.add(tf1)

tf2=new JPasswordField()

tf2.setEchoChar('●')

tf2.setBounds(80,100,160,20)

con.add(tf2)

ButtonGroup cg1=new ButtonGroup()

cb1=new JRadioButton("普通会员")

cb1.setBounds(15,135,80,20)

cb1.addItemListener(this) //普通会员

con.add(cb1)

cb2=new JRadioButton("图书管理员")

cb2.setBounds(90,135,102,20)

cb2.addItemListener(this)

con.add(cb2)

cb3=new JRadioButton("系统管理员")

cb3.setBounds(190,135,120,20)

cb3.addItemListener(this)

con.add(cb3)

cg1.add(cb1)

cg1.add(cb2)

cg1.add(cb3)

bu1=new JButton("登陆")

bu1.addActionListener(this)

bu1.setBounds(10,170,60,20)

con.add(bu1)

bu2=new JButton("取消")

bu2.setBounds(110,170,60,20)

con.add(bu2)

// bu1.addActionListener(new ActionListener(){

// public void actionPerformed(ActionEvent e){

////tf1.setText("")

////tf2.setText("")

////itemStateChanged()

// }

// })

bu2.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

tf1.setText("")

tf2.setText("")

//new Operator().showframe()

}

})

bu3=new JButton("注册")

bu3.setBounds(210,170,60,20)

// con.add(bu3)

bu3.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

// Face2()

}

})

lab3=new JLabel(new ImageIcon("2.jpg"))

lab3.setBounds(0,0,300,60)

con.add(lab3)

lab4=new JLabel(new ImageIcon("1.jpg"))

lab4.setBounds(0,0,300,230)

con.add(lab4)

fr1.setBounds(305,100,300,230)

fr1.setTitle("图书管理系统java版")

fr1.setResizable(false)

fr1.setVisible(true)

}

public void itemStateChanged(ItemEvent e){

//if(cb1.isSelected()){

//// p.showframe()

// try{

//// p.menu0.setEnabled(false)

////

//// p.menuItem6.setEnabled(false)

//// p.menuItem7.setEnabled(false)

//}catch(Exception d){}

//

//// p.menuItem3.setEnabled(false)

//// p.menuItem5.setEnabled(false)

//// p.menuItem6.setEnabled(false)

// }

// else if(cb2.isSelected()){

//// p.showframe()

//try{

//p.menu2.setEnabled(false)

// }catch(Exception d){}

//}

// else if(cb3.isSelected()){

//// p.showframe()

// }

}

public void actionPerformed(ActionEvent e){

//Operator p=new Operator()

if(e.getSource()==bu2)

{

this.setEnable("else")

}

else if(e.getSource()==bu1)

{

char[] password=tf2.getPassword() //获得密码

String passwordSTR=new String(password)

if(tf1.getText().equals("wp")&tf2.getText().equals("456")&!cb1.isSelected()){

JOptionPane.showMessageDialog(null,"请检查你的权限 !")

}

if(tf1.getText().equals("wp")&tf2.getText().equals("456")&cb1.isSelected()){

JOptionPane.showMessageDialog(null,"恭喜你提交成功")

fr1.setVisible(false)

// Operator s= new Operator("设计")//普通用户

//s.ti()

}

if(tf1.getText().equals("wp")&tf2.getText().equals("123456")&cb1.isSelected()){

JOptionPane.showMessageDialog(null,"恭喜你提交成功")

fr1.setVisible(false)

// Operator s=new Operator("设计")//普通用户

//s.ti()

}

if(tf1.getText().equals("王琼琨")&tf2.getText().equals("123456")&cb1.isSelected()){

JOptionPane.showMessageDialog(null,"恭喜你提交成功")

fr1.setVisible(false)

//Operator s= new Operator("设计")//普通用户

//s.ti()//普通用户

}

if(tf1.getText().equals("陈银山")&tf2.getText().equals("123456")&cb3.isSelected()){

fr1.setVisible(false)

JOptionPane.showMessageDialog(null,"恭喜你提交成功")//高级管理员

//new Operator("设计")//普通用户

}

if(tf1.getText().equals("刘凡")&tf2.getText().equals("6598")&cb2.isSelected()){

JOptionPane.showMessageDialog(null,"恭喜你提交成功")//图书管路元

fr1.setVisible(false)

//Operator s= new Operator("设计")//图书挂历员 用户

//s.ti1()

}

if(tf1.getText().trim().equals(""))

{

JOptionPane.showMessageDialog(null,"用户名不能为空!")

return

}

if(passwordSTR.equals(""))

{

JOptionPane.showMessageDialog(null,"密码不能为空!")

return

}

else{

userName=tf1.getText().trim()

}

String strSQL

strSQL="select * from users where 用户名='"+

tf1.getText().trim()+"'and 密码='"+

passwordSTR+"'"

//rs=db.getResult(strSQL)

boolean isExist=false

try

{

//isExist=rs.first()

}

catch(Exception sqle)

{

System.out.println(sqle.toString())

}

if(!isExist)

{

JOptionPane.showMessageDialog(null,"用户名不存在或者密码不正确!")

System.out.print("连接失败")

// this.setEnable("else")

}

else

{

//try

//{

////rs.first()

////this.setEnable(rs.getString("权限").trim())//设置权限

//// db.closeConnection()//DataBase

//}

//catch(SQLException sqle2)

//{

//System.out.println(sqle2.toString())

//}

}

}

}

/**

* 设置登录用户的权限

*

*/

public void setEnable(String powerType)

{

/**

* 根据不同用户设置相应权限

*/

//Operator p=new Operator()

if(powerType.trim().equals("系统管理员"))

{

System.out.println ("系统管理员")

//p.showframe()

}

else if(powerType.trim().equals("书籍管理员"))

{

System.out.println ("书籍管理员")

//p.showframe()

//p.menuItem6.setEnabled(false)

}

else if(powerType.trim().equals("else"))

{

System.out.println ("其他")

//p.showframe()

//p.menu0.setEnabled(false)

//p.menu2.setEnabled(false)

}

}

/**

* 登陆

*/

public static void main(String args []){

new MyFrame()

}

}

将使applet停止运行,浏览器调用destory方法终止,使JAVA中applet小程序有机会释放其存在期间锁定的资源。只要用户不关闭浏览器,重新加载页面,浏览器则只调用start方法和paint方法重新绘制并运行小程序。

import java.awt.*

import java.awt.event.*

import java.awt.geom.*

import javax.swing.*

//不规则图形的绘制

public class IrregularShapeDemo extends JFrame {

GeneralPath gPath= new GeneralPath()//GeneralPath对象实例

Point aPoint

//构造函数

public IrregularShapeDemo() {

super("不规则图形的绘制")//调用父类构造函数

enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK)//允许事件

setSize(300, 200)//设置窗口尺寸

setVisible(true)//设置窗口可视

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//关闭窗口时退出程序

}

public void paint(Graphics g) { //重载窗口组件的paint()方法

Graphics2D g2D = (Graphics2D)g//获取图形环境

g2D.draw(gPath)//绘制路径

}

public static void main(String[] args) {

new IrregularShapeDemo()

}

protected void processMouseEvent(MouseEvent e) { //鼠标事件处理

if(e.getID() == MouseEvent.MOUSE_PRESSED) {

aPoint = e.getPoint()//得到当前鼠标点

gPath = new GeneralPath()//重新实例化GeneralPath对象

gPath.moveTo(aPoint.x,aPoint.y)//设置路径点

}

}

protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理

if(e.getID() == MouseEvent.MOUSE_DRAGGED) {

aPoint = e.getPoint()//得到当前鼠标点

gPath.lineTo(aPoint.x, aPoint.y)//设置路径

gPath.moveTo(aPoint.x, aPoint.y)

repaint()//重绘组件

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7796428.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存