开发者

Help with SpringLayout?

I have a very odd problem when I use Java's SpringLayout manager. I'm triyng to get a tool bar to appear in my program. It was working at an earlier point in my program and now it does not work. Basically, if I remove the layout parameter from my instantiation of a JPanel the elements that I have added in the JPanel show up, albeit without my customizations. If I have that parameter in the instantiation, the toolbar does not appear at all. I have no idea what I jacked up or am doing wrong. The JPanel is going into a central JFrame, which I've changed around from a BorderLayout to another SpringLayout to nothing and it does not seem to affect this problem.

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

}

I've included here the class and the offending method. Any help would be most greatly appreciated as I'm sure I am not the first one to have this problem. I also apologize if this is a relatively simple fix and I did not see it. I'm still sorta new to Java GUIs (and Java in general) so please excuse me.


Why are you trying to create a toolbar using a panel? Whats wrong with JToolBar which uses its own custom layout?

Even if you did create a custom toolbar panel I wouldn't use a SpringLayout, its too complicated. Just use a FlowLayout or a horizontal BoxLayout.

Read the Swing tutorial on Layout Managers for working examples of each layout manager.


As noted in this related question, the order of constraint specification may be important, particularly if the resulting layout is over-constrained. Depending on the version, you may have to specify the EAST/SOUTH constraints before the WEST/NORTH constraints, as described here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜