我想将JPanel创建为JFrame
的一部分private void initialize() {
frame = new JFrame();
frame.getContentPane().add((new JPanelSql("testName" , "testTxt")) , "cell 1 2,grow");
}
class JPanelSql extends JPanel {
private String name ;
private String txt ;
public JPanelSql (String name , String txt ) {
this.name = name ;
this.txt = txt ;
}
public void createJPanel () {
JLabel lblNewLabel = new JLabel(name);
super.add(lblNewLabel);
JTextArea textArea = new JTextArea();
textArea.append(txt) ;
super.add(textArea);
}
}
但是,这不会在JFrame中创建任何JPanel。
如何动态创建JPanel?
答案 0 :(得分:1)
尝试在构造函数中添加createJPanel
...
public JPanelSql (String name , String txt ) {
this.name = name ;
this.txt = txt ;
createJPanel();
}
这将创建组件并将其添加到面板......