这是在做项目的时候写的第一个GUI,也是第一次用netbeans写的GUI,发现netbeans在写这个东西有很好的一个方面,就是可以直接将自己设计的GUI通过导航器实现,这一点大大方便了我们对GUI 代码的编写,更多的时间去对功能性的东西进行加工。
这只是一个很简便的窗口,大多东西都是netbeans提供的,只有open这个函数,还有跟python的连接是自己敲的。
GUI下载地址
编译器:netbeans
其他的不多说了,直接上代码看看:
1、open函数:
这个实现的原理:直接给一个文件选择对话框,选择需要的图片,读出图片路径,然后,在面板中加一个控件“标签”,利用这个标签将图片读取。
public void Open() throws IOException {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
jPanel3.removeAll();
jPanel3.add(Phtot);
jPanel3.setVisible(false);
JLabel I = new JLabel();
String name = chooser.getSelectedFile().getPath();
ImageIcon icon = new ImageIcon(name);
icon = new ImageIcon(icon.getImage().getScaledInstance(jPanel3.getWidth(), jPanel3.getHeight(), Image.SCALE_DEFAULT));
I.setIcon(icon);
I.setBounds(0,0, jPanel3.getWidth(), jPanel3.getHeight());
jPanel3.add(I);
jPanel3.setVisible(true);
python(chooser.getSelectedFile().getPath());
}
}
2、与python的连接:
主要是利用了线程方面的知识,创建一个线程去运行python的代码,这是一种比较方便的方法。当然,在运行的时候,需要注意python程序的args。这个也要求当前环境下有python:
public void python(String str) throws IOException{
String pythonPath = "program's path";
String[] cmd = new String[4];
cmd[0] = "python";
cmd[1] = pythonPath;
cmd[2] = "-v";
cmd[3] = str;
Runtime rt;
rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
BufferedReader bfr = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
String str1 = "";
while((line = bfr.readLine())!=null){
str1 = line;
}
Result(str1);
}
以上就是对这个程序的一些解释了,做的不好的,希望大家指教一下,能做的更好。
路途艰苦,天道酬勤。
注:本博客为原创,若需转载请注明出处。
注:若有侵权,请与本人联系删博客。