public class debug_TekstFrame_1 extends JFrame {
private JPanel pnlName, pnlPas_1, pnlPas_2, pnlData;
//private JLabel lblMessage;
private JLabel lblMessage = null;
private JButton btnCopy;
private JTextArea areSource, areDest;
private JTextField txtName;
private Action copyAction = new DefaultEditorKit.CopyAction();
private Action pasteAction = new DefaultEditorKit.PasteAction();
private int minpasLengt=3;//paswoordlengte
private int minNLengt=1;//naamlengte min 1
private boolean blnaccess = false;//Default form_access
private boolean blntxtnaam = false;
private String strtxtnaam = null;
private String strSelectText = null;
private JPasswordField txtPas_1, txtPas_2;
public Color rgb_accept = Color.blue ;//new Color(0, 255, 255);
public Color rgb_reject = Color.red ;//new Color(255, 0, 0);
ActionListener N_actionlistener = new ActionListener(){
public void actionPerformed (ActionEvent e){
if (is_empty(txtName,txtPas_1,minNLengt)){
infoMessage("Uw naam(min " + minNLengt + " char)mag niet leeg zijn",rgb_reject);
blntxtnaam = false;
} else{
blntxtnaam = true;
strtxtnaam= txtName.getText();
infoMessage("Welkom "+strtxtnaam.toUpperCase()+" ",rgb_accept);
}
}
};
ActionListener P1_actionlistener = new ActionListener(){
public void actionPerformed(ActionEvent e){
form_Access(false);
if (!blntxtnaam){
N_actionlistener.actionPerformed(e);
}else{
if(is_empty(txtPas_1,txtPas_2,minpasLengt)){
De_bug("action paswoord1 => leeg");// DEBUG
infoMessage("Uw paswoord (min " + minpasLengt + " char) mag niet leeg zijn",rgb_reject);
}else{;
De_bug("action paswoord1 => ok");
if(!is_empty(txtPas_2,txtPas_2,minpasLengt)){
if(is_equals(txtPas_1,txtPas_2,minpasLengt)){
//De_bug("action paswoord1 == paswoord2");// DEBUG
form_Access(true);
infoMessage("Access Ok",rgb_accept);
}else{
infoMessage("Uw paswoorden zijn Niet gelijk",rgb_reject);
txtPas_1.setText("");
txtPas_2.setText("");
}
}
}
}
}
};
ActionListener P2_actionlistener = new ActionListener(){
public void actionPerformed (ActionEvent e){
if(is_empty(txtPas_2,txtPas_2,minpasLengt)){
De_bug("action paswoord2 => leeg");// DEBUG
P1_actionlistener.actionPerformed(e);
infoMessage("Uw paswoord2(min " + minpasLengt + " char)mag niet leeg zijn",rgb_reject);
}else{;
De_bug("action paswoord2 => ok");// DEBUG
P1_actionlistener.actionPerformed(e);
}
}
};
ActionListener Copy_Alistener = new ActionListener(){
public void actionPerformed (ActionEvent e){
strSelectText=areSource.getSelectedText();
if (strSelectText == null){
infoMessage("u hebt geen tekst geselecteert ",rgb_reject);
}else{
strSelectText=areSource.getSelectedText();
infoMessage("copy van:Geselecteerde tekst"+strSelectText+"naar rechts2",rgb_accept);
strSelectText=areDest.getText()+strSelectText;
areDest.setText(strSelectText);
}
}
};
public boolean is_Access(){
return blnaccess;
}
private void infoMessage(String Text,Color kleur){
lblMessage.setText(Text);
lblMessage.setForeground(kleur);
repaint();
}
private void De_bug(String Text){
System.out.println("DEBUG:TFrame_1 "+Text+"'");// DEBUG
}
public debug_TekstFrame_1(String titel) {
De_bug("constructor");// DEBUG
buildGUI();//
}
public static void main(String[] args) {
TekstFrame_1 aFrame = new TekstFrame_1("Tekstvelden met Java");
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aFrame.setLocationRelativeTo(null);
aFrame.setVisible(true);
}
private void buildGUI() {
De_bug("buildGUI");// DEBUG
FlowLayout flowRight = new FlowLayout(FlowLayout.RIGHT);
pnlName = new JPanel(flowRight);
pnlPas_1 = new JPanel(flowRight);
pnlPas_2 = new JPanel(flowRight);
pnlData = new JPanel(flowRight);
txtName = new JTextField(10);
txtPas_1 = new JPasswordField(10);
txtPas_2 = new JPasswordField(10);
txtName.setText("Lieven");
txtPas_1.setText("Roegiers");
txtPas_2.setText("Roegiers");
areSource = new JTextArea(5, 15);
areDest = new JTextArea(5, 15);
btnCopy = new JButton(">>");
lblMessage = new JLabel("berichten aan gebruiker (rood of blauw)");
lblMessage.setForeground(Color.BLUE);
txtPas_1.addActionListener(P1_actionlistener);
txtPas_2.addActionListener(P2_actionlistener);
txtName.addActionListener(N_actionlistener);
pnlName.add(new JLabel("naam: "));
pnlName.add(txtName);
pnlPas_1.add(new JLabel("paswoord: "));
pnlPas_1.add(txtPas_1);
pnlPas_2.add(new JLabel("bevestig paswoord: "));
pnlPas_2.add(txtPas_2);
form_Access(blnaccess); //Default access
btnCopy.addActionListener(Copy_Alistener);
// verzorg titel rond de 2 tekstgebieden areSource en areDest
// . . .
// areDest mag niet toegankelijk zijn voor de gebruiker (niets in te geven)
//...
pnlData.add(areSource);
pnlData.add(btnCopy);
pnlData.add(areDest);
Box content = Box.createVerticalBox();
content.add(pnlName);
content.add(pnlPas_1);
content.add(pnlPas_2);
content.add(pnlData);
content.add(lblMessage);
setContentPane(content);
pack();
}
private void form_Access(boolean access){
blnaccess = access;
areDest.setEditable (false);//mag niet bereikbaar zijn
areSource.setEnabled(blnaccess);
pnlData.setEnabled(blnaccess);
btnCopy.setEnabled(blnaccess);
//true == toegang false
pnlData.setFocusable (blnaccess);
pnlData.enableInputMethods(blnaccess);
}
private boolean is_equals(JTextField TEXT1,JTextField TEXT2,int minlengt){
String gettxt1 = TEXT1.getText().trim();
String gettxt2 = TEXT2.getText().trim();
return(gettxt2.equals(gettxt1) || TEXT1.getText().length()< minlengt);
}
private boolean is_empty(JTextField name,JTextField Focusnext,int minlengt){
String input = name.getText().trim();
if (input.isEmpty() || name.getText().length() < minlengt){
name.grabFocus();
return true;
}else{
Focusnext.grabFocus();
return false;
}
}
}