图像不会与布局管理器对齐

时间:2013-12-23 07:04:36

标签: layout jpanel jlabel jtextfield border-layout

Output 我的背景图片不会居中。我正在使用BorderLayout.CENTER。其他所有东西都在正确的位置,除了背景图像,而不是居中,是左边的一点点。有关如何解决它的任何建议?谢谢你提前。

private JTextField tf;
private JLabel jl2;

   public void window() {
       ImageIcon ic = new ImageIcon("hangman.png");
      JFrame gameFrame = new JFrame();
      JPanel jp = new JPanel();
      JPanel jpLets = new JPanel();
      JPanel jpBlank = new JPanel();
      JPanel blankLet = new JPanel();
      JPanel imgPane = new JPanel();
      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();
      blankLet.setLayout(new BoxLayout(blankLet, BoxLayout.X_AXIS));
      panel2.setLayout(new BorderLayout());
      imgPane.setLayout(new BorderLayout());
      panel1.setLayout(new BorderLayout());
      panel1.setOpaque(false);//!!
     //jp.setBorder(BorderFactory.createTitledBorder(""));
      tf = new JTextField(1);
      JLabel img = new JLabel(ic, JLabel.CENTER);
      JLabel jl = new JLabel("Enter a letter", JLabel.CENTER);
      jl2 = new JLabel("Letters used:  ", JLabel.CENTER);
      JLabel jl3 = new JLabel("__ ", JLabel.CENTER);
      jl.setFont(new Font("Rockwell", Font.PLAIN, 20));
      tf.setFont(new Font("Rockwell", Font.PLAIN, 20));
      jl2.setFont(new Font("Rockwell", Font.PLAIN, 20));
      imgPane.add(img);
      jp.add(jl);
      jp.add(tf);
      jpLets.add(jl2);
      jpBlank.add(jl3);
      blankLet.add(jpLets);
      blankLet.add(jpBlank);
      panel1.add(imgPane, BorderLayout.CENTER);
      panel1.add(jp, BorderLayout.NORTH);
      panel1.add(blankLet, BorderLayout.SOUTH);
      panel2.add(panel1, BorderLayout.CENTER);
      gameFrame.setTitle("Hangman");
      gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      gameFrame.setIconImage(
      new ImageIcon("Hangman-Game-grey.png").getImage());
      gameFrame.setResizable(false);
      gameFrame.add(panel2);
      gameFrame.setSize(600, 600);
      gameFrame.setLocationRelativeTo(null);
      gameFrame.setVisible(true);

1 个答案:

答案 0 :(得分:1)

你可以尝试两件事

  1. 从此imgPane.setLayout(new BorderLayout());
  2. 中删除BorderLayout
  3. 如果一个不起作用,请尝试将标签添加到Panel而不是imgPane

    panel1.add(img, BorderLayout.CENTER);
    
  4. 此外,panel2看起来没必要。摆脱它,只需将panel1添加到框架

  5. 尝试设置对齐

    img.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    img.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    
    panel1.add(img, BorderLayout.CENTER);
    
  6. 这是我用来重建所需结果的代码

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.border.EmptyBorder;
    
    public class HangManAlign extends JPanel {
    
        public HangManAlign() {
            JLabel guess = new JLabel("Guess");
            JTextField jtfGuess = new JTextField(3);
    
            JLabel label = new JLabel(new ImageIcon("images/Xpo9R.png"));
    
            JLabel bottom = new JLabel("Letters Used                __ __ __ __ __ __ __ __ __");
            bottom.setHorizontalAlignment(JLabel.CENTER);
    
            JPanel topPanel = new JPanel();
            topPanel.add(guess);
            topPanel.add(jtfGuess);
    
            setLayout(new BorderLayout());
            add(topPanel, BorderLayout.NORTH);
            add(label, BorderLayout.CENTER);
            add(bottom, BorderLayout.SOUTH);
            setBorder(new EmptyBorder(20, 20, 20, 20));
    
        }
    
        public static void createAndShowGui() {
            JFrame frame = new JFrame();
            frame.add(new HangManAlign());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.pack();
            frame.setVisible(true);
    
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(500, 500);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGui();
                }
            });
        }
    } 
    

    enter image description here

    我复制了整个图像并将其缩小到适当大小,这样您就可以看到它的中心位置。这是我用过的图像

    enter image description here