私は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");
}
}
}
}