0

Loginを使用してプログラムのフォームを作成しましたJFrame。すべてのテキスト ラベル、入力フィールド、ボタン、その他の GUI 要素が表示されますが、何らかの理由で画像ファイルが表示されません (画像"mm.png"はプロジェクトの親ディレクトリに保存されています)。

私は何か間違ったことをしているに違いない。おそらく誰かが私を助けることができます。

私のコードは以下です。

どうもありがとう。

import java.io.*;
import java.net.*;
import java.awt.*;//contains layouts, buttons etc.
import java.awt.event.*; //contains actionListener, mouseListener etc.

import javax.swing.*; //allows GUI elements

public class Login extends JFrame implements ActionListener, KeyListener {

    private JLabel usernameLabel = new JLabel("Username/Email:");
    private JLabel userPasswordLabel = new JLabel("Password:");
    public JTextField usernameField = new JTextField();
    private JPasswordField userPasswordField = new JPasswordField();
    private JLabel status = new JLabel("Status: Not yet logged in.");

    private JButton loginButton = new JButton("Login");
    private JButton registerButton = new JButton("New User");

    public Login() {
        super("Please Enter Your Login Details...");// titlebar
        setVisible(true);
        setSize(400, 260);
        this.setLocationRelativeTo(null); // places frame in center of screen
        this.setResizable(false); // disables resizing of frame
        this.setLayout(null); // allows me to manually define layout of text
                                // fields etc.

        ImageIcon icon = new ImageIcon("mm.png");
        JLabel label = new JLabel(icon);

        this.add(usernameLabel);
        this.add(userPasswordLabel);
        this.add(usernameField);
        this.add(userPasswordField);
        this.add(loginButton);
        this.add(registerButton);
        this.add(status);

        usernameLabel.setBounds(30, 100, 120, 30); // (10, 60, 120, 20);
        userPasswordLabel.setBounds(30, 125, 80, 30);// (10, 85, 80, 20);
        usernameField.setBounds(150, 100, 220, 30);
        userPasswordField.setBounds(150, 125, 220, 30);
        loginButton.setBounds(150, 180, 110, 25);
        registerButton.setBounds(260, 180, 110, 25);
        status.setBounds(30, 210, 280, 30);
        status.setForeground(new Color(50, 0, 255)); // sets text colour to blue

        loginButton.addActionListener(this);
        registerButton.addActionListener(this);
        registerButton.setEnabled(false);
        userPasswordField.addKeyListener(this);

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == loginButton) {
            String userName = usernameField.getText();
            String password = userPasswordField.getText();
            if (userName.equals("mick") && password.equals("mick")) {
                status.setText("Status: Logged in.");
                this.setVisible(false);
                new Client("127.0.0.1").startRunning();
            } else {
                status.setText("Status: Password or username is incorrect.");
                status.setForeground(new Color(255, 0, 0)); // changes text
                                                            // colour to red
            }
        }

    }

}
4

4 に答える 4

3
  1. nullレイアウトを使用しないでください。Swing は、レイアウト マネージャーと連携するように設計されています。ピクセル パーフェクトなレイアウトは、現代の GUI 設計では幻想です。フォントの可用性や個々のシステムでのレンダリング方法を制御することはできません。レイアウト アウト マネージャーは、コンポーネントがどのように連携するかの関係を判断する際に当て推量を排除します。
  2. setVisibleフレームのコンテンツの作成が完了した後にのみ呼び出してください。フレームが表示された後にコンポーネントを追加/削除する必要がある場合は、呼び出す必要がありますrevalidate

詳細については、コンテナ内のコンポーネントのレイアウトをご覧ください。

于 2014-04-07T20:29:17.047 に答える
1
    ImageIcon icon = new ImageIcon("mm.png");
    JLabel label = new JLabel(icon);

しかし、何らかの理由で画像ファイルが表示されません

Icon と JLabel を作成しますが、ラベルを GUI に追加する場所がわかりません。

于 2014-04-07T20:49:07.613 に答える