我想制作一个GUI程序,用y写入文件中的x个数字。
例如:如果y = 103且x = 100,则输出为" 103 104 105 106 ... 202"(100个数字)。
我尝试了这段代码,但是当我运行它并提交一些数字时,该文件仍然是空的。
可能问题出在void filein()
。
package me;
import java.io.FileInputStream;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class program {
static PrintStream OUT = System.out;
public static void main(String args[]) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
startGUI();
}
});
}
private static void startGUI() {
JFrame window = new JFrame();
window.setTitle("Text Writer");
{
setupWindowContent(window);
}
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
private static void setupWindowContent(JFrame window) {
JPanel content = new JPanel();
final JLabel lblEnterSmthng = new JLabel("Enter the first number and the iterate limit:");
final JTextField txtField1 = new JTextField(30);
final JTextField txtField2 = new JTextField(30);
JButton btnOk = new JButton("Ok");
final JLabel labelLastText = new JLabel();
content.add(lblEnterSmthng);
content.add(txtField1);
content.add(txtField2);
content.add(btnOk);
content.add(labelLastText);
window.setContentPane(content);
ActionListener submitAction = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String txt = txtField1.getText();
String txt2 = txtField2.getText();
int i = Integer.parseInt(txt);
int serii = Integer.parseInt(txt2);
if(!txt.isEmpty()){
labelLastText.setText(txt);
}
}
void filein(int i, int serii) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
out = new FileOutputStream("C:/Users/mihai/OneDrive/Desktop/Text.txt");
System.out.print(i + serii);
for(i=i;i<i+serii-1;i++) {
out.write(i);
}
}finally {
if (out != null) {
out.close();
}
}
}
};
btnOk.addActionListener(submitAction);
txtField1.addActionListener(submitAction);
txtField2.addActionListener(submitAction);
}
}
我不知道给你什么细节。