1

我一直在编写和重构我开始为 Hangman 游戏编写的代码。我现在基本上有两个班。一个是启动hangman(包含main),另一个是让面板插入各种组件。总的来说,我将拥有一个框架和一个面板、几个按钮以及一个标签和文本字段。我在下面粘贴了我的两段代码。我想把所有的东西都推到框架上,中间的标签和文本字段,我需要顶部的空白空间来放置更多的东西。我试过 BorderLayout.SOUTH,但没有帮助。边框标题到处都是,但我希望它只围绕单选按钮。请运行代码以了解我的意思。感谢 Code1:使用 Main 方法

import javax.swing.*;
import javax.swing.text.MaskFormatter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.text.ParseException;

public class HangmanGUI {

    DetailsPanel myPanel;

    public HangmanGUI() throws ParseException {

        myPanel = new DetailsPanel();
        JFrame myframe = new JFrame();
        myframe.getContentPane().setLayout(new BorderLayout());
        myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
        myframe.setTitle("Hangman Game");
        myframe.setVisible(true);
        myframe.setLocationRelativeTo(null);
        myframe.pack();
        myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws ParseException {
        new HangmanGUI();
    }
}

代码 2:带面板和组件

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;

public class DetailsPanel extends JPanel {

    public DetailsPanel() {

        setPreferredSize(new Dimension(400, 600));

        setBorder(BorderFactory.createTitledBorder(" ciick here "));

        createFormattedPanel();

        for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
            String buttonText = String.valueOf(alphabet);
            JButton letterButton = new JButton(buttonText);
            letterButton.addActionListener(clickedbutton());
            this.add(letterButton, BorderLayout.CENTER);
        }

    }

    private ActionListener clickedbutton() {
        return new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String actionCommand = e.getActionCommand();
                System.out.println("actionCommand is: " + actionCommand);
            }
        };
    }

    public void createFormattedPanel() {
        MaskFormatter formatter = null;
        try {
            JLabel label = new JLabel("Guesss");
            formatter = new MaskFormatter("? ? ? ? ? ? ?");
            formatter.setPlaceholderCharacter('?');
            JFormattedTextField input = new JFormattedTextField(formatter);
            input.setColumns(20);
            this.add(label);
            this.add(input);
        } catch (java.text.ParseException exc) {
            System.err.println("formatter is bad: " + exc.getMessage());
            System.exit(-1);
        }
    }
}
4

3 回答 3

2

“如果”我理解正确,为什么不将按钮添加到自己的面板并在其上设置标题边框,例如...

public DetailsPanel() {

    setPreferredSize(new Dimension(400, 600));

    createFormattedPanel();

    JPanel buttons = new JPanel();
    buttons.setBorder(BorderFactory.createTitledBorder(" ciick here "));

    for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
        String buttonText = String.valueOf(alphabet);
        JButton letterButton = new JButton(buttonText);
        letterButton.addActionListener(clickedbutton());
        buttons.add(letterButton, BorderLayout.CENTER);
    }

    add(buttons);

}
于 2013-10-17T23:02:53.683 回答
2

我会使用图像 JPanel 来保存您的刽子手绘图,并充当占位符并将其放置在 BorderLayout.CENTER 位置的 JFrame 中。我还将通过使用布局管理器而不是依赖默认的 FlowLayout 来清理南 JPanel。比如南Jpanel的BorderLayout,将猜测放在页面开始槽中,将按钮放在中心槽中,然后将按钮放入GridLayout JPanel中。例如:

import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.text.ParseException;

public class HangmanGUI {
   private DetailsPanel myPanel;
   private ImagePanel imagePanel = new ImagePanel();

   public HangmanGUI() throws ParseException {
      myPanel = new DetailsPanel();
      JFrame myframe = new JFrame();
      // myframe.getContentPane().setLayout(new BorderLayout());
      myframe.getContentPane().add(imagePanel, BorderLayout.CENTER);
      myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
      myframe.setTitle("Hangman Game");
      // myframe.setVisible(true);
      // myframe.setLocationRelativeTo(null);
      myframe.pack();
      myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      myframe.setLocationRelativeTo(null);
      myframe.setVisible(true);
   }

   public static void main(String[] args) throws ParseException {
      new HangmanGUI();
   }
}

class ImagePanel extends JPanel {
   private static final int PREF_W = 400;
   private static final int PREF_H = PREF_W;
   private static final String TITLE = "Hangman Image";

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   public ImagePanel() {
      setBorder(BorderFactory.createTitledBorder(TITLE));
   }
}

class DetailsPanel extends JPanel {
   public DetailsPanel() {
      setLayout(new BorderLayout());

      setBorder(BorderFactory.createTitledBorder(" ciick here "));
      add(createFormattedPanel(), BorderLayout.PAGE_START);

      JPanel letterPanel = new JPanel(new GridLayout(0, 5));
      for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
         String buttonText = String.valueOf(alphabet);
         JButton letterButton = new JButton(buttonText);
         letterButton.addActionListener(clickedbutton());
         letterPanel.add(letterButton, BorderLayout.CENTER);
      }
      add(letterPanel, BorderLayout.CENTER);
   }

   private ActionListener clickedbutton() {
      return new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            String actionCommand = e.getActionCommand();
            System.out.println("actionCommand is: " + actionCommand);
         }
      };
   }

   public JPanel createFormattedPanel() {
      JPanel panel = new JPanel();
      MaskFormatter formatter = null;
      try {
         JLabel label = new JLabel("Guesss");
         formatter = new MaskFormatter("? ? ? ? ? ? ?");
         formatter.setPlaceholderCharacter('?');
         JFormattedTextField input = new JFormattedTextField(formatter);
         input.setColumns(20);
//         this.add(label);
//         this.add(input);
         panel.add(label);
         panel.add(input);
      } catch (java.text.ParseException exc) {
         System.err.println("formatter is bad: " + exc.getMessage());
         System.exit(-1);
      }

      return panel;
   }
}

另外,不要设置组件的位置或将其设置为可见,直到添加所有并打包所有。不要设置组件的大小,而是让首选大小和布局管理器为您设置大小。


编辑
你问:

首先感谢您的帮助。这是我想要的解决方案。不过,我有一些基本问题。你为什么注释掉 // myframe.getContentPane().setLayout(new BorderLayout());

JFrame 的 contentPane 已经默认使用 BorderLayout,因此没有必要将其显式设置为此布局。

您还创建了三个面板,这很好。对于图像面板,您指定了尺寸。但是对于 detailsPanel,您没有指定维度。这是什么意思?

我认为图像将具有确定的大小,因此我覆盖了它的 getPreferredSize 以便它足够大以显示图像。所有其他组件,我让它们根据组件的首选大小和布局管理器自行调整大小。

如果我有很多面板并且我为一个面板指定尺寸(大小),那么所有其他面板都会在框架上获得该默认值。

同样,所有其他人将根据其组件和布局管理器的首选尺寸拥有自己的首选尺寸。

于 2013-10-17T23:11:24.827 回答
2

GridLayout您可以像这样为您的按钮创建一个:

public DetailsPanel() {
    setPreferredSize(new Dimension(400, 600));
    createFormattedPanel();
    JPanel buttons = new JPanel();
    buttons.setBorder(BorderFactory.createTitledBorder(" ciick here "));
    buttons.setLayout(new GridLayout(5,6));

    for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
        String buttonText = String.valueOf(alphabet);
        JButton letterButton = new JButton(buttonText);
        letterButton.addActionListener(clickedbutton());
        buttons.add(letterButton);
    }
    add(buttons);
}

然后我还在你的主页面中添加了一个额外的虚拟面板,只有一个标签,将此标签更改为你想要的任何组件:

    myPanel = new DetailsPanel();
    JFrame myframe = new JFrame();
    JPanel aDummy = new JPanel();
    aDummy.add(new JLabel("Extra text"));
    myframe.getContentPane().setLayout(new BorderLayout());
    myframe.getContentPane().add(aDummy, BorderLayout.CENTER);
    myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
    //...etc

出来是这样的

在此处输入图像描述

于 2013-10-17T23:16:38.800 回答