[英]Adding Swing GUI to existing working java program
我已經在互聯網上搜索了有關將 Swing GUI 添加到現有 Java 程序的信息,但我仍然對如何實現它感到有些困惑。
我為表單布局創建了一個單獨的類,並且可以從主類調用/打開表單窗口。 我從互聯網上獲得了 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 的動作應該是什么?
我可以從我的主方法調用該類,並打開表單,但我不知道如何從表單本身獲取數據,並將它們輸入到我現有的代碼中(即 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");
任何幫助表示贊賞,
在此先感謝各位。
我建議通過實現MVC Pattern將 gui 與其控制分開。
有一個Model
類,其中包含視圖 (gui) 需要的所有信息。
有一個使用模型來顯示 gui 的View
類。
有一個Controller
模型和視圖的Controller
類。
以下mre演示了具有非常簡單功能的 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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.