Java 多次运行GUI客户端而不必关闭它

Java 多次运行GUI客户端而不必关闭它,java,swing,Java,Swing,我正在制作一个GUI,在其中我将字符串输入到一个文本框中,一旦我单击一个Jbutton,第二个文本框将生成我输入到第一个文本框中的字符串,或者从我创建的方法(public void associate())生成一个随机字符串。当我运行GUI并单击按钮生成第二个文本框中的文本时,一切正常。但是,当我再次单击按钮以便GUI执行相同的操作时,什么也没有发生。有什么我可以做的,使我不必关闭图形用户界面每次我希望运行多次 public class GUIWindow extends JFrame { pr

我正在制作一个GUI,在其中我将字符串输入到一个文本框中,一旦我单击一个Jbutton,第二个文本框将生成我输入到第一个文本框中的字符串,或者从我创建的方法(public void associate())生成一个随机字符串。当我运行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;
    }

    }

感谢您的帮助。谢谢。

我首先想到的是过度使用
静态

public class Joketeller {


    private static String Marco;
    private static String Response;
    static int i= (int)(Math.random()*((5-1)+1)+1);
    static String r;
    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

public class Joketeller {


    private static String Marco;
    private static String Response;
    static int i= (int)(Math.random()*((5-1)+1)+1);
    static String r;
    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
实例,并且它需要更改,所以响应总是相同的

虽然有许多可能的方法来修复它,但最简单的方法是删除所有的
静态
,并使
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命名约定:包、属性、变量、参数、方法必须以小写字母开头,而类、接口应该以大写字母开头。既然
i
从未改变过,为什么还要期望其他任何东西改变呢