Domanda

I am still learning java in college but I just would like to know the way or ways to put the JComponent's in specific location and specify their size.

I have seen the methods setLocation() and that sort of methods but I just really wanna know how to put a Component without using those layout managers(flow, grid, border?).

Here is the code I would like to apply that knowledge to: they are two overloaded methods that display the same window.

public void addWelcome()
{
    setWindow();
    MaxDimension = new Dimension(WelcomeLabelDimensionWidth,WelcomeLabelDimensionHeight);
    getWindow().setMaximumSize(MaxDimension);
    MinDimension = new Dimension(WelcomeLabelDimensionWidth,WelcomeLabelDimensionHeight);
    getWindow().setMinimumSize(MinDimension);
    getWindow().setTitle("Bills");
    getWindow().setSize(WindowWidth,WindowHeight);
    getWindow().setDefaultCloseOperation(EXIT_ON_CLOSE);
    getWindow().setLayout(new GridLayout(5,1,2,0));
    getWindow().add(getWelcomeLabel());
    getWindow().add(getGreetLabel());
    getWindow().add(getDescriptionLabel());
    getWindow().add(getInstructionLabel());
    getWindow().add(getStartButton());  
    getWindow().pack();
    getWindow().setVisible(true);

}


public void addWelcome(final int Width, final int Height)
{
    setWindow();
    MaxDimension = new Dimension(Width,Height);
    getWindow().setMaximumSize(MaxDimension);
    MinDimension = new Dimension(Width,Height);
    getWindow().setMinimumSize(MinDimension);
    getWindow().setTitle("Bills");
    getWindow().setSize(WindowWidth,WindowHeight);
    getWindow().setDefaultCloseOperation(EXIT_ON_CLOSE);
    getWindow().setLayout(new GridLayout(5,1));
    getWindow().add(getWelcomeLabel());
    getWindow().add(getGreetLabel());
    getWindow().add(getDescriptionLabel());
    getWindow().add(getInstructionLabel());
    getWindow().add(getStartButton());
    getWindow().setVisible(true);
}

Apologize if code/question are not clear enough.

Thanks in advance.

È stato utile?

Soluzione

You can use an Absolute Layout, which makes you responsible for positioning everything. Do note that this also makes it nigh impossible to create a layout that will be usable with different screen and window sizes (so yeah, learn to use proper layouts).

Altri suggerimenti

Yes a layout manager is the best solution

An IDE like eclipse or netbeans can help you effortlessly visually position your components with different layouts.

It autogenerates code which you can easily observe and learn.

If you want to code it yourself you have all resources in this link http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top