【问题标题】:JPanel additional column on GUI upon run?运行时 GUI 上的 JPanel 附加列?
【发布时间】:2013-05-10 17:17:36
【问题描述】:

一直在做一个学校项目,我和我的团队正在制作一个带有 GUI 和所有内容的命运之轮游戏。在为要显示的拼图制作panel 时,我遇到了创建附加列的问题。它也可能只是空的空间,但无论如何,我想知道如何摆脱它。这是贯穿wheelGUI 类的letterBoard 类的代码。

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

 public class letterBoard extends JPanel
                            implements ActionListener                   
 {
 private JTextField[] fields = new JTextField[TEXT_FIELD_COUNT];
 private Box[] boxes = new Box[SUIT_COUNT];
 private static int TEXT_FIELD_COUNT = 14;
 private static int SUIT_COUNT = 1;
 Color datGreen=new Color(0, 180, 100);

public letterBoard()
{
    setBackground(Color.GRAY);
    JPanel panel = new JPanel(new GridLayout(4,1));
    panel.setBackground(datGreen);
    for(int t=0; t<4; t++)
    {
        for (int i =0; i < boxes.length; i++)
        {
            boxes[i] = Box.createHorizontalBox();
            for (int j=0; j< TEXT_FIELD_COUNT/SUIT_COUNT; j++)
            {
                int index = i * (TEXT_FIELD_COUNT/SUIT_COUNT) + j;
                fields[index] = new JTextField("   ");
                fields[index].setEditable(false);
                fields[index].setPreferredSize(new Dimension(50, 50));
                fields[index].setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
                panel.add(fields[index]);
                panel.add(boxes[i]);
                panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK,3),"WHEEL OF FORTUNE"));
            }
        }
    }
    Box b1 = Box.createVerticalBox();
    b1.add(panel);
    b1.add(Box.createVerticalStrut(5));
    b1.add(Box.createHorizontalStrut(5));
    add(b1);
}
public void actionPerformed(ActionEvent e) 
{

}
}

在确定问题方面的任何帮助都会很棒。 Here's 编译后的样子。右边的绿条是问题所在。谢谢!

【问题讨论】:

  • 你试过打包你的板容器吗?

标签: java swing jpanel jtextfield 2d-games


【解决方案1】:

也许我只是错过了重点,但我认为不需要所有 Box 属性...

我只是将GridLayout 修改为优先于行布局14 列,并删除了所有Box 的东西...

public LetterBoard() {
    setBackground(Color.GRAY);
    JPanel panel = new JPanel(new GridLayout(0, 14));
    panel.setBackground(datGreen);
    for (int t = 0; t < 4; t++) {
        for (int i = 0; i < boxes.length; i++) {
            for (int j = 0; j < TEXT_FIELD_COUNT / SUIT_COUNT; j++) {
                int index = i * (TEXT_FIELD_COUNT / SUIT_COUNT) + j;
                fields[index] = new JTextField("   ");
                fields[index].setEditable(false);
                fields[index].setPreferredSize(new Dimension(50, 50));
                fields[index].setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
                panel.add(fields[index]);
            }
        }
    }
    panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK, 3), "WHEEL OF FORTUNE"));
    add(panel);
}

所有Box 的东西都在每一行的末尾添加了一个额外的“列”...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多