为什么这个会报错,还有请问怎么样在构造函数中调用方法,这个方法是不是一定要写在构造函数里面?publicclassMainextendsJFrame{publicMain(){publicvoidsetMainPanel(intlayout,Str...
为什么这个会报错,还有请问怎么样在构造函数中调用方法, 这个方法是不是一定要写在构造函数里面?
public class Main extends JFrame{
public Main(){
public void setMainPanel(int layout, String[] labelName, String[] textName) {
switch (layout) {
case 1:
setLayout(new FlowLayout());
break;
case 2:
setLayout(new GridLayout());
break;
case 3:
setLayout(new BorderLayout());
break;
case 0:
setLayout(null);
break;
}
for (int i = 0; i < labelName.length; i++) {
JLabel jLabel = new JLabel(labelName[i]);
this.add(jLabel);
}
for(int i = 0; i < textName.length; i++) {
JTextField jTextField = new JTextField(textName[i]);
this.add(jTextField);
}
}
JPanel hint = new JPanel();
String[] a = {"dfgdf"};
String[] b = {"sdfsdfs"};
hint.setMainPanel(0,a,b);
add(hint);
JPanel nav = new JPanel();
String[] a = {"dfgdf"};
String[] b = {"sdfsdfs"};
nav.setMainPanel(0,a,b);
add(nav);
JPanel table = new JPanel();
String[] a = {"dfgdf"};
String[] b = {"sdfsdfs"};
table.setMainPanel(0,a,b);
add(table);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
展开