2014-11-21 112 views
0

我正在Swing中開發我的新應用程序,並且希望在此應用程序中重用JChempaint。我有JChempaint applet的jar文件(使用JApplet在Swing中開發)。將JApplet添加到JPanel

基本上,我想jar文件在我的新應用程序添加到JPanel。無論如何,這可能嗎? JChempaint是開源的,我也有源代碼。

如何將JChempaint小程序添加到面板?


以下是試圖實現的建議------ 我開始與我的項目,並試圖建立一個骨架嵌入JChemPaint窗口之後的細節。 以下是我的佈局代碼:

package LearnSwingPkg; 

import java.awt.BorderLayout; 

class SplitPane extends JFrame { 

private JPanel panel1; 
private JPanel panel2; 
private JScrollPane panel3; 
private JScrollPane panel4; 

protected JSplitPane split; 

public SplitPane(){ 

    super("Learn Swing"); 
    JFrame.setDefaultLookAndFeelDecorated(true); 
    setSize(900, 700); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setLocation(0,0); 

    setTitle("Split Pane Application"); 

    JPanel topPanel = new JPanel(); 
    topPanel.setLayout(new BorderLayout()); 
    getContentPane().add(topPanel); 

    // Create the panels 
    createPanel1(); 
    createPanel2(); 
    createPanel3(); 
    createPanel4(); 

    JSplitPane spLeft = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true,panel1, panel3); 
    JSplitPane spRight = new JSplitPane(JSplitPane.VERTICAL_SPLIT,true, panel2, panel4); 

    split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,spLeft, spRight); 
    split.setOneTouchExpandable(true); 

    getContentPane().add(split, BorderLayout.CENTER); 



} 
//top left 
public void createPanel1(){ 
    panel1 = new JPanel(); 
    panel1.setLayout(new BorderLayout()); 
    panel1.add((new TextArea("Panel1"))); 

    panel1.setPreferredSize(new Dimension(450, 400)); 
    panel1.setMaximumSize(new Dimension(450, 400)); 
} 


//top right 
public void createPanel2(){ 
    panel2 = new JPanel(); 
    panel2.setLayout(new BorderLayout()); 
    panel2.add((new TextArea("Panel2"))); 
    panel2.setPreferredSize(new Dimension(450, 400)); 
    panel2.setMaximumSize(new Dimension(450, 400)); 

} 

//bottom left 
public void createPanel3(){ 
    Label label_prop = new Label("Properties:", Label.LEFT); 

    String[] columnNames = {"Properties", 
      "", 
      }; 
    Object[][] data = { 
      {"", "",}, {"", ""}, {"", ""},{"", ""}, 
      {"", "",}, {"", ""}, {"", ""},{"", ""}, 
      {"", "",}, {"", ""}, {"", ""},{"", ""} 
      }; 


    JTable table = new JTable(data, columnNames); 
    table.setBackground(getBackground()); 
    table.setBackground(Color.LIGHT_GRAY); 
    table.setRowHeight(20); 
    table.setBorder(BasicBorders.getMenuBarBorder()); 

    panel3 = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZO 
    panel3.add(label_prop); 
    panel3.setPreferredSize(new Dimension(20, 20)); 
    panel3.setMinimumSize(new Dimension(20, 20)); 

} 
//bottom right 
public void createPanel4(){ 

    panel4 = new JScrollPane(); 
     //panel4.setLayout(new FlowLayout()); 
    String[] columnNames = {"Activities", 
       "", 
       }; 
     Object[][] data = { 
        {"", "",}, {"", ""}, {"", ""},{"", ""}, 
        {"", "",}, {"", ""}, {"", ""},{"", ""}, 
        {"", "",}, {"", ""}, {"", ""},{"", ""} 
        }; 


     JTable table = new JTable(data, columnNames); 
     table.setBackground(getBackground()); 
     table.setBackground(Color.LIGHT_GRAY); 
     table.setRowHeight(20); 
     table.setBorder(BasicBorders.getMenuBarBorder()); 
     panel4 = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    panel4.setPreferredSize(new Dimension(20, 20)); 
    panel4.setMinimumSize(new Dimension(20, 20)); 


} 

public static void main(String args[]){ 
    try { 
     UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
    } catch (Exception evt) {} 
    // Create an instance of the test application 
    SplitPane mainFrame = new SplitPane(); 
    mainFrame.setVisible(true); 
    mainFrame.setBackground(Color.blue); 
    } 
} 

對於暫時,我試圖插入一個空表,在上面的代碼。稍後,它將填入相關數據。

這讓我有四個街區的框架,左上角會有JCHemPaint窗口,下兩個街區就會有一個表。

現在,爲了在面板1添加JChemPaint我編輯在此file.I代碼改變了方法createPanel1:

//top left 
public void createPanel1(){ 
    panel1 = new JPanel(); 
    panel1.setLayout(new BorderLayout()); 
    JChemPaint.showInstance(filename, null, null, debug); 
    panel1.setPreferredSize(new Dimension(450, 400)); 
    panel1.setMaximumSize(new Dimension(450, 400)); 
} 

此輸出我只JChemPaint窗口。

我無法將它放入面板1如果我的框架。 我該怎麼做? 謝謝!

+4

「jar」是一個包含Java類和資源的zip文件,它不是Swing組件,因此不能這樣處理。因此,對你的直接問題的簡短回答,''我可以添加一個Jar到JPanel嗎?「是不,你不能。但是,如果jar文件包含從Swing組件(例如JPanel或JComponent)擴展的Swing類,並且文檔或源代碼將能夠告訴您這一點,那麼可以,您可以在您自己的Swing GUI中使用這些類的對象。 – 2014-11-21 00:06:21

+1

非常感謝。這一定會幫助我。 – ksk 2014-11-21 18:45:28

回答

3

至於建議hereJChemPaint是一個標準的Java應用程序。有關構建JChemPaintPanel並將其添加到JFrame的示例,請參見showInstance()

+0

謝謝@trashgod。我會研究這個例子,如果我有任何問題,請回復你。 – ksk 2014-11-21 18:48:21

+0

它看起來像一個混合applet /應用程序;一些相關的例子被引用[這裏](http://stackoverflow.com/a/12449949/230513)。 – trashgod 2014-11-22 11:54:14