4

我正在为我的程序创建一个 about JFrame。我有一个用于该程序的图标,我将它显示为关于 JFrame 的第一件事,但我在尝试使图像居中时遇到问题。如果我进行某种居中,它会破坏其他所有内容的整体对齐。

我试图让除图标之外的所有 JLabels 左对齐。然后让图标与中心对齐。

我不得不删除一些个人信息,无论我删除了什么,我都将它们放在“[]”之间。

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class About extends JFrame {

    public About() {
        super("About [PROGRAM]");
        setIconImage([PROGRAM].getInstance().setIcon());

        JPanel main = new JPanel();

        main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
        main.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        JLabel icon = new JLabel("", new ImageIcon(getClass().getResource(Constants.ICON_FULL)), JLabel.CENTER);        
        JLabel name = new JLabel("[PROGRAM]");
        JLabel expandedName = new JLabel("[PROGRAM DESCRIPTION]");
        JLabel copyright = new JLabel("[COPYRIGHT JUNK]");
        JLabel credits = new JLabel("[CREDITS]");

        name.setFont(new Font(name.getFont().getFamily(), Font.BOLD, 18));

        copyright.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));

        main.add(icon);
        main.add(Box.createRigidArea(new Dimension(0, 10)));
        main.add(name);
        main.add(expandedName);
        main.add(copyright);
        main.add(credits);

        add(main);

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

}
4

1 回答 1

3

考虑使用一些布局来帮助您。想到的包括在 BorderLayout.CENTER 位置带有图标的 BorderLayout。您可以使用使用 JPanel 的 BoxLayout 在一侧堆叠东西,该 JPanel 添加到使用 JPanel 的主 BorderLayout。

例如,

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

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

@SuppressWarnings("serial")
public class About extends JDialog {
   public static final String IMAGE_PATH = "http://upload.wikimedia.org/wikipedia/"
         + "commons/thumb/3/39/European_Common_Frog_Rana_temporaria.jpg/"
         + "800px-European_Common_Frog_Rana_temporaria.jpg";

   public About(JFrame frame) {
      super(frame, "About [PROGRAM]", true);

      ImageIcon myIcon = null;
      try {
         URL imgUrl = new URL(IMAGE_PATH);
         BufferedImage img = ImageIO.read(imgUrl);
         myIcon = new ImageIcon(img);
      } catch (MalformedURLException e) {
         e.printStackTrace();
         System.exit(-1);
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }

      JPanel main = new JPanel(new BorderLayout());

      main.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

      JLabel centerLabel = new JLabel(myIcon);
      JLabel name = new JLabel("[PROGRAM]");
      JLabel expandedName = new JLabel("[PROGRAM DESCRIPTION]");
      JLabel copyright = new JLabel("[COPYRIGHT JUNK]");
      JLabel credits = new JLabel("[CREDITS]");

      name.setFont(new Font(name.getFont().getFamily(), Font.BOLD, 18));

      copyright.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));

      int eb = 20;
      centerLabel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));

      JPanel leftPanel = new JPanel();
      leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
      leftPanel.add(name);
      leftPanel.add(Box.createVerticalGlue());
      leftPanel.add(expandedName);
      leftPanel.add(copyright);
      leftPanel.add(credits);
      leftPanel.add(Box.createVerticalGlue());

      main.add(centerLabel, BorderLayout.CENTER);
      main.add(leftPanel, BorderLayout.LINE_START);

      add(main);

      pack();
   }

   public static void main(String[] args) {
      final JFrame frame = new JFrame("GUI");
      JPanel panel = new JPanel();
      panel.add(new JButton(new AbstractAction("About") {

         @Override
         public void actionPerformed(ActionEvent e) {
            About about = new About(frame);
            about.setLocationRelativeTo(frame);
            about.setVisible(true);
         }
      }));
      frame.add(panel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}
于 2012-06-27T00:27:06.253 回答