2011-04-03 39 views
0
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個按鈕的框架的代碼。每個按鈕都有自己的功能。我現在面臨的問題是,在點擊第一個按鈕之後,代碼爲第一個按鈕運行,之後框架關閉。如何在第一個按鈕代碼運行後維護框架,允許我在第一個按鈕運行後單擊其他按鈕。僅執行一個按鈕後,框架關閉

+1

正如你在antoher問題中已經說過的,請閱讀關於swing的入門教程。背後的模型與你期望的完全不同。 – Howard 2011-04-03 08:28:09

+0

'SimpleWebCrawler.main(null);'調用'System.exit'? – khachik 2011-04-03 08:30:00

回答

2

擺脫SimpleWebCrawler類中的main,將SimpleWebCrawler類轉換爲JDialog而不是JFrame,並創建一個實例並使其在按鈕的動作偵聽器中可見。

+0

嗨costis,你是說我需要改變上面的代碼? – jasper 2011-04-03 08:30:03

+0

嗨,你能提供一些例子嗎?謝謝。 – jasper 2011-04-03 08:31:03

+0

在按鈕的動作監聽器中稍微有點兒。您需要將其他類更改爲JDialog並相應地調用它。像這樣:new SimpleWebCrawler(myHTMLText).setVisible(true); – 2011-04-03 08:34:21