【发布时间】:2020-04-03 10:04:08
【问题描述】:
我已经在互联网上搜索了有关将 swing GUI 添加到现有 java 程序的信息,但我仍然对如何实现它感到有些困惑。
我为表单布局创建了一个单独的类,并且可以从主类调用/打开表单窗口。我从互联网上获得了 GUI 的基本代码...
GUI类:
package gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainWindow extends JFrame implements ActionListener {
private Container c;
private JLabel title;
private JLabel masterFilePath;
private JTextField masterFilePathInput;
private JLabel dataFilePath;
private JTextField dataFilePathInput;
private JButton reset;
String def = "";
public void mainWindow() {
setTitle("Count Survey Comparison");
setBounds(300, 90, 900, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
c = getContentPane();
c.setLayout(null);
title = new JLabel("Count Survey Comparison");
title.setFont(new Font("Arial", Font.PLAIN, 30));
title.setSize(500, 30);
title.setLocation(300, 30);
c.add(title);
masterFilePath = new JLabel("Master Data File Location");
masterFilePath.setFont(new Font("Arial", Font.PLAIN, 20));
masterFilePath.setSize(500, 20);
masterFilePath.setLocation(80, 100);
c.add(masterFilePath);
masterFilePathInput = new JTextField();
masterFilePathInput.setFont(new Font("Arial", Font.PLAIN, 15));
masterFilePathInput.setSize(200, 20);
masterFilePathInput.setLocation(400, 100);
c.add(masterFilePathInput);
dataFilePath = new JLabel("Count Survey Data File Location");
dataFilePath.setFont(new Font("Arial", Font.PLAIN, 20));
dataFilePath.setSize(500, 20);
dataFilePath.setLocation(80, 150);
c.add(dataFilePath);
dataFilePathInput = new JTextField();
dataFilePathInput.setFont(new Font("Arial", Font.PLAIN, 15));
dataFilePathInput.setSize(200, 20);
dataFilePathInput.setLocation(400, 150);
c.add(dataFilePathInput);
reset = new JButton("Reset");
reset.setFont(new Font("Arial", Font.PLAIN, 15));
reset.setSize(100, 20);
reset.setLocation(270, 450);
reset.addActionListener(this);
c.add(reset);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
}
进入 actionPerformed 的动作应该是什么?
我可以从我的 main 方法调用该类,然后打开表单,但我不知道如何从表单本身获取数据,并将它们输入到我现有的代码中(即 readExcel(...)方法)
主要方法:
MainWindow window = new MainWindow();
window.mainWindow();
ReadExcel readExcel = new ReadExcel();
CompareAndCleanData cleanData = new CompareAndCleanData();
WriteExcel writeExcel = new WriteExcel();
/* Change file paths here */
/* Based on BSTVN Data */
System.out.println("Read Input Excel Data: In Progress...");
List<VehicleData> vehicleDataList = readExcel.readExcel(
"C:\\Users\\ray.tong\\Desktop\\Count Survey Digitalisation Things\\BSTVN\\Count survey data (PSR19, TBR18).xlsx");
System.out.println("Read Input Excel Data: Complete");
/* Read MasterData */
System.out.println("Read Brand Master Data: In Progress...");
List<BrandMasterData> brandMasterDataList = readExcel
.readBrandMaster("C:\\Users\\ray.tong\\Desktop\\Count Survey Digitalisation Things\\TireMasterData.xlsx");
System.out.println("Read Brand Master Data: Complete");
System.out.println("Read Size Master Data: In Progress...");
List<SizeMasterData> sizeMasterDataList = readExcel
.readSizeMaster("C:\\Users\\ray.tong\\Desktop\\Count Survey Digitalisation Things\\TireMasterData.xlsx");
System.out.println("Read Size Master Data: Complete");
System.out.println("Read Pattern Master Data: In Progress...");
List<PatternMasterData> patternMasterDataList = readExcel
.readPatternMaster("C:\\Users\\ray.tong\\Desktop\\Count Survey Digitalisation Things\\TireMasterData.xlsx");
System.out.println("Read Pattern Master Data: Complete");
System.out.println("Read Vehicle Make/Model Master Data: In Progress...");
List<VehicleMasterData> vehicleMasterDataList = readExcel
.readVehicleMaster("C:\\Users\\ray.tong\\Desktop\\Count Survey Digitalisation Things\\TireMasterData.xlsx");
System.out.println("Read Vehicle Master Data: Complete");
感谢任何帮助,
提前谢谢各位。
【问题讨论】:
-
"进入 actionPerformed 的动作应该是什么?" - 不要只使用你在互联网上找到的代码,而是要通过它来理解它的作用或意图。否则你会遇到很多麻烦。话虽如此,请查看
reset.addActionListener(this);。这就是使用作为 ActionListener 的框架的地方(我不会那样做,但那是另一回事)。所以任何需要进入actionPerformed(...)的东西都是你需要重置按钮来做的事情。 -
JTextFile有方法getText()应该用于获取用户的输入(在你的情况下是文件名)。但最好不要重新发明轮子,而是使用JFileChooser。 -
@thomas,感谢您的建议。那么,如果我添加另一个按钮来提交数据,我必须创建另一种方法来处理它?即 actionSubmit()?
-
@SergiyMedvynskyy,好的,谢谢,看看 JFileChooser
-
不,您应该为提交按钮设置不同的 ActionListener 或 Action - 这就是我不将框架本身设为 ActionListener 的原因之一。