将SwingGUI添加到现有的工作java程序中
我已经在互联网站上搜索了关于将SwingGUI添加到现有java程序的信息,但对于如何实现这一点,我仍然有点困惑 我已经为表单布局创建了一个单独的类,并且可以从主类调用/打开表单窗口。我从网上得到了GUI的基本代码 GUI类:将SwingGUI添加到现有的工作java程序中,java,swing,Java,Swing,我已经在互联网站上搜索了关于将SwingGUI添加到现有java程序的信息,但对于如何实现这一点,我仍然有点困惑 我已经为表单布局创建了一个单独的类,并且可以从主类调用/打开表单窗口。我从网上得到了GUI的基本代码 GUI类: package gui; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainWindow extends JFrame implements Ac
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) {
}
}
应该执行哪些行动 我可以从我的主方法调用该类,并打开表单,但我一直无法从表单本身获取数据,并将它们输入到我现有的代码(即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");
感谢您的帮助
提前感谢各位。我建议通过实现。 拥有一个模型类,该类保存视图gui所需的所有信息。 拥有一个使用模型显示gui的视图类。 具有控制模型和视图的控制器类 下面演示了MVC模式的使用,其功能非常简单:它允许用户选择一个文件。选择文件后,将显示文件名。它可以扩展以包括您需要的功能。 为了方便和简单,可以将以下代码复制粘贴到一个名为MVCDemo.java的文件中并运行:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MVCDemo {
public static void main(String[] args) {
Model model = new Model();
View view = new View();
new Controller(view, model);
}
}
/*Model contains the information for the view and information from the view
* as well as the logic.
* The model is independent of the user interface.
*/
class Model {
private String fileName;
String getFileName() {
return fileName;
}
void setFileName(String fileName) {
this.fileName = fileName;
}
}
/*View only contains the user interface part*/
class View {
private Model model;
private JFrame frame;
private JButton selectFileButton;
private JTextField textField;
void createAndShowGui(Model model){
this.model = model;
frame = new JFrame("MVC Model Demo");
textField = new JTextField(25);
//do not use null layout
frame.add(textField, BorderLayout.NORTH); //JFrame uses BorderLayout by default
selectFileButton = new JButton("Select File");
frame.add(selectFileButton, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
//may return null
File getFile(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// pop up file chooser dialog
fileChooser.showOpenDialog(frame);
return fileChooser.getSelectedFile();
}
void update() {
textField.setText(model.getFileName());
}
JButton getSelectFileButton() {
return selectFileButton;
}
}
/* The controller controls the view and model.
* Based on the user action, the Controller calls methods in the View and Model
* to accomplish the requested action.
*/
class Controller implements ActionListener{
private final Model model;
private final View view;
Controller(View view,Model model) {
this.model=model;
this.view=view;
view.createAndShowGui(model);
view.getSelectFileButton().addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
File file = view.getFile();
model.setFileName(file == null ? "No file selected" : file.getName());
view.update(); //alternatively use have View listen to Model changes
}
}
应该执行哪些行动不要只使用你在互联网上找到的代码,而是通过它来理解它的作用或意图。否则你会有很多麻烦。话虽如此,请查看reset.addActionListenerthis;。这就是使用框架作为ActionListener的地方,我不会这样做,但这是另一个故事。所以无论需要采取什么行动。。。是您需要重置按钮执行的任何操作。JTextFile具有getText方法,在您的情况下,该方法应用于获取用户输入的文件名。但是,如果你不重新发明轮子,使用它会更好。@thomas,谢谢你的建议。所以,如果我添加另一个按钮来提交数据,我必须创建另一个方法来处理它?i、 e.actionSubmit?@Sergiymedvynsky,好的,谢谢,将查看JFileChooserNo,您已经为submit按钮设置了不同的ActionListener或Action-这就是我不将框架本身设置为ActionListener的原因之一。