package inter.frameimport java.awt.BorderLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListenerimport javax.swing.JFrame
import javax.swing.JMenu
import javax.swing.JMenuBar
import javax.swing.JMenuItem
import javax.swing.JOptionPanepublic class MenuTest { /**
* @param args
*/
JFrame frame //定义一个窗口架构
JMenuBar mb//定义窗口的菜单工具栏
JMenu m//定义菜单
JMenuItem mi1//定义菜单的内容
JMenuItem mi2//定义菜单的内容
public MenuTest() {
initFrame()
initAction()
}
public void initFrame() {
frame = new JFrame()
mb = new JMenuBar()
m = new JMenu("学生查询")
mi1 = new JMenuItem("确认")
mi2 = new JMenuItem("取消")m.add(mi1)
m.add(mi2)
mb.add(m)
frame.add(mb, BorderLayout.NORTH)
frame.setSize(300, 300)//设置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//设置退出时关闭窗口
frame.setVisible(true)//设置窗口可见
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具体实现代码根据实际要求填写
System.out.println("click")
JOptionPane.showMessageDialog(null, "你点击了确定按钮")
}
})
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具体实现代码根据实际要求填写
JOptionPane.showMessageDialog(null, "你点击了取消按钮")
}
})
} public static void main(String[] args) {
new MenuTest()//执行菜单创建
}}
我试一下硬盘文件存储吧,首先在C盘根目录下创建个login.swing的文件,在里面写上 tom##123&&lydia##123,这个为了方便测试,自己试下吧,我也是没学多久,如果有太2的地方,请联系我...谢谢...import java.awt.*
import javax.swing.*
import java.io.*
public class LoginTest implements ActionListener{
private JFrame jf
private JLabel l1,l2
private JTextField tf1
private JPasswordField tf2
private JPanel northPanel,centerPanel
private JButton b1,b2
private File file = new File("c:/login.swing")
public LoginTest() {
jf = new JFrame("My First WindowTest")
northPanel = new JPanel(new GridLayout(2,2,10,10))
l1 = new JLabel("用户名:")
tf1 = new JTextField()
l2 = new JLabel("密 码:")
tf2 = new JPasswordField()
northPanel.add(l1)
northPanel.add(tf1)
northPanel.add(l2)
northPanel.add(tf2)
centerPanel = new JPanel()
b1 = new JButton("login")
b2 = new JButton("exit")
centerPanel.add(b1)
centerPanel.add(b2)
b1.addActionListener(this)
b2.addActionListener(this)
jf.add(northPanel)
jf.add(centerPanel,"South")
jf.setSize(200,130)
Dimension size = Toolkit.getDefaultToolkit().getScreenSize()
jf.setLocation(size.width / 2 - jf.getWidth() / 2, size.height / 2 - jf.getHeight() / 2)
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
jf.setVisible(true)
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b1)) {
String username = tf1.getText()
String password = String.valueOf(tf2.getPassword())
BufferedReader br = null
try {
FileReader fr = new FileReader(file)
br = new BufferedReader(fr)
String line = "",str = ""
while((line = br.readLine()) != null) {
str += line
}
String[] users = str.split("&&")
for(String user : users) {
String[] userInfo = user.split("##")
if(userInfo[0].equals(username) &&userInfo[1].equals(password)) {
JOptionPane.showMessageDialog(null, "登录成功!")
return
}
}
JOptionPane.showMessageDialog(null, "用户名或密码错误!")
return
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace()
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace()
}
} else {
System.exit(0)
}
}
public static void main(String args[]) {
new LoginTest()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)