Виктор Кон, vkBook, Java-Second
Класс intForm
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class inpForm extends JDialog implements ActionListener {
Container pcont; Box hbl,hbi,hbox,vbox,vbi;
String[] tt; String lt; JTextField[] inpf; JLabel[] labf; boolean ok;
JButton b1,b2; int j,k,l,m,n,n1,n2,ns1,ns2,ivs,ifs,nsx,nsy;
public inpForm(Frame own, String tit, String[] lab, String[] txt, int nf, int ns, int mod){
super(own); setTitle(tit); setResizable(false); setModal(true);
ok=true; n=nf; ifs=20; m=mod;
if( n > 0 ){
tt = new String[n]; inpf = new JTextField[n]; labf = new JLabel[n];
for( k=0; k < n; k++ ){ tt[k]=txt[k]; inpf[k] = new JTextField(tt[k]); lt=lab[k]; labf[k] = new JLabel(lt); }
if(m*(m-1) == 0){ns1=ns; ns2=ns; ivs=10;}
if((m-2)*(m-3) == 0){ns1=5; ns2=2*ns-5; ivs=2;}
if((m-4)*(m-5) == 0){ns1=2*ns-5; ns2=5; ivs=2;}
if( m > 5 ){ns1=ns; ns2=ns; ivs=2;}
hbox = Box.createHorizontalBox(); hbox.add(Box.createHorizontalStrut(ns1));
b1 = new JButton("OK"); b1.setActionCommand("ok"); b1.addActionListener(this);
b2 = new JButton("Cancel"); b2.setActionCommand("ca"); b2.addActionListener(this);
hbox.add(b1); hbox.add(Box.createHorizontalStrut(5)); hbox.add(b2); hbox.add(Box.createHorizontalStrut(ns2));
vbox = Box.createVerticalBox();
if( m < 6 ){
for( k=0; k < n; k++ ){
if(mod < 2){hbl=Box.createHorizontalBox(); hbl.add(labf[k]); vbox.add(hbl); vbox.add(inpf[k]);}
else{hbl=Box.createHorizontalBox(); hbl.add(labf[k]); hbl.add(inpf[k]); vbox.add(hbl);}
vbox.add(Box.createVerticalStrut(ivs));
}}
else
if( m < 8 ){ n2=n/2; hbi=Box.createHorizontalBox(); vbi = Box.createVerticalBox();
for( k=0; k < n2; k++ ){hbl=Box.createHorizontalBox(); hbl.add(labf[k]); hbl.add(inpf[k]);
vbi.add(hbl); vbi.add(Box.createVerticalStrut(ivs));
} hbi.add(vbi); vbi = Box.createVerticalBox();
for( k=n2; k < n; k++ ){hbl=Box.createHorizontalBox(); hbl.add(labf[k]); hbl.add(inpf[k]);
vbi.add(hbl); vbi.add(Box.createVerticalStrut(ivs));
} hbi.add(Box.createHorizontalStrut(10)); hbi.add(vbi); vbox.add(hbi);
}
else
if( m < 10 ){ n1=n/3; n2=2*n/3; hbi=Box.createHorizontalBox(); vbi = Box.createVerticalBox();
for( k=0; k < n1; k++ ){hbl=Box.createHorizontalBox(); hbl.add(labf[k]); hbl.add(inpf[k]);
vbi.add(hbl); vbi.add(Box.createVerticalStrut(ivs));
} hbi.add(vbi); vbi = Box.createVerticalBox();
for( k=n1; k < n2; k++ ){hbl=Box.createHorizontalBox(); hbl.add(labf[k]); hbl.add(inpf[k]);
vbi.add(hbl); vbi.add(Box.createVerticalStrut(ivs));
} hbi.add(Box.createHorizontalStrut(10)); hbi.add(vbi); vbi = Box.createVerticalBox();
for( k=n2; k < n; k++ ){hbl=Box.createHorizontalBox(); hbl.add(labf[k]); hbl.add(inpf[k]);
vbi.add(hbl); vbi.add(Box.createVerticalStrut(ivs));
} hbi.add(Box.createHorizontalStrut(10)); hbi.add(vbi); vbox.add(hbi);
}else{
n1=m/2-1; n2=n/n1;
for( l=0; l < n2; l++ ){ hbl=Box.createHorizontalBox();
for( k=0; k < n1; k++ ){ j=l+k*n2;
hbl.add(Box.createHorizontalStrut(5)); hbl.add(labf[j]); hbl.add(inpf[j]); hbl.add(Box.createHorizontalStrut(5));
} hbl.validate(); vbox.add(hbl); vbox.add(Box.createVerticalStrut(ivs));
}}
vbox.add(Box.createVerticalStrut(2*ivs)); vbox.add(hbox);
pcont = getContentPane();
pcont.add(Box.createVerticalStrut(ifs), BorderLayout.NORTH);
pcont.add(Box.createHorizontalStrut(ifs), BorderLayout.WEST);
pcont.add(vbox, BorderLayout.CENTER);
pcont.add(Box.createHorizontalStrut(ifs), BorderLayout.EAST);
pcont.add(Box.createVerticalStrut(ifs), BorderLayout.SOUTH);
pack(); setLocationRelativeTo(null);
}
}
public void actionPerformed(ActionEvent e) {
if( "ok".equals(e.getActionCommand()) ){
if(n > 0){for(k=0; k < n; k++){tt[k]=inpf[k].getText();}}
MyPro.tt=tt; MyPro.ok=true; this.setVisible(false);
}
if( "ca".equals(e.getActionCommand()) ){MyPro.tt=tt; MyPro.ok=false; this.setVisible(false);
}
}
}