Как установить положение объектов в JFrame? - PullRequest
3 голосов
/ 04 ноября 2011

У меня есть Labels и JButton, я хочу определить позицию в JFrame.

import java.awt.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.*;

public class GuiFrame extends JFrame {

    public static void main(String[] args) throws UnknownHostException {

        JFrame f = new JFrame("This is a test");
        f.setSize(400, 150);
        JRadioButton ButtonServer = new JRadioButton("Server");
        JRadioButton ButtonClient = new JRadioButton("Client");

        InetAddress thisIp = InetAddress.getLocalHost();

        Label lip = new Label("Your IP is : " + thisIp.getHostAddress());
        Label setup = new Label("Setup as ");
        JButton ButtonOk = new JButton("OK");

        Container content = f.getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout());
        content.add(lip);
        content.add(setup);
        content.add(ButtonServer);
        content.add(ButtonClient);
        content.add(ButtonOk);
        // f.addWindowListener(new ExitListener());
        f.setVisible(true);
    }
}

setLocation () здесь не работает.Как управлять положением объекта в JFrame?

Ответы [ 4 ]

4 голосов
/ 04 ноября 2011

Используйте правильный LayoutManager.Например, GridBagLayout.

Или вы можете комбинировать несколько вложенных панелей, назначая собственный LayoutManager для каждой панели.

Худший способ - установить для макета значение null и использовать setBounds ()

3 голосов
/ 04 ноября 2011

FlowLayout дает вам несколько вариантов.Смотрите здесь .

Например

   FlowLayout layout = new FlowLayout();
   layout.setAlignment(FlowLayout.CENTER);
   c.setLayout(layout);
   c.add(panel);
2 голосов
/ 04 ноября 2011

Я всегда использую это: http://download.oracle.com/javase/tutorial/uiswing/layout/using.html

:)

0 голосов
/ 04 ноября 2011

Netbeans GUI Builder великолепен.Я предлагаю вам посмотреть на это.

http://netbeans.org/kb/docs/java/quickstart-gui.html

...