BorderLayout, Impossible to set a manual Location

2019-09-14 04:33发布

问题:

I'm having a problem, i implemented a BorderLayout JPanel. And if i try to move a button for a example in the North section, it'll stay at a default location. Here's my code :

public class Window extends JFrame{


Panel pan = new Panel();
JPanel container, north,south, west;
public JButton ip,print,cancel,start,ok;
JTextArea timeStep;
JLabel legend;
double time;
double temperature=0.0;

public static void main(String[] args) {
    new Window();

}

public Window()
{
    System.out.println("je suis là");
    this.setSize(700,400);
    this.setLocationRelativeTo(null);
    this.setResizable(true);
    this.setTitle("Assignment2 - CPU temperature");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    container = new JPanel(new BorderLayout());


    north = new JPanel();
    ip = new JButton ("New");
    ip.setPreferredSize(new Dimension(100,50));
    ip.setLocation(0, 0);
    north.add(ip);
    print = new JButton ("Print");
    north.add(print);

    north.add(new JLabel("Time Step (in s): "));
    timeStep = new JTextArea("10",1,5);
    north.add(timeStep);
    start = new JButton("OK");
    ListenForButton lForButton = new ListenForButton();
    start.addActionListener(lForButton);
    north.add(start);


    south = new JPanel();
    legend = new JLabel("Legends are here");
    south.add(legend);

    west = new JPanel();
    JLabel temp = new JLabel("°C");
    west.add(temp);

    container.add(north, BorderLayout.NORTH);
    container.add(west,BorderLayout.WEST);
    container.add(pan, BorderLayout.CENTER);
    container.add(south, BorderLayout.SOUTH);

    this.setContentPane(container);
    this.setVisible(true);
}

I want for example my "New" button to be at the left top corner of my window by writing "ip.setLocation(0,0);"

Window

And it stays by default to the center..

Any ideas ?

回答1:

Oracle Documentation about BorderLayout

A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center.


Solution

north = new JPanel();
north.setLayout(new BorderLayout());
ip = new JButton ("New");
ip.setPreferredSize(new Dimension(100,50));
print = new JButton ("Print");
north.add(ip, BorderLayout.WEST);

JPanel centerPanel = new JPanel();
centerPanel.add(print);
centerPanel.add(new JLabel("Time Step (in s): "));
timeStep = new JTextArea("10",1,5);
centerPanel.add(timeStep);
start = new JButton("OK");
centerPanel.add(start);

north.add(centerPanel, BorderLayout.CENTER);

The north panel is now composed of the following two parts :

ip (JButton) and centerPanel(JPanel) containing the rest of the components.


Output



回答2:

What you're trying to do is use an AbsoluteLayout instead of a BorderLayout, BorderLayout uses Cardinal directions to set objects on the pane, such as North, East, South, West, and Center. You may want to look at the JavaDoc for BorderLayout.

As an example, you need to set your north panel to a BorderLayout()

north.setLayout(new BorderLayout());