1

我想让多个文本文件按自定义顺序对齐,以便它们位于背景图像的顶部。我尝试使用setBounds但没有用:

import javax.swing.*;


    public class TestJP {


        public static void main(String[] args) {

                  JLabel myLabel = new JLabel();
                  myLabel.setIcon ( new ImageIcon("system\\myBackground.jpg"));
                  myLabel.setBounds(0,0,750,500);


                  JTextField login = new JTextField(5);
                  login.setBounds(50,50,20,100); // This does not work

                  JPasswordField password = new JPasswordField(5);
                  password.setBounds( 50, 70, 20, 100); // Doesn't help either

                  JPanel myPanel = new JPanel();
                  myPanel.add(myLabel);
                  myPanel.add(login);
                  myPanel.add(password);

                   int result = JOptionPane.showConfirmDialog(null, myPanel, 
               "Please Login", JOptionPane.OK_CANCEL_OPTION);

                 // etc

               }

        }
4

2 回答 2

4

不要使用setBounds(). Swing 旨在与布局管理器一起使用。

您可以通过执行以下操作将文本字段添加到标签:

JLabel myLabel = new JLabel( new ImageIcon("system\\myBackground.jpg") );
mylabel.setLayout( new FlowLayout() );
mylabel.add(login);
mylabel.add(password);

为标签使用适当的布局管理器以获得所需的布局。

于 2013-07-26T02:55:40.050 回答
4

“我尝试使用 setBounds 但它不起作用:”

  • 使用布局管理器,因为这就是它们的用途。您可以在此处找到布局管理器教程。
  • 将布局添加到显示图像的 JLabel。
  • 将您的 JTextFields 添加到该 JLabel。
  • 在您的 JOptionPane 中显示 JLabel。

如需更多帮助,请发布您正在尝试执行的操作的图片。


编辑您在评论中声明:

谢谢你!我在 contentPane 中多次使用 setBounds ...效果很好。

作为一个新手,你会发现 setBounds 和 null 布局看起来更容易使用,你用 swing 编码的时间越长,你的 GUI 越多,你就会发现 null 布局会失败,在不同的平台上看起来很糟糕,将是不灵活的,并且不允许您以后更新或改进您的 GUI。

为什么它不适用于 JOptionPane?常规布局管理器不精确......我不想使用那些:东,西

它与 JOptionPane 无关,而与您未正确创建 GUI 的方式有关。学习使用布局管理器,它们会更容易使用。


编辑 2例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.beans.Transient;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

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

public class GridBagEg {
   public static final String IMG_PATH = "https://duke.kenai.com/tshirts/.Midsize/Tshirt1997.png.png";

   private static void createAndShowGui() {
      BufferedImage img = null;
      try {
         URL imgUrl = new URL(IMG_PATH);
         img = ImageIO.read(imgUrl);
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }
      PlayerEditorPanel playerEditorPane = new PlayerEditorPanel(img);

      int result = JOptionPane.showConfirmDialog(null, playerEditorPane,
            "Edit Player", JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE);
      if (result == JOptionPane.OK_OPTION) {
         // TODO: do something with info

         for (PlayerEditorPanel.FieldTitle fieldTitle : 
            PlayerEditorPanel.FieldTitle.values()) {
            System.out.printf("%10s: %s%n", fieldTitle.getTitle(),
                  playerEditorPane.getFieldText(fieldTitle));
         }
      }
   }

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

@SuppressWarnings("serial")
class PlayerEditorPanel extends JPanel {
   enum FieldTitle {
      NAME("Name"), SPEED("Speed"), STRENGTH("Strength");
      private String title;

      private FieldTitle(String title) {
         this.title = title;
      }

      public String getTitle() {
         return title;
      }
   };

   private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
   private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
   private static final double SCALE = 0.4;
   private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>();
   private BufferedImage backgroundImg = null;
   private int imgWidth;
   private int imgHeight;

   public PlayerEditorPanel(BufferedImage img) {
      this.backgroundImg = img;
      imgWidth = (int) (backgroundImg.getWidth() * SCALE);
      imgHeight = (int) (backgroundImg.getHeight() * SCALE);

      setLayout(new GridBagLayout());
      setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Player Editor"),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));
      GridBagConstraints gbc;
      for (int i = 0; i < FieldTitle.values().length; i++) {
         FieldTitle fieldTitle = FieldTitle.values()[i];
         gbc = createGbc(0, i);
         JLabel fieldLabel = new JLabel(fieldTitle.getTitle() + ":",
               JLabel.LEFT);
         fieldLabel.setForeground(new Color(200, 10, 10));
         fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD, 24f));
         add(fieldLabel, gbc);
         gbc = createGbc(1, i);
         JTextField textField = new JTextField(10);
         add(textField, gbc);

         fieldMap.put(fieldTitle, textField);
      }
   }

   @Override
   @Transient
   public Dimension getPreferredSize() {
      if (backgroundImg != null) {
         return new Dimension(imgWidth, imgHeight);
      }

      return super.getPreferredSize();
   }

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

   private GridBagConstraints createGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;

      gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
      gbc.fill = (x == 0) ? GridBagConstraints.BOTH
            : GridBagConstraints.HORIZONTAL;

      gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
      gbc.weightx = (x == 0) ? 0.1 : 1.0;
      gbc.weighty = 1.0;
      return gbc;
   }

   public String getFieldText(FieldTitle fieldTitle) {
      return fieldMap.get(fieldTitle).getText();
   }

}

它显示一个 JOptionPane 像这样:

在此处输入图像描述

于 2013-07-26T02:56:01.503 回答