如何在Java中创建一个接受动态字符串值的工作簿?

huangapple go评论84阅读模式
英文:

How can i make a workbook in java which accepts dynamic values of string in java?

问题

这是一个简单的程序,我有一个文本字段,用户可以输入内容。然后,我将输入存储在一个全局变量中,并创建一个函数来获取工作簿的文件路径。然而,我被要求将其更改为静态,并且我创建的所有文件都被命名为 null。我使用的是 Eclipse IDE 和 Apache POI 版本 5.0.0。

// 导入的文件
public class MyFrame extends JFrame implements ActionListener {
    String name;
    JButton button;
    JTextField textField;
    
    String workbook1() {
        String name1 = "D:\\" + name + ".xlsx";
        return name1;
    }
    
    MyFrame() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());
        
        button = new JButton("Submit");
        button.addActionListener(this);
        
        textField = new JTextField();
        textField.setPreferredSize(new Dimension(250, 40));
        textField.setFont(new Font("Consolas", Font.PLAIN, 35));
        textField.setForeground(new Color(0x00FF00));
        textField.setBackground(Color.black);
        textField.setCaretColor(Color.white);
        textField.setText("username");
        
        this.add(button);
        this.add(textField);
        this.pack();
        this.setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            System.out.println("Welcome " + textField.getText());
            name = textField.getText();
            // button.setEnabled(false);
            // textField.setEditable(false);
        }
    }
    
    public static void main(String args[]) throws IOException {
        new MyFrame();
        Workbook wb = new XSSFWorkbook();
        Sheet sheet1 = wb.createSheet("Ripon");
        FileOutputStream fileOut = new FileOutputStream(workbook1());
        wb.write(fileOut);
        fileOut.close();
        wb.close();
    }
}

我只是一个初学者,任何帮助都会受益。这段代码是从 BroCode 和 Stack Overflow 借鉴来的。

我正在尝试创建一个 Java 应用程序,其中我可以输入不同列和行的详细信息,以创建一种账单。我观看了教程并根据自己的需求进行了自定义。

英文:

Its a simple program where i have a text field and i get user input .
i store it in a global variable and create a function to get the file path for the workbook.
however i am made to change it to static and all my files created are named null.
i am using eclipse IDE and apache poi version. 5.0.0.

// imported files
public class MyFrame extends JFrame implements ActionListener{
String name;
JButton button;
JTextField textField;
String workbook1() {
String name1="D:\\"+name+".xlsx";
return name1;
}
MyFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
button = new JButton("Submit");
button.addActionListener(this);
textField = new JTextField();
textField.setPreferredSize(new Dimension(250,40));
textField.setFont(new Font("Consolas",Font.PLAIN,35));
textField.setForeground(new Color(0x00FF00));
textField.setBackground(Color.black);
textField.setCaretColor(Color.white);
textField.setText("username");
this.add(button);
this.add(textField);
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button) {
System.out.println("Welcome "+ textField.getText());
name=textField.getText();
//button.setEnabled(false);
//textField.setEditable(false);
}
}
public static void main(String args[]) throws IOException {
new MyFrame();
Workbook wb = new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("Ripon");
FileOutputStream fileOut = new FileOutputStream(workbook1());
wb.write(fileOut);
fileOut.close();
wb.close();
}
}

I am just a beginner and any help is appreciated. The code is stolen from BroCode and Stack overflow

I am trying to create a java application where i can enter details into different columns and rows to create a kind of bill.
I watch tutorial and customize it to my need.

答案1

得分: 0

我得到了我的问题的答案:不应该创建一个新的全局变量,而是应该在actionPerformed方法中创建一个新的表单。

mainbutton.setText("提交");
mainbutton.setPreferredSize(new Dimension(100, 25));
maintext.setPreferredSize(new Dimension(100, 25));
mainbutton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == mainbutton) {
            try {
                XSSFWorkbook workbook = new XSSFWorkbook();
                Sheet sheet = workbook.createSheet("数据");
                FileOutputStream fileOutputStream = new FileOutputStream(".\\documents\\" + maintext.getText() + ".xlsx");
                workbook.write(fileOutputStream);
                fileOutputStream.close();
                System.out.println("工作簿已保存到文件:" + maintext + ".xlsx");
                maintext.setEditable(false);
                mainbutton.setEnabled(false);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
});

// 将内容添加到主框架
mainframe.add(mainlabelheading, BorderLayout.NORTH);
mainframe.add(mainlabelheading2, BorderLayout.CENTER);
mainframe.add(mainlabelheadingempty, BorderLayout.SOUTH);
gbc.gridx = 0;
gbc.gridy = 0;
mainlabelheading2.add(mainlabelheading2sublabel1, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
mainlabelheading2.add(mainbutton, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
mainlabelheading2.add(maintext, gbc);

在代码中玩耍后,我发现我从静态方法引用了一个非静态变量。当我将全局变量更改为静态变量时,它会创建空的.xlsx文件。然后使用ChatGPT和StackOverflow找到了我的解决方案。

英文:

I got the answer to my question: instead of creating a new global variable I should have made a new form in the actionPerformed method.

mainbutton.setText("SUBMIT");
mainbutton.setPreferredSize(new Dimension(100,25));
maintext.setPreferredSize(new Dimension(100,25));
mainbutton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == mainbutton) {
            try {
                XSSFWorkbook workbook = new XSSFWorkbook();
                Sheet sheet = workbook.createSheet("Data");
                FileOutputStream fileOutputStream = new FileOutputStream(".\\documents\\"+maintext.getText() + ".xlsx");
                workbook.write(fileOutputStream);
                fileOutputStream.close();
                System.out.println("Workbook saved to file: " + maintext + ".xlsx");
                maintext.setEditable(false);
                mainbutton.setEnabled(false);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
});

//adding stuff to the main frame	
mainframe.add(mainlabelheading,BorderLayout.NORTH);
mainframe.add(mainlabelheading2,BorderLayout.CENTER);
mainframe.add(mainlabelheadingempty,BorderLayout.SOUTH);
gbc.gridx=0;
gbc.gridy=0;
mainlabelheading2.add(mainlabelheading2sublabel1,gbc);
gbc.gridx=2;
gbc.gridy=0;
mainlabelheading2.add(mainbutton,gbc);
gbc.gridx=1;
gbc.gridy=0;
mainlabelheading2.add(maintext,gbc);

Did play with the code and found out I was referencing a non static variable from a static method. When I changed the global variable to static it was creating null .xlsx files. Then used ChatGPT and stackoverflow to find the answer for my solutions.

huangapple
  • 本文由 发表于 2023年6月28日 23:48:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76574800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定