首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对SpringLayout有帮助吗?

对SpringLayout有帮助吗?
EN

Stack Overflow用户
提问于 2011-04-06 12:20:11
回答 2查看 2K关注 0票数 0

当我使用Java的SpringLayout管理器时,我遇到了一个非常奇怪的问题。我正在尝试让一个工具栏出现在我的程序中。它在我的程序的早期工作,但现在它不工作。基本上,如果我从JPanel的实例化中删除layout参数,那么我在JPanel中添加的元素就会显示出来,尽管没有我的定制。如果我在实例化中有这个参数,工具栏就根本不会出现。我不知道我做错了什么或做错了什么。JPanel将进入中央JFrame,我已将其从BorderLayout更改为另一个SpringLayout,再更改为nothing,它似乎不会影响此问题。

代码语言:javascript
复制
public class purchaserApp
{
static JFrame mainWindow;                                   //Main window
static JFrame addView = new JFrame ("Add An Item...");      //Add item window
static JFrame aboutView = new JFrame ("About Purchaser");   //About window
static JFrame helpView = new JFrame ("Purchaser Help");     //Help window
static JPanel toolBar, contentArea, previewPane;            //Panels for GUI
static JRootPane root;
static JToolBar toolBar2;
//SpringLayout mainLayout;
JTable table;

public purchaserApp()
{
    makemainWindow();
    makeMenu();
    makeToolbar();
     // makeMainContentView();
    mainWindow.setVisible(true);

}
public void makeToolbar()
{
    SpringLayout tbLayout = new SpringLayout();
    toolBar = new JPanel(tbLayout);     //this is the offending line of code, if I remove "tbLayout" the buttons show up in the GUI but obviously without the customizations I made below...
    JButton toolBarButtons[];
    String buttonNames[] = {"Add", "Edit", "Delete"};
    //Instantiate buttons for toolbar
    toolBarButtons = new JButton[3];
    //Resize
    Dimension d = new Dimension (40,55);

    //Add/modify/whatever
    for (i = 0; i < 3; i++)
    {
        toolBarButtons[i] = new JButton(buttonNames[i]);
        toolBarButtons[i].setPreferredSize(d);
        tbLayout.putConstraint(SpringLayout.NORTH, toolBarButtons[i], 5, SpringLayout.NORTH, toolBar);
    }

    //Adjust constraints
    tbLayout.putConstraint(SpringLayout.WEST, toolBarButtons[0], 5, SpringLayout.WEST, toolBar);
    tbLayout.putConstraint(SpringLayout.WEST, toolBarButtons[1], 5, SpringLayout.EAST, toolBarButtons[0]);
    tbLayout.putConstraint(SpringLayout.EAST, toolBarButtons[2], -5, SpringLayout.EAST, toolBar);       //due to x-axis, we must go negative to get inside the frame
    for (i = 0; i < 3; i++)
        toolBar.add(toolBarButtons[i]);

    mainWindow.add(toolBar,BorderLayout.NORTH);   //Lies! Does not add

}

我已经在这里包含了类和有问题的方法。任何帮助都是非常感谢的,因为我相信我不是第一个有这个问题的人。我也道歉,如果这是一个相对简单的修复,我没有看到它。我对Java GUI(以及一般的Java )还是比较陌生的,所以请原谅。

EN

回答 2

Stack Overflow用户

发布于 2011-04-06 12:57:08

为什么要尝试使用面板创建工具栏?使用自己的自定义布局的JToolBar有什么问题?

即使你创建了一个自定义工具栏面板,我也不会使用SpringLayout,它太复杂了。只需使用FlowLayout或水平BoxLayout即可。

有关每个布局管理器的工作示例,请阅读Layout Managers上的Swing教程。

票数 0
EN

Stack Overflow用户

发布于 2011-04-06 13:21:28

正如在本相关question中所指出的,约束规范的顺序可能很重要,特别是当所得到的布局是时。根据版本的不同,您可能必须在指定EAST/SOUTH约束之前指定WEST/NORTH约束,如here所述。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5561401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档