我正在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如果我的框架。 我该怎么做? 谢谢!
“jar”是一个包含Java类和资源的zip文件,它不是Swing组件,因此不能这样处理。因此,对你的直接问题的简短回答,''我可以添加一个Jar到JPanel吗?“是不,你不能。但是,如果jar文件包含从Swing组件(例如JPanel或JComponent)扩展的Swing类,并且文档或源代码将能够告诉您这一点,那么可以,您可以在您自己的Swing GUI中使用这些类的对象。 – 2014-11-21 00:06:21
非常感谢。这一定会帮助我。 – ksk 2014-11-21 18:45:28