为什么不显示JButtons,JLabel和JTextField?

时间:2015-07-28 11:29:22

标签: java swing

此代码使员工可以登录咖啡店系统。我承认我有很多不需要的代码。我的问题是,当我运行程序时,图像显示在上面,没有JButtons,JLabel或JTextFields。

提前致谢。

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.ImageIcon;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;

public class login extends JFrame {



public void CreateFrame() {

    JFrame frame = new JFrame("Welcome");


    JPanel panel = new JPanel();
    panel.setOpaque(true);
    panel.setBackground(Color.WHITE);
    panel.setLayout(new BorderLayout(1000,1000));
    panel.setLayout(new FlowLayout());
    getContentPane().add(panel);

    ImagePanel imagePanel = new ImagePanel();
    imagePanel.show();

    panel.add(imagePanel, BorderLayout.CENTER);

    frame.setContentPane(panel);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    frame.add(panel);



}

public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                new login().CreateFrame();
            }
        });

    } 
}

class GUI extends JFrame{

private JButton buttonLogin;
private JButton buttonNewUser;
private JLabel iUsername;
private JLabel iPassword;
private JTextField userField;
private JPasswordField passField;
public void createGUI(){

    setLayout(new GridBagLayout());

    JPanel loginPanel = new JPanel();
    loginPanel.setOpaque(false);
    loginPanel.setLayout(new GridLayout(3,3,3,3));



    iUsername = new JLabel("Username ");
    iUsername.setForeground(Color.BLACK);
    userField = new JTextField(10);
    iPassword = new JLabel("Password ");
    iPassword.setForeground(Color.BLACK);
    passField = new JPasswordField(10);
    buttonLogin = new JButton("Login");
    buttonNewUser = new JButton("New User");



    loginPanel.add(iUsername);
    loginPanel.add(iPassword);
    loginPanel.add(userField);
    loginPanel.add(passField);
    loginPanel.add(buttonLogin);
    loginPanel.add(buttonNewUser);

    add(loginPanel);

    pack();

    Writer writer = null;
    File check = new File("userPass.txt");
    if(check.exists()){

        //Checks if the file exists. will not add anything if the file does exist.
    }else{
        try{
                File texting = new File("userPass.txt");
                writer = new BufferedWriter(new FileWriter(texting));
                writer.write("message");
            }catch(IOException e){
                e.printStackTrace();
            }
    }




    buttonLogin.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                File file = new File("userPass.txt");
                Scanner scan = new Scanner(file);;
                String line = null;
                FileWriter filewrite = new FileWriter(file, true);

                String usertxt = " ";
                String passtxt = " ";
                String puname = userField.getText();
                String ppaswd = passField.getText();


                while (scan.hasNext()) {
                    usertxt = scan.nextLine();
                    passtxt = scan.nextLine();
                }




                if(puname.equals(usertxt) && ppaswd.equals(passtxt)) {
                    MainMenu menu = new MainMenu();
                    dispose();
                } 
                else if(puname.equals("") && ppaswd.equals("")){
                    JOptionPane.showMessageDialog(null,"Please insert Username and Password");
                }
                else {

                    JOptionPane.showMessageDialog(null,"Wrong Username / Password");
                    userField.setText("");
                    passField.setText("");
                    userField.requestFocus();
                }
            } catch (IOException d) {
                d.printStackTrace();
            }
        }
    });


    buttonNewUser.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            NewUser user = new NewUser();
            dispose();
        }
    });


}

}




  class ImagePanel extends JPanel{

  private BufferedImage image;

  public ImagePanel(){
      setOpaque(true);
      setBorder(BorderFactory.createLineBorder(Color.BLACK,5));
      try
      {
          image = ImageIO.read(new URL("https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQ8F5S_KK7uelpM5qdQXuaL1r09SS484R3-gLYArOp7Bom-LTYTT8Kjaiw"));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

      GUI show = new GUI();
      show.createGUI();
    }


    @Override 
    public Dimension getPreferredSize(){
        return (new Dimension(430, 300));
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(image,0,0,this);
   }
}

1 个答案:

答案 0 :(得分:0)

我觉得你有一个班级loginJFrame,但从未用作一个班级)。此登录类会在其中创建一个新的通用“欢迎”JFrame,其中包含ImagePanel。 ImagePanel调用GUI.createGUI()(它创建了另一个JFrame,但没有显示它),然后对它完全没有任何作用,因此它会立即丢失。

您的代码中有许多JFrame。一个应该足够,也许两个。但你有三个:login,gui和一个简单的新JFrame()。