0

私はJavaインターフェイスを作成しようとするのが初めてで、大学のプロジェクトの一環として作成したいと考えています。

現時点では、まだ開始インターフェイスに取り組んでいますが、背景画像をフレームに設定することはできません。見つけることができるすべてのYouTubeビデオを見て、すべてのフォーラムを調べましたが、まだ何も機能していないようです.

私が見たすべての例には、ボタンとテキストボックスが既に配置されていないため、これが問題であるかどうかはわかりませんが、「試してキャッチ」では、画像を配置したにもかかわらず、常に「画像が存在しません」と表示されますの正しいファイル名。

私が言ったように、私はインターフェースを扱うのが初めてなので、それが本当に単純であるか、本当にすべてを台無しにしていないことを知っていますが、誰かが助けることができれば、それは本当に高く評価されます.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.*;

public class CoopWelcome extends JFrame {

    private ImageIcon image1;
    private JButton b1;
    private JTextField userName;
    private static String password = "";
    private JPanel backGround;

    CoopWelcome() {
        setLayout(new FlowLayout());
        //creating username textbox
        userName = new JTextField("");
        userName.setText("Username");
        userName.setForeground(Color.GRAY);
        userName.setColumns(10);
        getContentPane().add(userName);
        //creating password textbox
        JPasswordField passWord = new JPasswordField(10);
        passWord.setEchoChar('*');
        passWord.addActionListener(new AL());
        getContentPane().add(passWord);
        //adding the button and the label to the panel
        b1 = new JButton("something");
        getContentPane().add(b1);
        //getting the image and displaying to the label
    }

    public static void main(String[] Args) {
        //Creating the interface
        CoopWelcome gui = new CoopWelcome();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.pack();
        gui.setTitle("The Co-operative");
        try {
            gui.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg")))));

        } catch (IOException e) {
            System.out.println("image doesn't exist");
        }
    }

    static class AL implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            JPasswordField input = (JPasswordField) e.getSource();
            char[] passy = input.getPassword();
            String p = new String(passy);
            if (p.equals(password)) {
                JOptionPane.showMessageDialog(null, "Correct");

            } else {
                JOptionPane.showMessageDialog(null, "Incorrect");
            }
        }
    }
}
4

3 に答える 3

3

gui.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg")))));

合理的なアプローチですが、問題は、デフォルトでは JLabel がレイアウトマネージャーを使用しないため、コンポーネントを簡単に追加できないことです。試す:

JLabel background = new JLabel(...);
background.setLayout( new BorderLayout() );
gui.setContentPane( background );
于 2013-10-26T04:31:28.413 に答える
0

問題は解決しました。

私は基本的に JFrame とラベルなどを完全に作り直し、なんとか機能させました。おそらくまだひどくフォーマットされていますが、少なくとも今は機能しており、将来、どこが間違っているのかを知るのが非常に簡単になりました.

    CoopWelcome() {

setLayout (new FlowLayout());

//username field

userName = new JTextField("");
userName.setText("Username");
userName.setForeground(Color.GRAY);
userName.setColumns(10);
getContentPane().add(userName);

    //password field

JPasswordField passWord = new JPasswordField(10);
passWord.setEchoChar('*');
passWord.addActionListener(new AL());
getContentPane().add(passWord);

//button

b1 = new JButton("something");
getContentPane().add(b1);


//frame

setSize(500,600);
    setVisible(true); setLayout(new BorderLayout());
    JLabel background=new JLabel(new ImageIcon("img.jpg"));
    add(background);
background.setLayout(new FlowLayout());

}

public static void main(String[] Args){

    new CoopWelcome();

}
于 2013-10-27T03:17:54.953 に答える
-1
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setContentPane(new JLabel(new ImageIcon("someImage.png")));
f.setSize(300,300);
f.setSize(301,301); //just a refresh
于 2013-10-26T14:13:50.333 に答える