下面为示例代码
package io.patamon.geocoding
import java.awt.EventQueue
import java.awt.Font
import java.awt.Toolkit
import java.awt.event.ItemEvent
import java.io.File
import java.io.FileWriter
import javax.swing.*
import javax.swing.filechooser.FileNameExtensionFilter
class Main(title: String) : JFrame() {
init {
this.iconImage = ImageIcon("地址.png").image
setTitle(title)
isResizable= false
layout =null
val label =JLabel("输入类型:")
label.setBounds(5, 10, 80, 30)
label.font = Font("Dialog", 1, 16)
val label2 =JLabel("文件路径:")
label2.setBounds(5, 60, 80, 30)
label2.font = Font("Dialog", 1, 16)
val label3 =JLabel("保存路径:")
label3.setBounds(5, 110, 80, 30)
label3.font = Font("Dialog", 1, 16)
val pathText = JTextField()
pathText.setBounds(80,65,350,25)
pathText.font = Font("Dialog", 1, 16)
val resultText = JTextField()
resultText.setBounds(80,115,350,25)
resultText.font = Font("Dialog", 1, 16)
val excelCheck = JCheckBox("excel文件", false)
excelCheck.setBounds(80,10,100,30)
excelCheck.font = Font("Dialog", 1, 16)
val txtCheck = JCheckBox("txt文件", false)
txtCheck.setBounds(220,10,100,30)
txtCheck.font = Font("Dialog", 1, 16)
val inputCheck = JCheckBox("输入文本", false)
inputCheck.setBounds(350,10,100,30)
inputCheck.font = Font("Dialog", 1, 16)
val inputFile = JButton("选择")
inputFile.setBounds(433,62,80,30)
inputFile.font = Font("Dialog", 1, 16)
val outFile = JButton("选择")
outFile.setBounds(433,112,80,30)
outFile.font = Font("Dialog", 1, 16)
val expressBtn = JButton("快递地址处理")
expressBtn.setBounds(100,150,140,30)
expressBtn.font = Font("Dialog", 1, 16)
val mapBtn = JButton("在地图中查看")
mapBtn.setBounds(280,150,140,30)
mapBtn.font = Font("Dialog", 1, 16)
val startBtn = JButton("开始")
startBtn.setBounds(220,190,80,30)
startBtn.font = Font("Dialog", 1, 16)
inputFile.addActionListener {
if(excelCheck.isSelected||txtCheck.isSelected||inputCheck.isSelected){
val chooser = JFileChooser()
chooser.fileSelectionMode = JFileChooser.FILES_ONLY
val filter:FileNameExtensionFilter
if(excelCheck.isSelected) {
filter = FileNameExtensionFilter("excel文件", "xlsx")
chooser.fileFilter = filter
}
else if(txtCheck.isSelected) {
filter = FileNameExtensionFilter("txt文件", "txt")
chooser.fileFilter = filter
}
val returnVal = chooser.showOpenDialog(chooser)
if (returnVal == JFileChooser.APPROVE_OPTION) {
inputPath = chooser.selectedFile.path
pathText.text = inputPath
}
}
else{
JOptionPane.showMessageDialog(null, "请选择输入类型", "操作提示", JOptionPane.INFORMATION_MESSAGE)
}
}
outFile.addActionListener {
val chooser = JFileChooser()
chooser.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
val returnVal = chooser.showOpenDialog(chooser)
if (returnVal == JFileChooser.APPROVE_OPTION) {
outPath = chooser.selectedFile.path
resultText.text = outPath
}
}
expressBtn.addActionListener {
var url = "file:///D:/Standardizer/expressAddressPro/index.html"
try {
url.replace("\"", "\'")
url = "\"" + url.toString() + "\""
val command = "cmd /c start edge "
Runtime.getRuntime().exec(command + url)
} catch (e: Exception) {
JOptionPane.showMessageDialog(null, e.toString(), "操作提示", JOptionPane.INFORMATION_MESSAGE)
}
}
mapBtn.addActionListener {
var url = "file:///D:/Standardizer/map/map.html"
if (inputCheck.isSelected&&resultText.text!=""){
url = url+"?name="+resultText.text.replace(" ","")
}
try {
url.replace("\"", "\'")
url = "\"" + url.toString() + "\""
val command = "cmd /c start edge "
Runtime.getRuntime().exec(command + url)
} catch (e: Exception) {
JOptionPane.showMessageDialog(null, e.toString(), "操作提示", JOptionPane.INFORMATION_MESSAGE)
}
}
startBtn.addActionListener {
}
excelCheck.addItemListener { e ->
if (e.stateChange == ItemEvent.SELECTED) {
label2.text = "文件路径:"
label3.text = "保存路径:"
inputFile.isEnabled = true
outFile.isEnabled = true
txtCheck.isSelected = false
inputCheck.isSelected = false
}
}
txtCheck.addItemListener { e ->
if (e.stateChange == ItemEvent.SELECTED) {
label2.text = "文件路径:"
label3.text = "保存路径:"
inputFile.isEnabled = true
outFile.isEnabled = true
excelCheck.isSelected = false
inputCheck.isSelected = false
}
}
inputCheck.addItemListener { e ->
if (e.stateChange == ItemEvent.SELECTED) {
label2.text = "输入内容:"
label3.text = "输出内容:"
inputFile.isEnabled = false
outFile.isEnabled = false
excelCheck.isSelected = false
txtCheck.isSelected = false
}
}
label.horizontalAlignment = SwingConstants.CENTER
label2.horizontalAlignment = SwingConstants.CENTER
label3.horizontalAlignment = SwingConstants.CENTER
contentPane.add(label)
contentPane.add(label2)
contentPane.add(label3)
contentPane.add(pathText)
contentPane.add(resultText)
contentPane.add(excelCheck)
contentPane.add(txtCheck)
contentPane.add(inputCheck)
contentPane.add(inputFile)
contentPane.add(outFile)
contentPane.add(mapBtn)
contentPane.add(startBtn)
contentPane.add(expressBtn)
defaultCloseOperation = JFrame.EXIT_ON_CLOSE
setSize(530, 280)
setLocationRelativeTo(null)
}
}
private fun createAndShowGUI() {
val frame = Main("地址标准化处理")
frame.isVisible = true
}
fun main(args: Array<String>) {
EventQueue.invokeLater(::createAndShowGUI)
}
|