【问题标题】:Why won't the JButtons, JLabels and JTextFields be displayed?为什么不显示 JButtons、JLabels 和 JTextFields?
【发布时间】:2015-07-28 11:29:21
【问题描述】:

此代码使员工能够登录到咖啡店系统。我承认我有很多不需要的代码。我的问题是,当我运行程序时,上面只显示图像,没有 JButtons、JLabels 或 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);
   }
}

【问题讨论】:

  • 登录代码和新菜单??
  • 你的代码一团糟。建议你从头开始。首先创建只显示空框架的应用程序。查看它,直到你理解每一点代码。然后添加图像。再看一遍。然后添加一个什么都不做的按钮。重复直到完成。您的代码表明您不了解您所写的内容。我不想冒犯你,但你需要学习的不仅仅是从互联网上复制粘贴随机代码,而是了解它的作用。
  • 问题是我编写了登录代码,但是当我需要制作背景时出现了问题。有时我会感到困惑

标签: java swing


【解决方案1】:

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

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

【讨论】:

  • @lfarr 如果您创建了另一个框架但没有显示它,则在该JFrame 中添加setVisible(true)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多