gpt4 book ai didi

打开文件后 Java GUI 刷新结果

转载 作者:行者123 更新时间:2023-12-01 22:17:42 28 4
gpt4 key购买 nike

我正在尝试制作一个所得税计算器,并且我有一个带有菜单栏的MainWindow来打开文件。在我使用 JFileChooser 打开并解析文件后,我将结果放入 Taxpayer 类中。问题是读取文件,但我无法使结果显示在 MainWindow 上。我需要以某种方式返回到 MainWindow 并在打开文件后刷新结果。因此,基本上我需要做的是使用 JFormattedTextField 的适当信息刷新已打开的 MainWindow(其中有空的 JFormattedTextFields)。

这是MainWindow的代码:

package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.JComboBox;
import javax.swing.JTextPane;
import javax.swing.JFormattedTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JCheckBox;
import javax.swing.text.NumberFormatter;

import datamodel.Taxpayer;

public class MainWindow extends JFrame{

private JFrame frame;
private Taxpayer taxpayer;


/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public MainWindow() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 590, 524);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Income Tax Calculator");

JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);

JMenu menuFile = new JMenu("File");
menuBar.add(menuFile);

JMenuItem menuOpenFile = new JMenuItem("Open File");
menuOpenFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FileChooser chooser = new FileChooser();
chooser.setVisible(true);

}
});
menuFile.add(menuOpenFile);

JMenuItem menuExit = new JMenuItem("Exit");
menuExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
}
});
menuFile.add(menuExit);

JMenu menuOptions = new JMenu("Options");
menuBar.add(menuOptions);

JMenu menuTaxpayer = new JMenu("Taxpayer");
menuOptions.add(menuTaxpayer);

JMenuItem menuItemDeleteTaxpayer = new JMenuItem("Delete");
menuItemDeleteTaxpayer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
TaxpayerDeletion deletedTaxpayer = new TaxpayerDeletion();
deletedTaxpayer.setVisible(true);
}
});
menuTaxpayer.add(menuItemDeleteTaxpayer);

JMenu menuReceipt = new JMenu("Receipt");
menuOptions.add(menuReceipt);

JMenuItem menuItemAdd = new JMenuItem("Add");
menuItemAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ReceiptAdding receipt = new ReceiptAdding();
receipt.setVisible(true);
}
});
menuReceipt.add(menuItemAdd);

JMenuItem menuItemDeleteReceipt = new JMenuItem("Delete");
menuItemDeleteReceipt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ReceiptDeletion deletedReceipt = new ReceiptDeletion();
deletedReceipt.setVisible(true);
}
});
menuReceipt.add(menuItemDeleteReceipt);

JMenu menuHelp = new JMenu("Help");
menuBar.add(menuHelp);

JMenuItem menuItemHelp = new JMenuItem("Help");
menuHelp.add(menuItemHelp);

JMenuItem menuItemAbout = new JMenuItem("About");
menuHelp.add(menuItemAbout);
frame.getContentPane().setLayout(null);

JLabel labelSelectTaxpayer = new JLabel("Please select a taxpayer from the list:");
labelSelectTaxpayer.setFont(new Font("Tahoma", Font.PLAIN, 12));
labelSelectTaxpayer.setBounds(25, 21, 211, 23);
frame.getContentPane().add(labelSelectTaxpayer);

JLabel labelPersonalDetails = new JLabel("Personal Details");
labelPersonalDetails.setFont(new Font("Tahoma", Font.BOLD, 14));
labelPersonalDetails.setBounds(238, 69, 211, 14);
frame.getContentPane().add(labelPersonalDetails);

JLabel labelName = new JLabel("Name:");
labelName.setFont(new Font("Tahoma", Font.PLAIN, 12));
labelName.setBounds(56, 105, 46, 14);
frame.getContentPane().add(labelName);

JLabel labelAfm = new JLabel("AFM:");
labelAfm.setFont(new Font("Tahoma", Font.PLAIN, 12));
labelAfm.setBounds(56, 130, 46, 14);
frame.getContentPane().add(labelAfm);

JLabel labelStatus = new JLabel("Status:");
labelStatus.setFont(new Font("Tahoma", Font.PLAIN, 12));
labelStatus.setBounds(56, 155, 46, 14);
frame.getContentPane().add(labelStatus);

JLabel labelIncome = new JLabel("Income:");
labelIncome.setFont(new Font("Tahoma", Font.PLAIN, 12));
labelIncome.setBounds(56, 180, 59, 14);
frame.getContentPane().add(labelIncome);

JLabel labelReceipts = new JLabel("Receipts:");
labelReceipts.setFont(new Font("Tahoma", Font.PLAIN, 12));
labelReceipts.setBounds(56, 205, 59, 23);
frame.getContentPane().add(labelReceipts);

JButton buttonCalculate = new JButton("Calculate");
buttonCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Report report = new Report();
report.setVisible(true);
}
});
buttonCalculate.setFont(new Font("Tahoma", Font.BOLD, 14));
buttonCalculate.setBounds(429, 425, 135, 31);
frame.getContentPane().add(buttonCalculate);

JSeparator separatorLeftDetails = new JSeparator();
separatorLeftDetails.setBounds(10, 81, 218, 2);
frame.getContentPane().add(separatorLeftDetails);

JSeparator separatorRightDetails = new JSeparator();
separatorRightDetails.setBounds(346, 81, 219, 2);
frame.getContentPane().add(separatorRightDetails);

JComboBox comboBoxTaxpayerList = new JComboBox();
comboBoxTaxpayerList.setBounds(246, 23, 319, 20);
frame.getContentPane().add(comboBoxTaxpayerList);

JComboBox comboBoxReceiptsList = new JComboBox();
comboBoxReceiptsList.setBounds(143, 207, 357, 20);
frame.getContentPane().add(comboBoxReceiptsList);


JFormattedTextField formattedTextFieldAfm = new JFormattedTextField();
formattedTextFieldAfm.setEditable(false);
formattedTextFieldAfm.setEnabled(true);
if(taxpayer != null){
System.out.println("TAXPAYER NOT NULL");
formattedTextFieldAfm.setText(String.valueOf(taxpayer.getTaxRegistrationNumber()));
formattedTextFieldAfm.setVisible(true);
}

formattedTextFieldAfm.setBounds(143, 128, 357, 20);
frame.getContentPane().add(formattedTextFieldAfm);

JFormattedTextField formattedTextFieldStatus = new JFormattedTextField();
formattedTextFieldStatus.setEditable(false);
formattedTextFieldStatus.setEnabled(true);
if(taxpayer != null){
formattedTextFieldStatus.setText(taxpayer.getStatus());
}

formattedTextFieldStatus.setBounds(143, 153, 357, 20);
frame.getContentPane().add(formattedTextFieldStatus);

JFormattedTextField formattedTextFieldIncome = new JFormattedTextField();
formattedTextFieldIncome.setEditable(false);
formattedTextFieldIncome.setEnabled(true);
//formattedTextFieldIncome.setText(String.valueOf(taxpayer.getIncome()));
formattedTextFieldIncome.setBounds(143, 178, 357, 20);
frame.getContentPane().add(formattedTextFieldIncome);

JFormattedTextField formattedTextFieldName = new JFormattedTextField();
formattedTextFieldName.setEditable(false);
formattedTextFieldName.setEnabled(true);
//formattedTextFieldName.setText(taxpayer.getName());
formattedTextFieldName.setBounds(143, 103, 357, 20);
frame.getContentPane().add(formattedTextFieldName);

JSeparator separatorLeftCalculation = new JSeparator();
separatorLeftCalculation.setBounds(10, 253, 218, 2);
frame.getContentPane().add(separatorLeftCalculation);

JLabel lblTaxCalculation = new JLabel("Tax Calculation");
lblTaxCalculation.setFont(new Font("Tahoma", Font.BOLD, 14));
lblTaxCalculation.setBounds(233, 227, 200, 50);
frame.getContentPane().add(lblTaxCalculation);

JSeparator separatorReightCalculation = new JSeparator();
separatorReightCalculation.setBounds(340, 253, 225, 2);
frame.getContentPane().add(separatorReightCalculation);

JLabel lblPleaseSelectOne = new JLabel("Please select one or more options for tax calculation");
lblPleaseSelectOne.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblPleaseSelectOne.setBounds(155, 266, 505, 23);
frame.getContentPane().add(lblPleaseSelectOne);

JCheckBox checkBoxTotalTax = new JCheckBox("Total Tax");
checkBoxTotalTax.setBounds(93, 306, 150, 23);
frame.getContentPane().add(checkBoxTotalTax);

JCheckBox chechBoxTaxAnalysis = new JCheckBox("Tax analysis(Basic tax, tax increase or decrease based on receipts)");
chechBoxTaxAnalysis.setBounds(92, 332, 357, 23);
frame.getContentPane().add(chechBoxTaxAnalysis);

JCheckBox checkBoxReceiptsTotalValue = new JCheckBox("Total value of the gathered receipts");
checkBoxReceiptsTotalValue.setBounds(93, 358, 271, 23);
frame.getContentPane().add(checkBoxReceiptsTotalValue);

JCheckBox checkBoxAmountDifferentCategories = new JCheckBox("Analysis of the amounts spent for different categories of expenditure");
checkBoxAmountDifferentCategories.setBounds(93, 384, 421, 23);
frame.getContentPane().add(checkBoxAmountDifferentCategories);


}

public void setTaxpayerInfo(Taxpayer taxpayer){
this.taxpayer = taxpayer;
System.out.println("Test message :Inside");
}
}

这是FileChooser的代码:

package gui;


import input.TxtParser;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;








import datamodel.Taxpayer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;

public class FileChooser extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private Taxpayer taxpayer;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FileChooser frame = new FileChooser();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FileChooser() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 593, 436);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
fileChooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()!= null){
File file = fileChooser.getSelectedFile();
TxtParser txtParser = new TxtParser();

try {
taxpayer = txtParser.parse(file);
if(taxpayer == null){
System.out.println("NULL TAXPAYER");
}
System.out.println(taxpayer);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

MainWindow mainWindow = new MainWindow();
mainWindow.setTaxpayerInfo(taxpayer);
mainWindow.setEnabled(true);
mainWindow.setVisible(true);

close();
}
}
});
fileChooser.setBounds(0, 0, 582, 397);
contentPane.add(fileChooser);
this.setTitle("Income Tax Calculator");
}



private void close(){
this.dispose();
}
}

最佳答案

您有很多问题...

首先,您的 MainWindow 类从 JFrame 扩展,但也创建 JFrame 的另一个实例

public class MainWindow extends JFrame{

private JFrame frame;

哪一帧实际上包含内容或在屏幕上可见?我首先删除 extends JFrame

FileChooser 类不是必需的,您正在重复 JFileChooser 类本身提供的功能,并且这样做会让您的生活变得困难......

JMenuItem menuOpenFile = new JMenuItem("Open File");
menuOpenFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser chooser = new JFileChooser();
switch (chooser.showOpenDialog(frame)) {
case JFileChooser.APPROVE_OPTION:
File file = fileChooser.getSelectedFile();
TxtParser txtParser = new TxtParser();

try {
taxpayer = txtParser.parse(file);
if (taxpayer == null) {
System.out.println("NULL TAXPAYER");
}
System.out.println(taxpayer);
setTextpayerInfo(taxPayer);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
}
}
});

在现有的 FileChooser 类中,您创建 MainWindow 的另一个实例并调用 setTextpayerInfo,但 setTextpayerInfo 不会什么都不做?!

您的 setTextpayerInfo 方法需要填写您希望用户能够操作的字段或控件。

将您的功能分成单独的类,以便您拥有一个 TaxPayerPane,其中包含与 TaxPayer 的管理直接相关的字段和控件类。

然后将您需要的功能(菜单)和 TaxPayerPane 组合到 JFrame 的单个实例上。根据需要使用 setter/getter 在应用程序的各个部分之间传递信息。

这里的想法是分离和限制应用程序的不同部分,以便它们只执行其设计的单一工作。这可以解耦您的代码,使其更易于管理和更改,并减少尝试管理不同逻辑 block 的重叠复杂性

此外,请避免使用 null 布局,它们比它们的值(value)更让人头疼。利用适当的布局管理器,将它们组合起来不仅可以生成丰富的 UI,还可以生成灵活的 UI,可以适应平台和外观之间的差异

关于打开文件后 Java GUI 刷新结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30662977/

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