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

时间:2018-12-20 20:31:41

标签: java swing

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

Joketeller班:

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;
    }

    }

感谢您的帮助。谢谢。

1 个答案:

答案 0 :(得分:2)

跳到我身上的第一件事是过度使用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从未更改。因为它是static,所以您可以根据需要创建任意数量的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;
        }

    }

}