JFrame控制台扫描程序

时间:2014-11-29 13:37:58

标签: java swing jframe java.util.scanner

我有这个应用程序在eclipse的控制台中运行,我希望它在一个jframe中运行。 我的意思是我希望它在JFrame窗口上询问name,a和b然后在文件上写一些东西。

它在控制台中完美运行,但我不知道如何将其作为JFrame运行。

我希望它看起来像这样(在photoshop中制作的图像): http://i.imgur.com/rTWko1R.png 然后自动关闭

提前致谢!

some imports...(trying to save space)

public class Test {

    public static void main(String[] args) throws FileNotFoundException,IOException {

    Scanner s = new Scanner(System.in);
    String fileName = new SimpleDateFormat("dd-MM-yyyy_HH-mm'.txt'").format(new Date());
    String obs;
    String name;
    String path = "some path";
    int a = 0, b = 0, c = 0, d = 0;
    System.out.println("input file name");
    name = s.nextLine();
    System.out.println("input a");
    a = s.nextInt();
    System.out.println("input b");
    b = s.nextInt();
    obs = s.nextLine();
    if (a >= 100) {
        d = a / 100;
        c = a % 100;
        b = c;
        a = a + d;
    }
    File file;
    if (StringUtils.isBlank(name)) {
        file = new File(path + fileName);
    } else {
        file = new File(path + name + "#" + fileName);
    }

    FileWriter writer = null;
    try {
        writer = new FileWriter(file);
        writer.write("something");

        if (StringUtils.isBlank(obs)) {
            writer.write("something");
        } else {
            writer.write(obs + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException ignore) {
            }
    }

}
}

2 个答案:

答案 0 :(得分:1)

您需要做什么

  • 将您的核心逻辑分离为一个单独的方法,该方法采用String name,int a,int b,理想情况下在一个单独的类中 - 然后您可以从控制台版本重用
  • 使用按钮启动框架中的基本GUI以启动流程
  • 按下按钮并调用核心逻辑方法
  • 必要时添加输入验证
  • 考虑使用JFileChooser来允许用户选择文件而不必在
  • 中输入文件

实施例

public class ConsoleInFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ConsoleInFrame().showGui();
            }
        });
    }

    public void showGui() {
        JFrame frame = new JFrame();
        JTextField file = new JTextField(20);
        JTextField aText = new JTextField(4);
        JTextField bText = new JTextField(4);
        JButton go = new JButton("Go");

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3, 2));
        panel.add(new JLabel("File"));
        panel.add(file);
        panel.add(new JLabel("a"));
        panel.add(aText);
        panel.add(new JLabel("b"));
        panel.add(bText);

        frame.getContentPane().setLayout(
                new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
        frame.getContentPane().add(panel);

        frame.getContentPane().add(go);

        go.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                process(file.getText(), Integer.parseInt(aText.getText()),
                        Integer.parseInt(bText.getText()));
            }
        });
        frame.pack();
        frame.setVisible(true);
    }

    public void process(String name, int a, int b) {
        String fileName = new SimpleDateFormat("dd-MM-yyyy_HH-mm'.txt'")
                .format(new Date());
        String obs;
        String path = "some path";
        int c = 0, d = 0;
        if (a >= 100) {
            d = a / 100;
            c = a % 100;
            b = c;
            a = a + d;
        }
        File file;
        if (StringUtils.isBlank(name)) {
            file = new File(path + fileName);
        } else {
            file = new File(path + name + "#" + fileName);
        }

        FileWriter writer = null;
        try {
            writer = new FileWriter(file);
            writer.write("something");

            if (StringUtils.isBlank(obs)) {
                writer.write("something");
            } else {
                writer.write(obs + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (IOException ignore) {
                }
        }

    }
}

答案 1 :(得分:0)

我认为你可以这样做:

enter image description here

要执行此操作,您必须使用JLabel来显示文字:https://docs.oracle.com/javase/tutorial/uiswing/components/label.html

然后使用JTextField获取输入: https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html

如果您愿意,可以在JButton中写入后使用JTextField将所有内容保存到文件中: https://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html http://www.javamex.com/tutorials/swing/jbutton.shtml

相关问题