-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathIO.java
More file actions
200 lines (153 loc) · 4.97 KB
/
IO.java
File metadata and controls
200 lines (153 loc) · 4.97 KB
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
package homework;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class IO
{
private JFrame jFrame;
private JPanel jPanel;
private JLabel jLabel;
private JButton chooseFile;
private JFileChooser jFileChooser;
private File file;
private Toolkit toolkit;
private StringBuilder codeOfText;
private String codeOfUser;
private JTextField jTextField;
private JButton okButton;
public IO()
{
toolkit = Toolkit.getDefaultToolkit();
init();
}
public void init()
{
jFrame = new JFrame("请输入密码:");
jFrame.setLayout(new BorderLayout());
jFrame.setBounds((toolkit.getScreenSize().width - 400) / 2, (toolkit.getScreenSize().height - 400) / 2, 300, 200);
jFrame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
jFrame.setResizable(false);
jLabel = new JLabel("请输入密码");
jLabel.setFont(new Font("微软雅黑", 1, 20));
chooseFile = new JButton("打开密码文件 ");
chooseFile.setSize(50, 25);
chooseFile.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
chooseFile();
}
});
jTextField = new JTextField(10);
jTextField.setFont(new Font("微软雅黑", 1, 20));
okButton = new JButton("确定");
okButton.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
try
{
codeOfUser = jTextField.getText();
if (codeOfText.toString().equals(codeOfUser))
{
mDialog(1);
}
else
mDialog(0);
} catch (NullPointerException e1)
{
mDialog(-1);
}
}
});
jPanel = new JPanel();
jPanel.add(jLabel);
jPanel.add(chooseFile);
jPanel.add(jTextField);
jPanel.add(okButton);
jFrame.add(jPanel);
jFrame.setVisible(true);
}
public void chooseFile()
{
jFileChooser = new JFileChooser(".");
jFileChooser.showOpenDialog(jFrame);
jFileChooser.setVisible(true);
jFileChooser.setBounds((toolkit.getScreenSize().width - 400) / 2, (toolkit.getScreenSize().height - 400) / 2, 300, 200);
jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
file = jFileChooser.getSelectedFile();
BufferedInputStream bufferedInputStream = null;
try
{
bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
Byte str[] = new Byte[bufferedInputStream.available()];
int b;
codeOfText = new StringBuilder();
while ((b = bufferedInputStream.read()) != -1)
{
codeOfText.append((char) b);
}
} catch (IOException e)
{
System.out.println("打开文件失败!");
} finally
{
try
{
if (bufferedInputStream != null)
{
bufferedInputStream.close();
}
} catch (IOException e)
{
System.out.println("关闭流失败!");
}
}
}
public void mDialog(int isRight)//Dialog提示窗口
{
JDialog jDialog = new JDialog(jFrame, "输入有误!", true);
jDialog.setBounds((toolkit.getScreenSize().width - 200) / 2, (toolkit.getScreenSize().height - 200) / 2, 300, 100);
//dialog提示信息
JPanel jPanel = new JPanel();
jPanel.setLayout(new BorderLayout());//创建jPanel容器,存储两个部件,并设置为BorderLayout布局
JLabel jLabel = new JLabel();
if (isRight == 1)
jLabel.setText("密码正确!");
else if (isRight == 0)
jLabel.setText("密码错误!");
else
jLabel.setText("请先打开文件!");
jLabel.setFont(new Font("微软雅黑", 1, 20));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
JButton jButton = new JButton("确定");
jPanel.add(jLabel, BorderLayout.CENTER);
jPanel.add(jButton, BorderLayout.SOUTH);
jDialog.add(jPanel);
jDialog.validate();//同步数据,和上面的原理一样
jButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
jDialog.setVisible(false);//点击确定设置为不可见
}
});
jDialog.setResizable(false);//不可调整大小
jDialog.setVisible(true);
}
public static void main(String[] args)
{
new IO();
}
}