java - 多次运行 GUI 客户端而无需关闭它

标签 java swing

我正在制作一个 GUI,在其中将字符串输入到文本框中,一旦单击 J 按钮,第二个文本框将生成我在第一个文本框中输入的字符串,或者从我创建的方法(public void Associate())。当我运行 GUI 并单击按钮在第二个文本框中生成文本时,一切正常。但是,当我第二次单击该按钮以使 GUI 执行相同的操作时,什么也没有发生。我可以做些什么,这样我就不必每次想多次运行 GUI 时都关闭它?

public class GUIWindow extends JFrame {
private Joketeller robot= new Joketeller();
private JLabel speakerlabel = new JLabel("Joke");
private JLabel MarcoLabel= new JLabel ("Marco");
private JTextField speakerfield= new JTextField ("Enter Joke Here");
private JTextField Marcofield= new JTextField ("",20);
private JButton Jokebutton=new JButton("Recite Joke >>>");

public GUIWindow()  {
    JPanel dataPanel= new JPanel(new GridLayout(2,2,12,16));
    dataPanel.add(speakerlabel);
    dataPanel.add(MarcoLabel);
    dataPanel.add(speakerfield);
    dataPanel.add(Marcofield);

    JPanel buttonPanel= new JPanel();
    buttonPanel.add(Jokebutton);
    Container container = getContentPane();
    container.add(dataPanel,BorderLayout.CENTER);
    container.add(buttonPanel,BorderLayout.SOUTH);
    Jokebutton.addActionListener(new JokeListener());
}

    private class JokeListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        String input=speakerfield.getText();
        if (Jokebutton.isEnabled()) {
        robot.setJoke(input);
        String Response= robot.getResponse();
        Marcofield.setText(Response);}

说笑者类:

public class Joketeller {


    private static String Marco;
    private static String Response;
    static int i= (int)(Math.random()*((5-1)+1)+1);
    static String r;

    public void setMarco(String Joke ) {
        Marco=Joke;
    }

    public void setJoke(String Joke) {
        Marco=Joke;
        associate();

    }


    public String getJoke() {
        return Marco;
    }

    public static String getMarco() {
        return Marco;
    }

        public static void associate(){
        if(i==1) 
            r= "Connect Angie";
        else if(i==2)
            r= "*Cloud Laugh*";
        else if(i==3)
            r= "Community";
        else if(i==4)
            r=getMarco();
        else if(i==5)
            r= "Indeed!";
        Response=r;

        }

    public String getResponse() {
        return Response;
    }

    }

感谢任何帮助。谢谢。

最佳答案

我首先想到的是过度使用static...

public class Joketeller {


    private static String Marco;
    private static String Response;
    static int i= (int)(Math.random()*((5-1)+1)+1);
    static String r;

这对您没有帮助,如果做得正确,则不需要。

下一期是关于i...

    static int i = (int) (Math.random() * ((5 - 1) + 1) + 1);

    public static void associate() {
        if (i == 1) {
            r = "Connect Angie";
        } else if (i == 2) {
            r = "*Cloud Laugh*";
        } else if (i == 3) {
            r = "Community";
        } else if (i == 4) {
            r = getMarco();
        } else if (i == 5) {
            r = "Indeed!";
        }
        Response = r;

    }

i 从未改变。因为它是静态,所以您可以根据需要创建任意数量的 Joketeller 实例,并且需要更改它,因此响应将始终相同。

虽然有多种可能的方法来修复它,但最简单的方法是删除所有 static 并使 i 成为 associate< 中的局部变量,因为它确实没有在其他地方使用过..

public void associate() {
    int rnd = (int) (Math.random() * ((5 - 1) + 1) + 1);
    if (rnd == 1) {
        r = "Connect Angie";
    } else if (rnd == 2) {
        r = "*Cloud Laugh*";
    } else if (rnd == 3) {
        r = "Community";
    } else if (rnd == 4) {
        r = getMarco();
    } else if (rnd == 5) {
        r = "Indeed!";
    }
    response = r;

}

这意味着您无需创建 Joketeller 的新实例即可获得不同的响应。

例如......

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GUIWindow extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GUIWindow wnd = new GUIWindow();
                wnd.pack();
                wnd.setLocationRelativeTo(null);
                wnd.setVisible(true);
            }
        });
    }

    private Joketeller robot = new Joketeller();
    private JLabel speakerlabel = new JLabel("Joke");
    private JLabel marcoLabel = new JLabel("Marco");
    private JTextField speakerfield = new JTextField("Enter Joke Here");
    private JTextField marcofield = new JTextField("", 20);
    private JButton jokebutton = new JButton("Recite Joke >>>");

    public GUIWindow() {
        JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 16));
        dataPanel.add(speakerlabel);
        dataPanel.add(marcoLabel);
        dataPanel.add(speakerfield);
        dataPanel.add(marcofield);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(jokebutton);
        Container container = getContentPane();
        container.add(dataPanel, BorderLayout.CENTER);
        container.add(buttonPanel, BorderLayout.SOUTH);
        jokebutton.addActionListener(new JokeListener());
    }

    private class JokeListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String input = speakerfield.getText();
            if (jokebutton.isEnabled()) {
                robot.setJoke(input);
                String Response = robot.getResponse();
                marcofield.setText(Response);
            }
        }
    }

    public class Joketeller {

        private String marco;
        private String response;
        private String r;

        public void setMarco(String Joke) {
            marco = Joke;
        }

        public void setJoke(String Joke) {
            marco = Joke;
            associate();

        }

        public String getJoke() {
            return marco;
        }

        public String getMarco() {
            return marco;
        }

        public void associate() {
            int rnd = (int) (Math.random() * ((5 - 1) + 1) + 1);
            if (rnd == 1) {
                r = "Connect Angie";
            } else if (rnd == 2) {
                r = "*Cloud Laugh*";
            } else if (rnd == 3) {
                r = "Community";
            } else if (rnd == 4) {
                r = getMarco();
            } else if (rnd == 5) {
                r = "Indeed!";
            }
            response = r;

        }

        public String getResponse() {
            return response;
        }

    }

}

关于java - 多次运行 GUI 客户端而无需关闭它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53875639/

相关文章:

Java 多捕获/重新抛出

java - 如何将鼠标监听器添加到包含呈现为复选框的 boolean 值的 JTable 单元格

java - 土耳其字符的小写点缀 i

java - 无法设置 guest 内存 'android_arm' : Invalid argument

Java单进程多线程

java - 如何点击JTable本身?

java - JButton 助记符仅在 ALT 键上有下划线

java - 如何在java中创建可检查的 ListView

java - 如何使主窗口对表格单击使用react

java - 快速简单的 Java 程序,可与 JIRA 对话并创建史诗、故事和问题