您的位置:首页 > 编程语言 > Java开发

【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元


图片使用的是图片数组,放在该项目的文件夹中。(相对路径)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: