JPasswordField在Image之上

时间:2015-01-13 22:00:25

标签: java swing user-interface jframe jlabel

我有这个代码创建一个Jframe,我在其中添加一个带有图片和JpasswordField的JLabel。我希望JpasswordField位于图像的顶部,但仅取决于我在哪里f.add我得到其中一个显示或不显示...我希望JpasswordField位于图片的顶部以允许用户引入密码,但图片为背景。

以下是代码:

package java;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;

public class Java implements Runnable {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Java());
    }

    @Override
    public void run() {
        JFrame f = new JFrame();

        /*Keep on front*/
        f.toFront();
        f.repaint();
        f.setAlwaysOnTop(true);
        f.setExtendedState( f.getExtendedState()|JFrame.MAXIMIZED_BOTH );
        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        f.setUndecorated(true);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        f.setBounds(0,0,screenSize.width-5, screenSize.height-100);
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();

        System.out.println(width+"   "+height);


        BufferedImage myPicture = null;
        try {
            myPicture = ImageIO.read(new File("C:\\image.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Image dimg = myPicture.getScaledInstance((int)width, (int)height,Image.SCALE_SMOOTH);
        JLabel picLabel = new JLabel(new ImageIcon(dimg));

        JPasswordField myTextfield = new JPasswordField("Password");
        myTextfield.setEchoChar('*'); // U+26AB

        picLabel.setPreferredSize(screenSize);
        picLabel.setVerticalAlignment(JLabel.BOTTOM);

        /*Depending of who I add first the image or the JPasswordField is showed*/
        f.add(myTextfield);
        f.add(picLabel);

        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:4)

首先,您的代码应该适合创建JPanels,您应该直接控制此JPanel的布局管理器。然后,您可以在JDialog 而不是 JFrame中放置和显示JPanel,因为您的大部分代码似乎都在尝试模拟对话框的操作(例如将其保持在顶部等)。

令人遗憾的是,你的一个大问题是你忽略了JFrame的contentPane正在使用的布局管理器BorderLayout。通过以默认方式向其添加组件,您将掩盖先前添加到相同位置的任何内容。相反,我建议:

  • 创建JPanel
  • 在JPanel的paintComponent methd。
  • 中将您的图像绘制为背景图像
  • 将您的JLabel添加到您的JPanel。
  • 在JDialog中显示JPanel而不是JFrame。

例如:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class DialogExample extends JPanel {
   private static final int COLUMN_COUNT = 10;
   private static final int I_GAP = 3;
   public static final String BKG_IMG_PATH = "http://upload.wikimedia.org/wikipedia/commons/"
         + "thumb/9/92/Camels_in_Jordan_valley_%284568207363%29.jpg/800px-Camels_in_Jordan_valley_"
         + "%284568207363%29.jpg";

   private BufferedImage backgrndImage;
   private JTextField userNameField = new JTextField();
   private JPasswordField passwordField = new JPasswordField();
   private JPanel mainPanel = new JPanel(new GridBagLayout());
   private JButton okButton = new JButton("OK");
   private JButton cancelButton = new JButton("Cancel");

   public DialogExample(BufferedImage backgrndImage) {
      this.backgrndImage = backgrndImage;
      userNameField.setColumns(COLUMN_COUNT);
      passwordField.setColumns(COLUMN_COUNT);

      JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 5));
      btnPanel.setOpaque(false);
      btnPanel.add(okButton);
      btnPanel.add(cancelButton);

      GridBagConstraints gbc = getGbc(0, 0, GridBagConstraints.BOTH);
      mainPanel.add(createLabel("User Name", Color.white), gbc);
      gbc = getGbc(1, 0, GridBagConstraints.HORIZONTAL);
      mainPanel.add(userNameField, gbc);
      gbc = getGbc(0, 1, GridBagConstraints.BOTH);
      mainPanel.add(createLabel("Password:", Color.white), gbc);
      gbc = getGbc(1, 1, GridBagConstraints.HORIZONTAL);
      mainPanel.add(passwordField, gbc);
      gbc = getGbc(0, 2, GridBagConstraints.BOTH, 2, 1);
      mainPanel.add(btnPanel, gbc);

      mainPanel.setOpaque(false);
      add(mainPanel);
   }

   private JLabel createLabel(String text, Color color) {
      JLabel label = new JLabel(text);
      label.setForeground(color);
      return label;
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (backgrndImage != null) {
         g.drawImage(backgrndImage, 0, 0, this);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet() || backgrndImage == null) {
         return super.getPreferredSize();
      }
      int imgW = backgrndImage.getWidth();
      int imgH = backgrndImage.getHeight();
      return new Dimension(imgW, imgH);
   }

   public static GridBagConstraints getGbc(int x, int y, int fill) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.insets = new Insets(I_GAP, I_GAP, I_GAP, I_GAP);
      gbc.fill = fill;

      return gbc;
   }

   public static GridBagConstraints getGbc(int x, int y, int fill, int width,
         int height) {
      GridBagConstraints gbc = getGbc(x, y, fill);
      gbc.gridwidth = width;
      gbc.gridheight = height;

      return gbc;
   }

   private static void createAndShowGui() throws IOException {
      final JFrame frame = new JFrame("Frame");

      final JDialog dialog = new JDialog(frame, "User Sign-In", ModalityType.APPLICATION_MODAL);
      URL imgUrl = new URL(BKG_IMG_PATH);
      BufferedImage img = ImageIO.read(imgUrl);
      final DialogExample dlgExample = new DialogExample(img);
      dialog.add(dlgExample);
      dialog.pack();

      JPanel mainPanel = new JPanel();
      mainPanel.add(new JButton(new AbstractAction("Please Press Me!") {

         @Override
         public void actionPerformed(ActionEvent e) {
            dialog.setVisible(true);
         }
      }));
      mainPanel.setPreferredSize(new Dimension(800, 650));

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            try {
               createAndShowGui();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      });
   }
}

答案 1 :(得分:1)

举一个例子(在许多其他可能性中),这里有一个小片段,展示了如何处理这个问题。

顺便说一句,当你拨打f.pack(); f.setLocationRelativeTo(null);时,这个电话无用:f.setBounds(0,0,screenSize.width-5, screenSize.height-100);

import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.SwingUtilities;

public class BasicSwingTest2 {

    private JFrame frame;

    protected void initUI() throws MalformedURLException {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        askForPassword();
    }

    private void askForPassword() {
        final JDialog dialog = new JDialog(frame);
        dialog.setResizable(false);
        BufferedImage myPicture = null;
        try {
            myPicture = ImageIO
                    .read(new URL("http://images.all-free-download.com/images/graphiclarge/blue_abstract_background_310971.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Image dimg = myPicture.getScaledInstance(Toolkit.getDefaultToolkit().getScreenSize().width * 2 / 3, Toolkit.getDefaultToolkit()
                .getScreenSize().height * 2 / 3, Image.SCALE_SMOOTH);
        JLabel picLabel = new JLabel(new ImageIcon(dimg));
        picLabel.setLayout(new GridBagLayout());
        final JPasswordField password = new JPasswordField(25);
        password.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Password is " + new String(password.getPassword()));
                dialog.setVisible(false);
            }
        });
        picLabel.add(password);

        dialog.add(picLabel);
        dialog.setTitle("Enter your password");
        dialog.pack();
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new BasicSwingTest2().initUI();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}