繁体   English   中英

Java 程序运行,但没有 GUI

[英]Java Program Runs, but No GUI

我正在学习 Java 并试图制作一个程序。 程序运行良好,但没有出现 GUI! 这个概念是输入一个从 1 到 10 的数字,然后它会向您发送一条消息。 我想出了如何在控制台中做到这一点,但我正试图让它在 GUI 中工作。 有任何想法吗?

import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import java.awt.event.*;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.FileWriter;


public class App extends JFrame {
JLabel label;
JTextField tf;
JButton button;

    public App () {
        setLayout(new FlowLayout());
        label = new JLabel("On a scale of 1 to 10, how are you feeling today?");
        add(label);
        tf = new JTextField(10);
        add(tf);
        button = new JButton("Enter");
        add(button);

        event e = new event();
        button.addActionListener(e);
    }
    public class event implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try{
                String word =tf.getText();
                FileWriter stream = new FileWriter("~/Library/Application Support/Cookies160/file.txt");
                BufferedWriter out = new BufferedWriter(stream);
                out.write(word);
                out.close();
            }catch(Exception ex) {}
        }
    }
    public static void main(String[] args)
    {
        App gui = new App();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(300,150);
        gui.setTitle("My Program");
        gui.setVisible(true);

     {

    Scanner input = new Scanner(System.in);

        System.out.println("On a scale of 1 to 10, how are you feeling today?");
    int line = input.nextInt();

    if (line < 5) {
        System.out.println("Really? I hope it gets better!");
    }

    if (line == 7) {
        System.out.println("That's good to hear!");
    }

    if (line == 8) {
        System.out.println("That's good to hear!");
    }

    if (line == 9) {

        System.out.println("That's good to hear!"); 
    }

    if (line == 10) {
        System.out.println("That's good to hear!");
    }

    if (line == 5) {
        System.out.println("Hmm... You should try to have more fun!");
    }

    if (line == 6) {
        System.out.println("Hmm... You should try to have even just a bit more fun!");
    }

    if (line < 0) {
        System.out.println("Oh?... Well, I hope it gets much better!");
    }

    if (line > 10) {
        System.out.println("Ha, I love people with your kind of enthusiasm!");
    }
    if (line == 42) {
        System.out.println("It's also great to see that your feeling full of life!");
    }

    if (line == 69) {
        System.out.println("Also,... -_-");
    }
}}}

你在任何时候都没有打包你的框架。 查看Oracle 教程以开始使用 Swing。

也许是这样的:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class App extends JFrame {
    JLabel label;
    JTextField tf;
    JButton button;
    private JLabel answerLabel;

    public App() {
        setLayout(new FlowLayout());
        label = new JLabel("On a scale of 1 to 10, how are you feeling today?");
        add(label);
        tf = new JTextField(10);
        add(tf);
        button = new JButton("Enter");
        add(button);
        answerLabel = new JLabel();
        add(answerLabel);

        ActionListenerImpl e = new ActionListenerImpl();
        button.addActionListener(e);
    }

    public class ActionListenerImpl implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                String answer = getAnswer(tf.getText());
                answerLabel.setText(answer);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InvocationTargetException,
            InterruptedException {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                App gui = new App();
                gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                gui.setSize(300, 150);
                gui.setTitle("My Program");
                gui.setVisible(true);
            }
        });
    }

    private String getAnswer(String input) {

        int line = Integer.parseInt(input);

        if (line < 5) {
            return "Really? I hope it gets better!";
        }

        if (line == 7) {
            return "That's good to hear!";
        }

        if (line == 8) {
            return "That's good to hear!";
        }

        if (line == 9) {

            return "That's good to hear!";
        }

        if (line == 10) {
            return "That's good to hear!";
        }

        if (line == 5) {
            return "Hmm... You should try to have more fun!";
        }

        if (line == 6) {
            return "Hmm... You should try to have even just a bit more fun!";
        }

        if (line < 0) {
            return "Oh?... Well, I hope it gets much better!";
        }

        if (line > 10) {
            return "Ha, I love people with your kind of enthusiasm!";
        }
        if (line == 42) {
            return "It's also great to see that your feeling full of life!";
        }

        if (line == 69) {
            return "Also,... -_-";
        }
        return "???";
    }
}

您可能会调用该方法

gui.setAlwaysOnTop(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM