gpt4 book ai didi

java - 如何在 Java Swing Gui 中存储用户输入的变量以供进一步使用?

转载 作者:太空宇宙 更新时间:2023-11-04 10:55:59 24 4
gpt4 key购买 nike

我目前有一个简单的 Java AWT/Swing 代码,它创建一个简单的 GUI,它接受多个字符串用户输入并将其存储并显示在 Intellij 终端中,如下所示:

import javax.swing.*;
import java.awt.*; // Using AWT container and component classes
import java.awt.event.*; // Using AWT event classes and listener interfaces
import java.util.ArrayList;
import java.util.Scanner;

// An AWT program inherits from the top-level container java.awt.Frame
public class DateTime extends JFrame implements ActionListener {
private Label lblCount, lblsource, lbldate1, lbldate2; // Declare a Label component
private JTextField tfCount, date1, date2; // Declare a TextField component
private Button btnCount; // Declare a Button component
private int count = 0; // Counter's value
static String type = null;
private JCheckBox source1, source2;
boolean a = false;
boolean b= false;
static String source, datedefined1, datedefined2;
ArrayList<String> texts = new ArrayList<String>();
// Constructor to setup GUI components and event handlers
public DateTime () {
setLayout(new FlowLayout());
// "super" Frame, which is a Container, sets its layout to FlowLayout to arrange
// the components from left-to-right, and flow to next row from top-to-bottom.

lblCount = new Label("Enter the type of report you want generated; Hourly/ Daily/ Weekly/ EventComparison:"); // construct the Label component
add(lblCount); // "super" Frame container adds Label component

tfCount = new JTextField("", 20); // construct the TextField component
tfCount.setEditable(true); // set to read-only
// "super" Frame container adds TextField component

tfCount.setBounds(10,50,200,40);
add(tfCount);
tfCount.addActionListener(this);

lblsource = new Label("Now choose the source type:");
add(lblsource);
source1 = new JCheckBox("Drivetest", a);
source1.setBounds(10,100,50,30);
add(source1);
source2 = new JCheckBox("Ookla Dump",b);
add(source2);
source1.addActionListener(this);
source2.addActionListener(this);


lbldate1 = new Label("Please enter the Start DATETIME of the chosen duration(YYYY-MM-DD HH:MM:SS) :");
add(lbldate1);
date1 = new JTextField("", 30); // construct the TextField component
date1.setEditable(true);
add(date1);
date1.addActionListener(this);
lbldate2 = new Label("Please enter the end DATETIME of the chosen duration(YYYY-MM-DD HH:MM:SS): ");
add(lbldate2);
date2 = new JTextField("",30);
date2.setEditable(true);
add(date2);
date2.addActionListener(this);

// set to read-only
// "super" Frame container adds TextField component



// "btnCount" is the source object that fires an ActionEvent when clicked.
// The source add "this" instance as an ActionEvent listener, which provides
// an ActionEvent handler called actionPerformed().
// Clicking "btnCount" invokes actionPerformed().

setTitle("Report Generator"); // "super" Frame sets its title
setSize(800, 700); // "super" Frame sets its initial window size

// For inspecting the Container/Components objects
// System.out.println(this);
// System.out.println(lblCount);
// System.out.println(tfCount);
// System.out.println(btnCount);

setVisible(true); // "super" Frame shows


// System.out.println(this);
// System.out.println(lblCount);
// System.out.println(tfCount);
// System.out.println(btnCount);
}

// The entry main() method
public static void main(String[] args) {
// Invoke the constructor to setup the GUI, by allocating an instance
DateTime app = new DateTime();

// or simply "new AWTCounter();" for an anonymous instance

}

// ActionEvent handler - Called back upon button-click.
@Override
public void actionPerformed(ActionEvent evt) {
Object actionsource = evt.getSource();
if(actionsource instanceof JTextField){
JTextField dateget1 = (JTextField) evt.getSource();
JTextField dateget2 = (JTextField) evt.getSource();

if (dateget1 == date1){
datedefined1 = date1.getText();
System.out.println(datedefined1);}
else if(dateget2 == date2){

datedefined2 = date2.getText();
System.out.println(datedefined2);}
else{
type = tfCount.getText();
System.out.println(type);


}



}
else if(actionsource instanceof JCheckBox){
JCheckBox cb = (JCheckBox) evt.getSource();
if(cb == source1){
source = "Drivetest";
System.out.println(source);
}
else if(cb == source2){
source = "Ookla Data Dump";
System.out.println(source);
}

}



}
}

问题是,我的主程序在执行之前需要接受并存储多个字符串变量(即类型、源、date1 和 date2)。

我的程序正常终端样式运行的代码如下所示:

 System.out.println("Enter the report type you would like: DailyComparison or HourlyComparison or WeeklyComparison or EventComparison; Type the exact words!");
type = scan.next();
System.out.println("Now enter the type of data you would like analysed: OOKLA or ManualTest: ");
source = scan.next();
if("DailyComparison".equals(type) || "HourlyComparison".equals(type) || "WeeklyComparison".equals(type) ){
Scanner scan2 = new Scanner((System.in));
System.out.println("Now enter the lower bound of the DateTime range(FORMAT YYYY-MM-DD HH:00:00):");
date1 = scan2.nextLine();
System.out.println("Now enter the upper bound of the DateTime range(FORMAT YYYY-MM-DD HH:00:00):");
date2 = scan2.nextLine();
}

正常情况下通过终端进行用户输入。

然后使用用户输入来运行程序的其余部分,调用我定义的其他类中的方法:

Report.report(date1, date2, type, filename, source);// Creates the excel .xlsx file report

MailSender.MailSender(filename, type); // Send a email containing the attached report xlsx file

所以我的问题是:如何扩展此 GUI 代码的功能,以便可以首先收集用户输入的字符串变量,然后用于运行程序的其余部分?

编辑:

谢谢各位的建议。

我有点让它工作,但我不确定结构是否合理。之前发生的情况是,由于每个组件都处理不同的变量,因此我想在调用处理这些变量的主方法类之前先存储所有变量。

所以我创建了一个名为“生成报告”的附加按钮,并在该按钮的 Action 监听器条件+ Action 下,我像这样放置了 class.methods 。基本上我在各个组件(复选框、按钮等)中输入所有变量,然后按“生成报告”

 if (evt.getActionCommand() == "Generate Report") {


if ("DailyComparison".equals(type)) {
filename = "\\Users\\User\\Documents\\Reports\\" + " Daily SpeedTest Telco Comparison Report";
datedefined3 = null;
datedefined4 = null;
datedefined5 = null;
datedefined6 = null;
} else if ("WeeklyComparison".equals(type)) {
filename = "\\Users\\User\\Documents\\Reports\\" + " Weekly Telco Comparison Report";
datedefined3 = null;
datedefined4 = null;
datedefined5 = null;
datedefined6 = null;
} else if ("HourlyComparison".equals(type)) {
filename = "\\Users\\User\\Documents\\Reports\\" + "Hourly Telco Comparison Report";
datedefined3 = null;
datedefined4 = null;
datedefined5 = null;
datedefined6 = null;
}
if("HourlyComparison".equals(type)|"DailyComparison".equals(type)|"WeeklyComparison".equals(type)) {
Report.report(datedefined1, datedefined2, datedefined3, datedefined4, datedefined5, datedefined6, type, filename, source);// Creates the base excel .xlsx file report
LinechartGenerator.chartgen(0, "upload", datedefined1, datedefined2, datedefined3, datedefined4, datedefined5, datedefined6, source, type, filename);
LinechartGenerator.chartgen(0, "download", datedefined1, datedefined2, datedefined3, datedefined4, datedefined5, datedefined6, source, type, filename);
LinechartGenerator.chartgen(0, "latency", datedefined1, datedefined2, datedefined3, datedefined4, datedefined5, datedefined6, source, type, filename);
}


}

尽管代码有其局限性,我不能先按“生成报告”,否则程序只会抛出错误,因为没有存储变量。

我还遇到了一个障碍,我试图找到 Flush Scanner 功能的 Swing 等效项,以允许用户在同一程序实例中生成多个报告。

最佳答案

这将引用一些基本原则:

  • 模型- View - Controller - “数据”与 View 以及用于收集数据的机制分离
  • 观察者模式 - 用于在某些状态发生变化时生成通知,以便感兴趣的各方可以采取行动。

观察者模式在大多数 UI 框架中广泛使用,这些框架往往是事件驱动的(发生了一些事情,你对其做出响应),而不是过程或线性驱动。

通常,您将创建一个“表单”,其中包含捕获所需数据的字段和某种“按钮”,按下该“按钮”将启动下一步 - 验证数据、构建模型并生成表单已完成的通知。

观察者随后将获取模型并根据需要对其进行处理。

这些只是 UI 开发中使用的一些基本概念。看看:

了解更多详情

关于java - 如何在 Java Swing Gui 中存储用户输入的变量以供进一步使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47322487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com