JAVA小程序:在窗体中的文本框,输入数字,窗体根据数字所代表的颜色变换颜色,完成背景的修改设置

JAVA小程序:在窗体中的文本框,输入数字,窗体根据数字所代表的颜色变换颜色,完成背景的修改设置,第1张

import javax.jws.*

import javax.swing.*

import java.awt.*

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

public class Colorful extends JFrame{

public static final int WIDTH=320//你可以在这里设置窗口宽度

public static final int HEIGHT=241//你可以在这里设置窗口高度

Dimension scrnsize

Toolkit toolkit = Toolkit.getDefaultToolkit()

JTextField jText1

JPanel panel1

Font font1=new Font("宋体", Font.PLAIN, 12)

String color_set

int coler_num

Colorful(){

setSize(WIDTH,HEIGHT)

setResizable(false)

setDefaultCloseOperation(EXIT_ON_CLOSE)

scrnsize = toolkit.getScreenSize()

setLocation(scrnsize.width / 2 - getWidth() / 2,

scrnsize.height / 2 - getHeight() / 2)

Container content=getContentPane()

content.setLayout(new BorderLayout())

jText1=new JTextField(12)

jText1.setFont(font1)

panel1=new JPanel()

panel1.setLayout(new FlowLayout())

panel1.add(jText1)

panel1.setBackground(Color.white)

jText1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {

color_set=jText1.getText()

if(color_set.length()!=0){

panel1.setBackground(new Color(Integer.parseInt(color_set)))

}

else

return

}

})

content.add(panel1)

}

public static void main(String args[]){

Colorful colorful=new Colorful()

colorful.setVisible(true)

}

}

代码测试运行如图所示,我窗口初始设置的较小,方便截图,楼主可以自己设大一点

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

/**

*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()

}

}


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

原文地址: https://outofmemory.cn/yw/11436469.html

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

发表评论

登录后才能评论

评论列表(0条)

保存