【java】MyEclipse的简易信息查询窗体
2016-05-29 23:33
309 查看
这是我学习java的第一个简易窗体,类似于数据库,为了简化,我使用的是txt文本,从txt中读入信息并显示在界面中的textfield中。
import java.awt.BorderLayout; public class First_Window extends JFrame { private JPanel contentPane; private JTextField textName; private JTextField textNum; private JTextField textPrice; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { First_Window frame = new First_Window(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public First_Window() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 451, 328); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblName = new JLabel("\u54C1\u540D\uFF1A"); lblName.setBounds(23, 26, 54, 15); contentPane.add(lblName); JLabel lblNum = new JLabel("\u4EF6\u6570\uFF1A"); lblNum.setBounds(23, 64, 54, 15); contentPane.add(lblNum); JLabel lblPrice = new JLabel("\u4EF7\u683C\uFF1A"); lblPrice.setBounds(23, 110, 54, 15); contentPane.add(lblPrice); JLabel lblPicture = new JLabel("\u56FE\u7247\uFF1A"); lblPicture.setBounds(23, 147, 54, 15); contentPane.add(lblPicture); textName = new JTextField(); textName.setBounds(102, 23, 102, 21); contentPane.add(textName); textName.setColumns(10); textNum = new JTextField(); textNum.setBounds(102, 61, 102, 21); contentPane.add(textNum); textNum.setColumns(10); textPrice = new JTextField(); textPrice.setBounds(102, 107, 102, 21); contentPane.add(textPrice); textPrice.setColumns(10); final JLabel lblNewPicture = new JLabel(""); lblNewPicture.setBounds(102, 147, 161, 120); contentPane.add(lblNewPicture); JButton btnOK = new JButton("\u786E\u5B9A"); btnOK.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { BufferedReader bf = null; try { bf = new BufferedReader(new FileReader("数据.txt")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String data[][] = new String[3][3]; int i = 0; String s = ""; try { while((s=bf.readLine()) != null){ data[i] = s.split(" "); i++; } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Icon p[]=new Icon[3]; //图片数组 p[0]=new ImageIcon("1.jpg"); p[1]=new ImageIcon("2.jpg"); p[2]=new ImageIcon("3.jpg"); String input = ""; input = JOptionPane.showInputDialog("请输入品名!"); for(int j = 0;j < data.length;j++){ if(input.equals(data[j][0])){ textName.setText(data[j][0]); textNum.setText(data[j][1]); textPrice.setText(data[j][2]); lblNewPicture.setIcon(p[j]); } } } }); btnOK.setBounds(311, 200, 93, 23); contentPane.add(btnOK); } }
txt中的内容如下:
毛巾 15条 5.0元 牙刷 15把 2.5元 牙膏 15支 10.8元
图片使用的是图片数组,放在该项目的文件夹中。(相对路径)
相关文章推荐
- Spring+MyBatis多数据源配置实现(mysql和sqlserver数据库)
- Java LockSupport
- 配置 OpenStack Eclipse开发环境 - 远程调试
- Spring MVC Beginner's Guide--应该看第二次
- JAVA_OA管理系统(二)番外篇:IoC原理
- java 集合练习题2
- Spring in action 03 -- 装配 Bean(xml配置)
- #java读书笔记#IO流1
- 2015-2016-2《Java程序设计》团队博客3
- Spring管理
- java自定义异常类型
- jdk安装及环境变量配置
- Maven+SpringMVC+Mybatis整合入门Demo
- Java基础回顾 : 字符流操作
- java注解之基础篇
- Java中那些没有定义方法的接口
- java笔记一
- Java中static的运用
- 利用javax.mail发送邮件
- Java异常处理