FlowLayout中的网格布局的顶部不工作(FlowLayout on top of GridLa

2019-06-17 19:11发布

我试图创建一个刽子手游戏,到目前为止,它沿着伟大的到来,但布局设计似乎没有陷入的地方! 字母表应该在结束了FlowLayout为了与按钮“重新启动”的刽子手照片的顶部,“帮助”,“添加新词”和“退出”在底部! 我究竟做错了什么?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Hangman extends JFrame
{
    int i = 0;
    static JPanel panel;
    static JPanel panel2;
    static JPanel panel3;

    public Hangman()
    {
        JButton[] buttons = new JButton[26];

        panel = new JPanel(new FlowLayout());
        panel2 = new JPanel();
        panel3 = new JPanel();

        JButton btnRestart = new JButton("Restart");
        btnRestart.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {

            }
        });

        JButton btnNewWord = new JButton("Add New Word");
        btnNewWord.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileWriter fw = new FileWriter("Words.txt", true);
                    PrintWriter pw = new PrintWriter(fw, true);

                    String word = JOptionPane.showInputDialog("Please enter a word: ");

                    pw.println(word);
                    pw.close();
                }
                catch(IOException ie)
                {
                    System.out.println("Error Thrown" + ie.getMessage());
                }
            }
        });

        JButton btnHelp = new JButton("Help");
        btnHelp.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                String message = "The word to guess is represented by a row "
                   + "of dashes, giving the number of letters and category of "
                   + "the word. \nIf the guessing player suggests a letter "
                   + "which occurs in the word, the other player writes it "
                   + "in all its correct positions. \nIf the suggested "
                   + "letter does not occur in the word, the other player "
                   + "draws one element of the hangman diagram as a tally mark."
                   + "\n"
                   + "\nThe game is over when:"
                   + "\nThe guessing player completes the word, or guesses "
                   + "the whole word correctly"
                   + "\nThe other player completes the diagram";
               JOptionPane.showMessageDialog(null,message, "Help",JOptionPane.INFORMATION_MESSAGE);
            }
        });

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });

        ImageIcon icon = new ImageIcon("D:\\Varsity College\\Prog212Assign1_10-013803\\images\\Hangman1.jpg");
        JLabel label = new JLabel();
        label.setIcon(icon);
        String  b[]=  {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        for(i = 0; i < buttons.length; i++)
        {
            buttons[i] = new JButton(b[i]);

            panel.add(buttons[i]);
        }
        panel2.add(label);

        panel3.add(btnRestart);
        panel3.add(btnNewWord);
        panel3.add(btnHelp);
        panel3.add(btnExit);
    }

    public static void main(String[] args) 
    {
        Hangman frame = new Hangman();
        frame.add(panel, BorderLayout.NORTH);
        frame.add(panel2, BorderLayout.CENTER);
        frame.add(panel3, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
}

Answer 1:

这里有几个建议:

  • 使用GridLayout的顶部面板; 在这种情况下,零表示行被指定列数目和布局中的分量的总数来确定的数量:

     JPanel north = new JPanel(new GridLayout(0, 9)); 
  • 这里是你如何让你的中央面板有一个合理的初始大小的轮廓; 注意,您如何绘制相对于当前的大小:

     JPanel center = new JPanel() { private static final int N = 256; private static final String S = "Todo..."; @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int dx = (getWidth() - g.getFontMetrics().stringWidth(S)) / 2; int dy = getHeight() / 2; g.drawString(S, dx, dy); } @Override public Dimension getPreferredSize() { return new Dimension(N, N); } }; 
  • 你可以建立你的按钮名称如下:

     for (int i = 0; i < 26; i++) { String letter = String.valueOf((char) (i + 'A')); buttons[i] = new JButton(letter); north.add(buttons[i]); } 
  • 让你的面板实例变量 ,并开始对事件调度线程

     EventQueue.invokeLater(new Runnable() { @Override public void run() { Hangman frame = new Hangman(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.add(frame.north, BorderLayout.NORTH); frame.add(frame.center, BorderLayout.CENTER); frame.add(frame.south, BorderLayout.SOUTH); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } }); 


Answer 2:

如果你做一些研究,这个问题是相当有据可查的 - 它似乎所有的面板(除了中心之一)调整时,不会重新计算。 见我如何使此FlowLayout其内JSplitPane的包? 和http://www.velocityreviews.com/forums/t608472-wrap-flowlayout.html

但是,对于一个真正速战速决,尝试改变你的main方法,这个...(基本上使用的BoxLayout作为你的主容器)

public static void main(String[] args) 
{
    TempProject frame = new TempProject();
    Box mainPanel = Box.createVerticalBox();
    frame.setContentPane(mainPanel);
    mainPanel.add(panel);
    mainPanel.add(panel2);
    mainPanel.add(panel3);
    frame.pack();
    frame.setVisible(true);
}


文章来源: FlowLayout on top of GridLayout not working