java图形化界面实现登录窗口

2025-05-29 0 19

登录窗口一般很常见,现在让我们自己也来写一个吧!

ps:很多import是重复的,是因为我是分了几个类写的,必须单独导入

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265
//模拟qq登录窗口

import java.awt.*;

import java.io.*;

import java.awt.event.*;

import javax.swing.*;

public class qqgui extends jframe implements actionlistener{

private jlabel userla;

private jlabel pwdla;

private jlabel vercodela;//验证码

private jtextfield usertxt;

private jpasswordfield pwdtxt;

private jtextfield vercodetxt;//验证码

private jbutton surebt;

private jbutton quitbt;

private mypanel mp;

//构造方法

public qqgui()

{

init();

}

public void init()

{

frame frame = new frame("qq登录");

//创建出控件对象(因为上面只是声明出来,并没有给出实际的空间)

//用户文本

userla = new jlabel();

userla.settext("用户名:");

userla.setsize(60, 50);

userla.setlocation(100, 80);

//密码文本

pwdla = new jlabel();

pwdla.settext("密码:");

pwdla.setsize(50, 50);

pwdla.setlocation(100, 120);

//用户输入框

usertxt = new jtextfield();

usertxt.setsize(100, 20);

//this.setsize(width, height)

usertxt.setlocation(170, 95);

//密码输入框

pwdtxt = new jpasswordfield();

pwdtxt.setsize(100, 20);

pwdtxt.setlocation(170, 135);

//确认按钮

surebt = new jbutton("登录");

surebt.setsize(60, 25);

surebt.setlocation(135, 260);

//退出按钮

quitbt = new jbutton("退出");

quitbt.setsize(60, 25);

quitbt.setlocation(240, 260);

//验证码文本

vercodela = new jlabel();

vercodela.settext("验证码:");

vercodela.setsize(60, 50);

vercodela.setlocation(100,165);

//验证码文本框

vercodetxt = new jtextfield();

vercodetxt.setsize(100, 20);

vercodetxt.setlocation(170, 180);

//验证码

mp = new mypanel();

mp.setsize(100, 30);

mp.setlocation(280, 175);

//登录方式选择框

jcombobox xlk=new jcombobox();

xlk.setsize(60, 20);

xlk.setlocation(250, 220);

xlk.additem("在线");

xlk.additem("隐身");

xlk.additem("离开");

this.setlayout(null);

this.setsize(500, 400);

this.add(userla);

this.add(pwdla);

this.add(usertxt);

this.add(pwdtxt);

this.add(surebt);

this.add(quitbt);

this.add(vercodela);

this.add(vercodetxt);

this.add(mp);

this.add(xlk);

surebt.addactionlistener(this);

quitbt.addactionlistener(this);

this.setvisible(true);

}

//具体事件的处理

public void actionperformed(actionevent e)

{

//获取产生事件的事件源强制转换

jbutton bt = (jbutton)e.getsource();

//获取按钮上显示的文本

string str = bt.gettext();

if(str.equals("登录"))

{

if(!checkisnull())

{

//获取用户所输入的用户名

string user = usertxt.gettext().trim();

//获取用户所输入的密码

string pwd = pwdtxt.gettext().trim();

if(checkuserandpwd(user,pwd))

{

//隐藏当前登录窗口

this.setvisible(false);

//验证成功创建一个主窗口

mainframe frame = new mainframe();

}

else

{

//如果错误则弹出一个显示框

joptionpane pane = new joptionpane("用户或密码错误");

jdialog dialog = pane.createdialog(this,"警告");

dialog.show();

}

}

}

else

{

//调用系统类中的一个正常退出

system.exit(0);

}

}

private boolean checkisnull()

{

boolean flag = false;

if(usertxt.gettext().trim().equals(" "))

{

flag = true;

}

else

{

if(pwdtxt.gettext().trim().equals(" "))

{

flag = true;

}

}

return flag;

}

private boolean checkuserandpwd(string user,string pwd)

{

boolean result = false;

try

{

filereader file = new filereader("d:\\\\workspaces\\\\myeclipse 8.5\\\\testgui.txt");

bufferedreader bre = new bufferedreader(file);

string str = bre.readline();

while(str!=null)

{

string[] strs = str.split(",");

if(strs[0].equals(user))

{

if(strs[1].equals(pwd))

result = true;

}

str = bre.readline();

}

file.close();

}catch(exception ex)

{

system.out.print("");

}

return result;

}

}

//mainframe类

import javax.swing.*;

public class mainframe extends jframe {

public mainframe()

{

this.setsize(300, 300);

this.setvisible(true);

}

}

//验证码的生成

import java.awt.*;

import java.util.*;

public class mypanel extends panel {

public void paint(graphics g)

{

int height = 50;

int width = 90;

//验证码框背景颜色

g.setcolor(color.light_gray);

//填充验证码背景

g.fillrect(0, 0, width, height);

g.setcolor(color.black);

g.drawrect(0, 0, width-1, height-1);

random r = new random();

//设置干扰点

for(int i = 0;i<100;i++)

{

int x = r.nextint(width)-1;

int y = r.nextint(height)-1;

g.drawoval(x, y, 2, 2);

}

g.setfont(new font("黑体",font.bold,20));//设置验证码字体以及大小

g.setcolor(color.red);//设置验证码字体颜色

//生成随机验证码

char[] tmp = ("0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz").tochararray();

stringbuilder sb = new stringbuilder();

for(int i = 0;i<4;i++)

{

int pos = r.nextint(tmp.length);

char c = tmp[pos];

sb.append(c + " ");

}

g.drawstring(sb.tostring(), 10, 15);//写入验证码

}

}

//下拉框的实现

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class xialakuang extends jframe {

private jcombobox combobox;//定义一个组合框

public void xia ()

{

//jpanel panel = new jpanel();//创建一个jpanel面板

combobox = new jcombobox();

combobox.additem("在线");

combobox.additem("隐身");

combobox.additem("离开");

this.add(combobox);

//this.add(panel);

this.setsize(200, 100);

this.setdefaultcloseoperation(jframe.exit_on_close);

this.setvisible(true);

}

}

//测试

public class testqqgui {

/**

* @param args

*/

public static void main(string[] args) {

// todo auto-generated method stub

qqgui frame = new qqgui();

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/qq_36474990/article/details/78617649

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java图形化界面实现登录窗口 https://www.kuaiidc.com/111948.html

相关文章

发表评论
暂无评论