2014-09-17 5 views
0

이 인터페이스를 수정하는 데 도움을 주시면 감사하겠습니다. 난 그냥 GridBagLayout을 학습 시작하고 난 GridBagLayout에를 사용하여 그려 인터페이스()GridBagLayout을 사용하여이보기를 수정하는 방법

|-----------------------------------------------| 
| Playing File: name of the file    | //This JLabel should cover the entire space 
|-----------------------------------------------| 
| 00:00:00 ----#---------------------- 00:00:00 | //This has on the left and on the right side two labels and in the middle a JSpinner 
|-----------------------------------------------| 
| [Open] [Play] [Pause] [Rewind] [Save] | //This line contains 5 buttons 
|-----------------------------------------------| 

는 첨부 파일에 작업 예를

import java.awt.FlowLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSlider; 
import javax.swing.SwingUtilities; 

public class Sample extends JPanel 
{ 

    private JLabel labelFileName = new JLabel("Playing File:"); 
    private JLabel labelTimeCounter = new JLabel("00:00:00"); 
    private JLabel labelDuration = new JLabel("00:00:00"); 

    private JButton buttonOpen = new JButton(" Open "); 
    private JButton buttonPlay = new JButton(" Play "); 
    private JButton buttonPause = new JButton(" Pause "); 
    private JButton buttonRewind = new JButton(" Rewind "); 
    private JButton buttonSave = new JButton(" Save "); 

    private JSlider sliderTime = new JSlider(); 

    GridBagConstraints constraints = new GridBagConstraints(); 

    public Sample() 
    { 

     setLayout(new GridBagLayout()); 

     constraints.insets = new Insets(5, 5, 5, 5); 

     sliderTime.setValue(0); 

     constraints.gridx = 0; 
     constraints.gridy = 0; 
     constraints.gridwidth = 5; 
     constraints.fill = GridBagConstraints.HORIZONTAL; 
     add(labelFileName, constraints); 

     constraints.gridx = 0; 
     constraints.gridy = 1; 
     constraints.gridwidth = 1; 
     add(labelTimeCounter, constraints); 

     constraints.gridx = 1; 
     constraints.gridy = 1; 
     constraints.gridwidth = 3; 
     add(sliderTime, constraints); 

     constraints.gridx = 4; 
     constraints.gridy = 1; 
     constraints.gridwidth = 1; 
     add(labelDuration, constraints); 

     JPanel panelButtons = new JPanel(new FlowLayout(FlowLayout.LEFT, 25, 5)); 
     panelButtons.add(buttonOpen); 
     panelButtons.add(buttonPlay); 
     panelButtons.add(buttonPause); 
     panelButtons.add(buttonRewind); 
     panelButtons.add(buttonSave); 

     constraints.gridx = 0; 
     constraints.gridy = 2; 
     add(panelButtons, constraints); 

    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       runApp(); 

      } 
     }); 
    } 

    public static void runApp() 
    { 
     JFrame frame = new JFrame("Frame"); 
     frame.setVisible(true); 
     frame.setSize(800, 200); 
     frame.add(new Sample()); 
     frame.setDefaultCloseOperation 
    } 

}

감사

을 찾아주세요을 구현할 수있는 방법을 알아낼 할
+1

레이아웃을 디버깅 할 수있는 기능이 있으면 좋을 것입니다. 그것 이외의 :'sliderTime'은 너비가 3이 아니라 2가되어야합니다.'panelButtons'는 1이 아닌 너비가 5 여야합니다. 또한 두번째 열에 대한 가중치를 양수로 설정하려고합니다. – Ordous

+2

'frame.setDefaultCloseOperation'을 잊지 마세요. 사람들이 mcve에서 그것을 잊어 버릴 때 참을 수 없습니다. 그것을 추가하는 것을 잊어 버린 경우, 많은 시간 동안 백 만개의 백그라운드 프로그램을 테스트하게 될 것입니다 :-) –

+0

@peeskillet 편집 됨 : – QGA

답변

2

3 열이있는 하나의 열 그리드를 강력하게 제안합니다.

  • 상단 행은 FlowLayout의 로 설정 패널이 될 수 있으며, 직접에 레이블을 드롭 할 수있다. 레이블의 크기는 사용자가 설정해야합니다. 레이아웃 관리자는이를 대신 할 수 없습니다.
  • 중간 행은 borderlayout이며 회 전자는 가운데로 설정되고 두 개의 레이블은 동쪽과 서쪽에 배치됩니다.
  • 하단 행은 flowlayout 인 패널 세트 여야합니다. 버튼을 다이어그램에 멋지게 배치하려면 flowlayout을 사용해야합니다.

내 관심사는 슬라이더 패널의 세로 방향 크기를 조정할 수 있다는 것입니다. 이 경우 GridBaglayout을 버리고 상단 행이 가운데에있는 테두리 위에 테두리가 있고 아래쪽 두 행이 남쪽 패널에 채워집니다.

+0

글쎄,이 답변은 좋아 보이지만, 인터페이스를 수행하기 위해 GridBagLayout()을 사용하고 싶습니다. 그것을 해결하기 위해 다른 레이아웃을 사용하는 방법을 알고 있지만 내 질문의 목적은 GridBagLayout을 조금 더 배우는 것이 었습니다. 물론 여전히 GridBagLayout – QGA

+1

다이어그램이 100 % GridBagLayout 사용에 적합하지 않습니다. 당신은 아마 그것을 할 수는 있지만 꽤 어려울 것입니다. 버튼과 슬라이더가 모든 열과 일치하지 않으므로 3x1 격자 이상을 볼 수 없습니다. –

+0

고마워요. – QGA

관련 문제