java - 仅执行一个按钮后,框架关闭

标签 java swing

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Main {

    private static void createAndShowGUI()  {

        JFrame frame1 = new JFrame("FINAL YEAR PROJECT VER 1.0");
        JTextArea test = new JTextArea(200, 200);
        frame1.setSize(500,500);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.add(test);

        FlowLayout experimentLayout = new FlowLayout();
        experimentLayout.setAlignment(FlowLayout.CENTER);
        frame1.setLayout(experimentLayout);



        JButton button = new JButton("Extract Links");
        JButton button1 = new JButton("Render To Text");
        JButton button2 = new JButton("Calculate Similarity");
        JButton button3 = new JButton("File Search");
        JButton button4 = new JButton("Exit");



        //Add action listener to button

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                try {
                    SimpleWebCrawler.main(null);

                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });      
      //Add action listener to button 1
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                try {
                    RenderToText.main(null);
                } catch (IOException e1) {

                    e1.printStackTrace();
                }
            }
        });

        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                try {
                 readFile.main(null);

                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });   

        button3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                try {
                    FileInput.main(null);
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });   

        button4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });     



        frame1.getContentPane().add(button);
        frame1.getContentPane().add(button1);
        frame1.getContentPane().add(button2);
        frame1.getContentPane().add(button3);
        frame1.getContentPane().add(button4);



        frame1.setVisible(true);
    }


    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

这是用于创建带有 5 个按钮的框架的代码。每个按钮都有自己的功能。我现在面临的问题是,单击第一个按钮后,代码会针对第一个按钮运行,然后框架关闭。如何在第一个按钮代码运行后维护框架,允许我在第一个按钮运行后单击其他按钮。

最佳答案

摆脱 SimpleWebCrawler 类中的 main 类,将 SimpleWebCrawler 类转换为 JDialog 而不是 JFrame 并创建一个实例并使其在按钮的操作监听器中可见。

关于java - 仅执行一个按钮后,框架关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5528450/

相关文章:

java - 如何正确使用来自 Actionlistener 事件的输入

java - 在 Guava 事件线程安全上更新 JTable 的模式

java - 有没有办法让我的地址栏的 JTextField 更大更 flex

java - 如何确定将为给定 Java Web 服务发出的 WSDL?

java - ehcache 不会在驱逐时从内存中删除元素

java - 列表大小具有意想不到的值(value)

java - 如何让程序等待用户输入?

java - 在Java Swing中设置GridBagLayout在JPanel顶部的位置

java - 将字符串转换为 XMLDocument 不会创建文本节点

java - <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>