设置宽度,高度和中心JPanel

时间:2016-04-14 18:13:56

标签: java swing miglayout

我想要一个Jpanel我的java应用程序在宽度和高度setSize (700.400);之外以及屏幕的中心。但我无法得到:

public class MainView extends JFrame {
private static final long serialVersionUID = -3698259040339973565L;

private JPanel _main;
private JPanel _log;
private CardLayout _cl = new CardLayout();

/**
 * 
 */
public MainView() {
    JPanel main = new JPanel();
    main.setLayout(new MigLayout("fill, gapy 2lp, insets 0", "", "[grow 90, 90%][grow 10, 10%]"));

    _main = new JPanel();
    _main.setLayout(_cl);
    _log = new JPanel();

    main.setBackground(Color.decode(Config.Config.COLOR_BACKGROUND));

    main.add(_main,     "grow, align center, wrap");
    main.add(_log,      "grow, hmin 80lp, hmax 80lp");

    this.setUndecorated(true);
    this.add(main);

    this.setTitle("Fotofinisher");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

    try {
        UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
    } catch (Exception e) {
        e.printStackTrace();
    }
    this.setFocusable(true);
  }
}

这段代码没有举例,它适合我的项目并且运行但无法调整大小。

1 个答案:

答案 0 :(得分:1)

  

和屏幕的中心。

setSize (700.400); 
setLocationRelativeTo(null);
setVisible(true);
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );

此外,上述语句需要在创建任何组件之前执行。