问题

时间:2018-01-16 12:01:33

标签: java grid-layout

您好我正在尝试创建一个GUI,其中有两个屏幕并排显示每个屏幕下方的控件,然后是一组主控件同时控制两个屏幕。我使用网格布局,这是因为在主控制集和两个单独的集之间存在巨大的空间。 (见图)为什么会这样?grid layout issue

这是我的代码:

    //create panel for content
    JPanel contentPane = new JPanel();
    GridLayout expLayout = new GridLayout(3,2);
    contentPane.setLayout(expLayout);

    mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
    mediaPlayerComponent.setPreferredSize(new Dimension(600, 600));
    contentPane.add(mediaPlayerComponent);

    mediaPlayerComponent2 = new EmbeddedMediaPlayerComponent();
    mediaPlayerComponent2.setPreferredSize(new Dimension(600, 600));
    contentPane.add(mediaPlayerComponent2);

    //create screen 1 buttons
    JPanel controlsPane1 = new JPanel();
    playButton1 = new JButton(playImg);
    playButton1.setPreferredSize(new Dimension(30, 30));
    controlsPane1.add(playButton1);
    rewindButton1 = new JButton(rewindImg);
    rewindButton1.setPreferredSize(new Dimension(30, 30));
    controlsPane1.add(rewindButton1);
    skipButton1 = new JButton(skipImg);
    skipButton1.setPreferredSize(new Dimension(30, 30));
    controlsPane1.add(skipButton1);
    nextFrame1 = new JButton(nextFrameImg);
    nextFrame1.setPreferredSize(new Dimension(30, 30));
    controlsPane1.add(nextFrame1);
    openButton = new JButton(openButtonImg);
    openButton.setPreferredSize(new Dimension(30, 30));
    controlsPane1.add(openButton);


    //place screen 1 controls
    contentPane.add(controlsPane1);

    //create screen 2 buttons
    JPanel controlsPane2 = new JPanel();
    playButton2 = new JButton(playImg);
    playButton2.setPreferredSize(new Dimension(30, 30));
    controlsPane2.add(playButton2);
    rewindButton2 = new JButton(rewindImg);
    rewindButton2.setPreferredSize(new Dimension(30, 30));
    controlsPane2.add(rewindButton2);
    skipButton2 = new JButton(skipImg);
    skipButton2.setPreferredSize(new Dimension(30, 30));
    controlsPane2.add(skipButton2);
    nextFrame2 = new JButton(nextFrameImg);
    nextFrame2.setPreferredSize(new Dimension(30, 30));
    controlsPane2.add(nextFrame2);
    openButton2 = new JButton(openButtonImg);
    openButton2.setPreferredSize(new Dimension(30, 30));
    controlsPane2.add(openButton2);

    //place screen 2 controls
    contentPane.add(controlsPane2);

    //create panel for buttons for Dual screen
    JPanel controlsPane = new JPanel();

    playButton = new JButton(playImg);
    playButton.setPreferredSize(new Dimension(30, 30));
    controlsPane.add(playButton, BorderLayout.PAGE_END);
    rewindButton = new JButton(rewindImg);
    rewindButton.setPreferredSize(new Dimension(30, 30));
    controlsPane.add(rewindButton, BorderLayout.PAGE_END);
    skipButton = new JButton(skipImg);        
    skipButton.setPreferredSize(new Dimension(30, 30));
    controlsPane.add(skipButton, BorderLayout.PAGE_END);
    nextFrame = new JButton(nextFrameImg);
    nextFrame.setPreferredSize(new Dimension(30, 30));
    controlsPane.add(nextFrame, BorderLayout.PAGE_END);
    sync = new JButton(syncImg);
    sync.setPreferredSize(new Dimension(30, 30));
    controlsPane.add(sync, BorderLayout.PAGE_END);
    unsync = new JButton(unSyncImg);
    unsync.setPreferredSize(new Dimension(30, 30));
    controlsPane.add(unsync);
    //place control pane at bottom
    contentPane.add(controlsPane);
frame.setContentPane(contentPane);

    frame.setVisible(true);

}

0 个答案:

没有答案