GridLayout 之上的 FlowLayout 不起作用 [英] FlowLayout on top of GridLayout not working

查看:40
本文介绍了GridLayout 之上的 FlowLayout 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个刽子手游戏,到目前为止它进展顺利,但布局设计似乎并没有到位!字母表应该以 FlowLayout 的顺序在 Hangman 图片的顶部结束,底部有重新启动"、帮助"、添加新单词"和退出"按钮!我做错了什么?

I'm trying to create a hangman game and so far it's coming along GREAT, but the layout design just doesn't seem to fall into place! The alphabet is supposed to end up in a FlowLayout order on top of the Hangman picture with the buttons "Restart", "Help" "Add New Word" and "Exit" at the bottom! What am I doing wrong?

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. 
If the guessing player suggests a letter "
                   + "which occurs in the word, the other player writes it "
                   + "in all its correct positions. 
If the suggested "
                   + "letter does not occur in the word, the other player "
                   + "draws one element of the hangman diagram as a tally mark."
                   + "
"
                   + "
The game is over when:"
                   + "
The guessing player completes the word, or guesses "
                   + "the whole word correctly"
                   + "
The 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);
    }
}

推荐答案

如果您进行一些研究,这个问题会得到很好的记录 - 似乎所有面板(除了 CENTER 面板)在调整大小时都不会重新计算.请参阅 如何让这个 FlowLayout 包裹在它的 JSplitPane 中?http://www.velocityreviews.com/forums/t608472-wrap-flowlayout.html

This problem is pretty well documented if you do some research - it seems all the panels (besides the CENTER one) aren't recalculated when resized. See How do I make this FlowLayout wrap within its JSplitPane? and http://www.velocityreviews.com/forums/t608472-wrap-flowlayout.html

但为了快速解决问题,请尝试将您的 main 方法更改为此...(基本上使用 BoxLayout 作为您的主容器)

But for a really quick fix, try changing your main method to this... (basically using a BoxLayout as your main container)

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);
}

这篇关于GridLayout 之上的 FlowLayout 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆