快速使用的java界面
package com.yinyezhizhao.ocrdemo;
import lombok.extern.slf4j.Slf4j;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Hello world!
*
* @author jgdu
*/
@Slf4j
public class App
{
public static void main( String[] args )
{
//
guiFrom();
}
public static void guiFrom(){
final JFrame f = new JFrame("测试DEMO");
f.setSize(410, 520);
f.setLocation(700, 200);
f.setLayout(null);
JPanel pInput = new JPanel();
pInput.setBounds(10, 10, 375, 120);
pInput.setLayout(new GridLayout(4,3,10,10));
JButton button = new JButton("开始");
button.setBounds(10, 120 + 10, 100, 20);
JLabel account = new JLabel("账号:");
final JTextField accountText = new JTextField();
JLabel key = new JLabel("Key:");
final JTextField keyText = new JTextField();
JLabel path = new JLabel("地址:");
final JTextField pathText = new JTextField();
pInput.add(account);
pInput.add(accountText);
pInput.add(key);
pInput.add(keyText);
pInput.add(path);
pInput.add(pathText);
pInput.add(button);
f.add(pInput);
//文本域
JLabel result = new JLabel("识别结果:");
result.setBounds(10, 110 + 20, 80, 80);
final JTextArea ta = new JTextArea();
ta.setLineWrap(true);
JScrollPane scroll = new JScrollPane(ta);
//把定义的JTextArea放到JScrollPane里面去
//分别设置水平和垂直滚动条自动出现
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setBounds(10, 160 + 20, 370, 290);
scroll.setVisible(true);
f.add(button);
f.add(result);
f.add(scroll);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (isEmpty(accountText,"账号")){
return;
}
if (isEmpty(keyText,"Key")){
return;
}
if (isEmpty(pathText,"地址")){
return;
}
String account = accountText.getText();
String key = keyText.getText();
String path = pathText.getText();
String result = "";
ta.setText(result);
}
//检验是否为空
private boolean isEmpty(JTextField tf, String msg){
String value = tf.getText();
if(value.length()==0){
JOptionPane.showMessageDialog(f, msg + " 不能为空");
tf.grabFocus();
return true;
}
return false;
}
});
}
}
完整界面:
maven 引入依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.28</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
编译成jar包后(具体操作请百度) 使用bat 运行文件,bat 配置方式:
@ echo off
rem set p="%~dp0\bin\javaw.exe"
set p="D:\Program Files\Java\jdk1.8.0_221\bin\javaw.exe"
start "" %p% -jar demo.jar