Avatar billede Slettet bruger
08. september 2003 - 13:24 Der er 9 kommentarer og
1 løsning

SpringLayout og Windows XP

Jeg har lavet en frame til login med SpringLayout, men hvis jeg sætter et JTextField / JPasswordField eller knapper på, så bliver framen helt tom når den kommer op på skærmen og man kan ikke lukke den på krydset.

Er der nogen der har prøvet SpringLayout på WinXP og kan hjælpe?
Avatar billede arne_v Ekspert
08. september 2003 - 13:26 #1
Hvilken version af Java ? (1.3.1/1.4.0/1.4.1/1.4.2)
Avatar billede Slettet bruger
08. september 2003 - 13:39 #2
version 1.4.2
Avatar billede _carsten Nybegynder
08. september 2003 - 13:51 #3
Nu kender jeg ikke selv det layout, men er det sådan at
du måske overrider paintComponents(g); og glemmer at kalde super.paintComponents(g); ???

Det kunne måske være tilfældet.
Avatar billede Slettet bruger
08. september 2003 - 14:43 #4
det ser ud til at mit problem ligger et andet sted.
Jeg laver en instans af Login klassen, og kalder derefter en metode der først skal returnere når der er logget ind. Framen skal så nedlægges og programmet skal køre videre i main-klassen. Nogen der kan se hvad jeg har gjort galt?

--- main-class ---
Login lgn = new Login();
lgn.authorize();
--- Login.java ---
public class Login extends JFrame implements ActionListener{
JTextField usernameField;
JPasswordField passwordField;
Hashtable users;
private static final int UNAUTHORIZED = 0;
private static final int AUTHORIZED = 1;
private int status = 0;
private JLabel statusLabel;
public static Object sandman = new Object();

public Login()
{
  super("Login");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setSize(320,240);
  //Center Frame
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  int x = (screenSize.width-getWidth()) / 2;
  int y = (screenSize.height - getHeight()) / 2;
  setLocation(x,y);
  //Set Layout
  SpringLayout layout = new SpringLayout();
  getContentPane().setLayout(layout);
  users = new Hashtable();
  users.put( "user", "password");

  //Contents
  statusLabel = new JLabel(" status ");
  getContentPane().add(statusLabel);

  JLabel usernameLabel = new JLabel("Username");
  getContentPane().add(usernameLabel);
  usernameField = new JTextField(16);
  getContentPane().add(usernameField);

  JLabel passwordLabel = new JLabel("Password");
  getContentPane().add(passwordLabel);
  passwordField = new JPasswordField(16);
  getContentPane().add(passwordField);

  JButton loginBtn = new JButton("Login");
  loginBtn.addActionListener(this);
  getContentPane().add(loginBtn);
  JButton exitBtn = new JButton("Exit");
  exitBtn.addActionListener(this);
  getContentPane().add(exitBtn);

  //Layout components
  int vPad = 10;        int hPad = 10;
  Dimension frameSize = getSize();
  SpringLayout.Constraints unlCons = layout.getConstraints(usernameLabel);
  SpringLayout.Constraints unfCons = layout.getConstraints(usernameField);
  int centerX = (frameSize.width/2);
  int centerY = (frameSize.height/2);
  int unlX = centerX - ((unlCons.getWidth().getValue() + hPad + unfCons.getWidth().getValue())/2);
  int unlY = centerY - (((3*unlCons.getHeight().getValue()) + (2*hPad)) /2);
  unlCons.setX( Spring.constant(unlX) );
  unlCons.setY( Spring.constant(unlY) );

  int unfX = unlX + unlCons.getWidth().getValue() + vPad;
  int unfY = unlY;
  unfCons.setX( Spring.constant(unfX) );
  unfCons.setY( Spring.constant(unfY) );

  SpringLayout.Constraints statusCons = layout.getConstraints(statusLabel);
  int statusX = unlX;
  int statusY = unlCons.getConstraint(SpringLayout.NORTH).getValue() - vPad - statusCons.getHeight().getValue();
  statusCons.setX(Spring.constant(statusX));
  statusCons.setY(Spring.constant(statusY));

  SpringLayout.Constraints pwlCons = layout.getConstraints(passwordLabel);
  int pwlX = unlX;
  int pwlY = unlY + unlCons.getHeight().getValue() + vPad;
  pwlCons.setX( Spring.constant(pwlX) );
  pwlCons.setY( Spring.constant(pwlY) );

  SpringLayout.Constraints pwfCons = layout.getConstraints(passwordField);
  int pwfX = unfX;
  int pwfY = pwlY;
  pwfCons.setX( Spring.constant(pwfX) );
  pwfCons.setY( Spring.constant(pwfY) );

  SpringLayout.Constraints loginCons = layout.getConstraints(loginBtn);
  int x6 = pwfX;
  int y6 = pwfY + pwfCons.getHeight().getValue() + vPad;
  loginCons.setX( Spring.constant(x6) );
  loginCons.setY( Spring.constant(y6) );

  int btnWidth = pwfCons.getWidth().getValue()/2 - hPad;
  int btnHeight = loginCons.getHeight().getValue();
  loginBtn.setPreferredSize(new Dimension(btnWidth,btnHeight));
  exitBtn.setPreferredSize(new Dimension(btnWidth,btnHeight));

  SpringLayout.Constraints exitCons = layout.getConstraints(exitBtn);
  int x7 = pwfCons.getConstraint(SpringLayout.EAST).getValue() -  exitCons.getWidth().getValue();
  int y7 = y6;
  exitCons.setX( Spring.constant(x7) );
  exitCons.setY( Spring.constant(y7) );
}

public synchronized void authorize()
{
  setVisible(true);
  try{
    synchronized(Login.sandman){
      Login.sandman.wait();
    }
  }
  catch(InterruptedException ie){Print.out(ie);}
  dispose();
}

public void actionPerformed(ActionEvent e)
{
  if ( "Login".equals(e.getActionCommand()) ){
    String un = usernameField.getText();
    char[] pw = passwordField.getPassword();
    String password = String.valueOf(password);
    if ( password.equals(users.get(un)) ){
      statusLabel.setText("AUTHORIZED");
      synchronized(Login.sandman){
        Login.sandman.notify();
      }
    }else{
      statusLabel.setText("UNAUTHORIZED");
    }
  }
}
}
Avatar billede Slettet bruger
08. september 2003 - 14:45 #5
den ser ud til at virke på win2000
Avatar billede _carsten Nybegynder
08. september 2003 - 15:28 #6
Det er din authorize() metode som blokerer, men jeg kan ikke lige se løsningen.
Avatar billede Slettet bruger
08. september 2003 - 15:35 #7
nogen anelse om hvorfor det kun sker under winXP og ikke under win2000?
Avatar billede _carsten Nybegynder
08. september 2003 - 16:52 #8
Nej - men det sker også under win98, har testet den både under XP og win98
den kan sagtens vises hvis man erstatter din authorize() metode med show();
så det må være den synkronisering som låser
Avatar billede arne_v Ekspert
08. september 2003 - 19:27 #9
1)  Jeg fik også problemet på min PC med Windows 2000.

2)  Jeg tror ikke at det er sikkert at have en synchronized metode
    i en klasse der extender JFrrame. Så vidt jeg ved bruger JFrame en
    tråd i baggrunden til at processe. Hvis den tråd også synchronizer
    på objektet - hvilket jo vil være meget logisk - så hænger det.

3)  Hvorvidt den hænger afhænger af timingen mellem main thread og
    Swing thread. Og det kan sikkert være meget "tilfældigt".

4)  Jeg kan ikke se at du behøver den synchronized overhovedet.

5)  Jeg har lavet en lille version af din kode uden den synchronized
    (og hvor jeg har rettet din sandman fra static til ikke static).
    Og det kører fint hos mig.
Avatar billede arne_v Ekspert
08. september 2003 - 19:27 #10
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class Login extends JFrame implements ActionListener {
    JTextField usernameField;
    JPasswordField passwordField;
    Hashtable users;
    private static final int UNAUTHORIZED = 0;
    private static final int AUTHORIZED = 1;
    private int status = 0;
    private JLabel statusLabel;
    public Object sandman = new Object();

    public Login() {
        super("Login");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(320, 240);
        //Center Frame
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screenSize.width - getWidth()) / 2;
        int y = (screenSize.height - getHeight()) / 2;
        setLocation(x, y);
        //Set Layout
        SpringLayout layout = new SpringLayout();
        getContentPane().setLayout(layout);
        users = new Hashtable();
        users.put("user", "password");

        //Contents
        statusLabel = new JLabel(" status ");
        getContentPane().add(statusLabel);

        JLabel usernameLabel = new JLabel("Username");
        getContentPane().add(usernameLabel);
        usernameField = new JTextField(16);
        getContentPane().add(usernameField);

        JLabel passwordLabel = new JLabel("Password");
        getContentPane().add(passwordLabel);
        passwordField = new JPasswordField(16);
        getContentPane().add(passwordField);

        JButton loginBtn = new JButton("Login");
        loginBtn.addActionListener(this);
        getContentPane().add(loginBtn);
        JButton exitBtn = new JButton("Exit");
        exitBtn.addActionListener(this);
        getContentPane().add(exitBtn);

        //Layout components
        int vPad = 10;
        int hPad = 10;
        Dimension frameSize = getSize();
        SpringLayout.Constraints unlCons = layout.getConstraints(usernameLabel);
        SpringLayout.Constraints unfCons = layout.getConstraints(usernameField);
        int centerX = (frameSize.width / 2);
        int centerY = (frameSize.height / 2);
        int unlX = centerX - ((unlCons.getWidth().getValue() + hPad + unfCons.getWidth().getValue()) / 2);
        int unlY = centerY - (((3 * unlCons.getHeight().getValue()) + (2 * hPad)) / 2);
        unlCons.setX(Spring.constant(unlX));
        unlCons.setY(Spring.constant(unlY));

        int unfX = unlX + unlCons.getWidth().getValue() + vPad;
        int unfY = unlY;
        unfCons.setX(Spring.constant(unfX));
        unfCons.setY(Spring.constant(unfY));

        SpringLayout.Constraints statusCons = layout.getConstraints(statusLabel);
        int statusX = unlX;
        int statusY = unlCons.getConstraint(SpringLayout.NORTH).getValue() - vPad - statusCons.getHeight().getValue();
        statusCons.setX(Spring.constant(statusX));
        statusCons.setY(Spring.constant(statusY));

        SpringLayout.Constraints pwlCons = layout.getConstraints(passwordLabel);
        int pwlX = unlX;
        int pwlY = unlY + unlCons.getHeight().getValue() + vPad;
        pwlCons.setX(Spring.constant(pwlX));
        pwlCons.setY(Spring.constant(pwlY));

        SpringLayout.Constraints pwfCons = layout.getConstraints(passwordField);
        int pwfX = unfX;
        int pwfY = pwlY;
        pwfCons.setX(Spring.constant(pwfX));
        pwfCons.setY(Spring.constant(pwfY));

        SpringLayout.Constraints loginCons = layout.getConstraints(loginBtn);
        int x6 = pwfX;
        int y6 = pwfY + pwfCons.getHeight().getValue() + vPad;
        loginCons.setX(Spring.constant(x6));
        loginCons.setY(Spring.constant(y6));

        int btnWidth = pwfCons.getWidth().getValue() / 2 - hPad;
        int btnHeight = loginCons.getHeight().getValue();
        loginBtn.setPreferredSize(new Dimension(btnWidth, btnHeight));
        exitBtn.setPreferredSize(new Dimension(btnWidth, btnHeight));

        SpringLayout.Constraints exitCons = layout.getConstraints(exitBtn);
        int x7 = pwfCons.getConstraint(SpringLayout.EAST).getValue() - exitCons.getWidth().getValue();
        int y7 = y6;
        exitCons.setX(Spring.constant(x7));
        exitCons.setY(Spring.constant(y7));
    }

    public void authorize() {
        setVisible(true);
        try {
            synchronized (sandman) {
                sandman.wait();
            }
        } catch (InterruptedException ie) {
            System.out.println(ie);
        }
        dispose();
    }

    public void actionPerformed(ActionEvent e) {
        if ("Login".equals(e.getActionCommand())) {
            String un = usernameField.getText();
            char[] pw = passwordField.getPassword();
            String password = String.valueOf(pw);
            if (password.equals(users.get(un))) {
                statusLabel.setText("AUTHORIZED");
                synchronized (sandman) {
                    sandman.notify();
                }
            } else {
                statusLabel.setText("UNAUTHORIZED");
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Login lgn = new Login();
        lgn.authorize();
    }
}
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester

IT-JOB

Netcompany A/S

Linux Operations Engineer

BEC Financial Technologies

Java software engineer (regular)

Forsvarsministeriets Materiel- og Indkøbsstyrelse

Driftstærk IT-profil til Forsvarsministeriets Materiel- og Indkøbsstyrelse

Digitaliseringsstyrelsen

Systemforvalter til borger.dk

Operate Technology A/S

PHP-udvikler til Drupal