2017-04-04 5 views
0

GridBagLayout에 관한 질문이 있습니다. 나는 간단한 계산기를 만들려고 노력 중이며 모든 버튼을 논리적 인 버튼의 크기를 변경하지 않고 중간에 놓을지라도 모든 버튼을 GridBagLayout을 사용하여 버튼 패널에 넣습니다. GridLayout (버튼의 크기를 JPanel의 크기로 조정)과 GridBagLayout (원하는대로 너비와 순서로 넣을 수 있음) 사이에 뭔가를 얻을 수 있습니까?GridBagLayout을 JPanel 크기로 조정

레이아웃과 코드는 다음과 같습니다 :

... BottomPanel() {

this.setLayout(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 

    backspace = new JButton("<--"); 
    clean = new JButton("C"); 
    plusminus = new JButton("+/-"); 
    squareroot = new JButton("\u221A"); 
    divide = new JButton("/"); 
    percent = new JButton("%"); 
    multiply = new JButton("*"); 
    fraction = new JButton("1/x"); 
    minus = new JButton("-"); 
    plus = new JButton("+"); 
    dot = new JButton("."); 
    equals = new JButton("="); 
    zero = new JButton("0"); 
    one = new JButton("1"); 
    two = new JButton("2"); 
    three = new JButton("3"); 
    four = new JButton("4"); 
    five = new JButton("5"); 
    six = new JButton("6"); 
    seven = new JButton("7"); 
    eight = new JButton("8"); 
    nine = new JButton("9"); 


    gbc.gridx = 0; 
    gbc.gridy = 0; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(backspace, gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 0; 
    gbc.gridwidth = 2; 
    gbc.gridheight = 1; 
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    this.add(clean, gbc); 

    gbc.gridx = 3; 
    gbc.gridy = 0; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(plusminus, gbc); 

    gbc.gridx = 4; 
    gbc.gridy = 0; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(squareroot, gbc); 

    gbc.gridx = 0; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(seven, gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(eight, gbc);  

    gbc.gridx = 2; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(nine, gbc); 

    gbc.gridx = 3; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(divide, gbc); 

    gbc.gridx = 4; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(percent, gbc); 

    gbc.gridx = 0; 
    gbc.gridy = 2; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(four, gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 2; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(five, gbc); 

    gbc.gridx = 2; 
    gbc.gridy = 2; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(six, gbc); 

    gbc.gridx = 3; 
    gbc.gridy = 2; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(multiply, gbc); 

    gbc.gridx = 4; 
    gbc.gridy = 2; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(fraction, gbc); 

    gbc.gridx = 0; 
    gbc.gridy = 3; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(one, gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 3; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(two, gbc); 

    gbc.gridx = 2; 
    gbc.gridy = 3; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(three, gbc); 

    gbc.gridx = 3; 
    gbc.gridy = 3; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(minus, gbc); 

    gbc.gridx = 4; 
    gbc.gridy = 3; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 2; 
    gbc.fill = GridBagConstraints.VERTICAL; 
    this.add(equals, gbc); 

    gbc.gridx = 0; 
    gbc.gridy = 4; 
    gbc.gridwidth = 2; 
    gbc.gridheight = 1; 
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    this.add(zero, gbc); 

    gbc.gridx = 2; 
    gbc.gridy = 4; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(dot, gbc); 

    gbc.gridx = 3; 
    gbc.gridy = 4; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(plus, gbc); 




} 

그 결과는 다음과 같습니다

Calculator

감사합니다!

+0

아마도 jgoodies formlayout이 필요에 맞습니다. – XtremeBaumer

+0

그리드 백 구속 조건에 0이 아닌 가중치를 지정하면 도움이됩니까? –

답변

0

다음은 올바른 방향으로 적어도 단계 : 당신의 가치 그것은 중요하지

gbc.weightx = 0.1; 
    gbc.weighty = 0.1; 

단지보다 0.0보다 선택과 : 당신이 당신의 GridBagConstraints 객체를 생성 한 후, 엄격하게 큰 공보다 그 무게 설정 1.0 이하. 당신이 원하는 경우에 버튼이 때 성장하는 것을

Small panel

Panel dragged large

당신은 아마 이미 알고있다 :이 가능한 공간을 채우기 위해 수평 스트레칭, 심지어 수직 간격과 확산을 가장하여 버튼의 원인 패널에서는 gbc.fill = GridBagConstraints.BOTH;을 사용할 수 있습니다.

+0

고마워요! 다른 사람이 동시에 "수직으로 스트레칭하는"버튼을 "강제"하는 방법을 알고 있습니까? 감사합니다. – Dominika

+0

'gbc.fill = GridBagConstraints.BOTH; 또는'gbc.fill = GridBagConstraints.VERTICAL;'. @Dominika –

+0

정말 고마워요! 그것은 지금 완벽하게 작동합니다! 너는 매우 도움이된다! 감사! – Dominika