Java程序运行,但没有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);

相关内容

  • 没有找到相关文章

最新更新