2013-05-10 73 views
0

好吧,現在我有這樣的代碼:從Swing GUI到系統控制檯?

package program.window; 
import jaco.mp3.player.MP3Player; 
public class obj { 
private JFrame frame; 
private static int xPosition = 30, yPosition = 30; 
final JDesktopPane desktopPane = new JDesktopPane(); 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       obj window = new obj(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public obj() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setTitle("bD suite v.0.0012__EARLY__ALPHA"); 
    frame.setBounds(574, 100, 745, 542); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(new BorderLayout(0, 0)); 

    JPanel panel = new JPanel(); 
    frame.getContentPane().add(panel, BorderLayout.NORTH); 

    final JDesktopPane desktopPane = new JDesktopPane(); 
    frame.getContentPane().add(desktopPane, BorderLayout.CENTER); 

    JButton btnMp3 = new JButton("Mp3"); 
    btnMp3.setIcon(new ImageIcon("/home/zmaj/Pictures/free-mp3-cutter-and-editor.png")); 
    btnMp3.setSelectedIcon(new ImageIcon("/home/zmaj/Pictures/free-mp3-cutter-and-editor.png")); 

    btnMp3.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      JFileChooser mp3FileChooser = new JFileChooser(); 
      mp3FileChooser.showOpenDialog(frame); 
      File selectedFile = mp3FileChooser.getSelectedFile(); 
      if (selectedFile != null) { 
       JInternalFrame mp3Frame = 
         new JInternalFrame(selectedFile.getName(), true, true, true, true); 
       mp3Frame.setTitle("MP3 player"); 
       mp3Frame.getContentPane().add(new JLabel("Sviram " + selectedFile.getName())); 
       mp3Frame.setSize(200, 50); 
       mp3Frame.setLocation(xPosition, yPosition); 
       xPosition += 100; 
       yPosition += 25; 
       desktopPane.add(mp3Frame); 
       mp3Frame.setVisible(true); 
       final MP3Player player = new MP3Player(selectedFile); 
       player.play(); 
       mp3Frame.addInternalFrameListener(new InternalFrameAdapter() { 
        public void internalFrameClosing(InternalFrameEvent e) { 
         player.stop(); 
        } 
       }); 
      } 

     } 
    }); 
    panel.add(btnMp3); 

    JButton btnImage = new JButton("Image"); 
    btnImage.setIcon(new ImageIcon("/home/zmaj/Pictures/cloud-image.jpg")); 
    btnImage.setSelectedIcon(new ImageIcon("/home/zmaj/Pictures/cloud-image.jpg")); 
    btnImage.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      JFileChooser imageFileChooser = new JFileChooser(); 
      imageFileChooser.showOpenDialog(frame); 
      File selectedFile = imageFileChooser.getSelectedFile(); 
      if (selectedFile != null) { 
       JInternalFrame imageFrame = new JInternalFrame(
         selectedFile.getName(), true, true, true, true); 
       imageFrame.setTitle("Prikazujem " + selectedFile.getName()); 
       imageFrame.getContentPane().add(new JLabel(new 
         ImageIcon(selectedFile.getPath()))); 
       imageFrame.setSize(200, 200); 
       imageFrame.setLocation(xPosition, yPosition); 
       xPosition += 50; 
       yPosition += 50; 
       desktopPane.add(imageFrame); 
       imageFrame.setVisible(true); 
      } 

     } 
    }); 
    panel.add(btnImage); 

    JButton btnText = new JButton("Text"); 
    btnText.setIcon(new ImageIcon("/home/zmaj/Pictures/insert-text.png")); 
    btnText.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
       JFrame frame=new JFrame("Text Frame"); 
       JTextArea textArea=new JTextArea("Welcome to notes,please write your text!",10,20); 
       frame.getContentPane().add(textArea); 
       frame.setTitle("bD notes"); 
       frame.getContentPane().setLayout(new FlowLayout()); 
       frame.setSize(250,250); 
       frame.setVisible(true); 
     } 
    }); 
    panel.add(btnText); 
} 
} 

我想添加一個按鈕,將打開一個新的框架,該框架將包含一個系統控制檯。我該怎麼做?

我打開導入新庫並安裝新的eclipse插件。

+0

http://stackoverflow.com/questions/9776465/how-to-visualize-console-java-in-jframe-jpanel – arynaq 2013-05-10 14:48:07

+0

1)不要設置頂級容器的大小。而是佈置內容並調用'pack()'。 2)請參閱[使用多個JFrames,良好/錯誤的實踐?](http://stackoverflow.com/a/9554657/418556) – 2013-05-10 15:08:44

+0

http://www.javassh.com/products/terminal-components是做什麼的你要?雖然這可能有點矯枉過正。 – 2013-05-13 07:28:00

回答

0

對於Windows,你可以打開一個system console,只需撥打:

  Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe"); 
     try { 
      p.waitFor(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

當然,如果你在不同的操作系統上運行你的應用程序,這將是一個不同的呼叫。

編輯: 如果你想這個代碼是獨立的操作系統使用:

String os = System.getProperty("os.name"); 
if(os.startsWith("Windows")) { 
     // make the command for windows 
} else { 
     if(os.startsWith("Linux") { 
      // make the command for linux 
     else { 
      // make the command for mac 
     } 
} 

查找here如何打開MAC在linux系統控制檯和here

+0

thx。但是這會打開更多的問題,如何確定操作系統以及如何爲Linux或Mac執行操作? – 2013-06-21 16:47:47

+0

@MarioKamenjak檢查我的編輯 – 2013-06-22 12:50:02