2017-12-06 21 views
2

이 내 JFrame의의 의도 된 디자인 : 그것은 구현할 수있을만큼 쉽게 보이는(구라 두 가방 레이아웃) 그래픽 구성 요소가 다른 그리드로 클리핑되어, 확실하지 왜

enter image description here

, 일부 버튼, 텍스트 필드, lables을 가지고 , 그리고 선택 목록.

왼쪽 상단의 노란색 상자가 "그래픽"이라고도합니다. 나는 사용법에 너무 익숙하지 않다.

위와 비슷한 것을 만들기 위해 그리드백 레이아웃을 사용하고 싶습니다. 이것은 내가 문제로 실행하기 전에 생성 할 수 있었다 무엇

:

enter image description here

레이블 red:, 텍스트 상자와 -/+ 버튼 모든 COL 1 (gridy = 1;)에 있습니다.

그래픽을 행 0 col0 (gridwidth = 4)에 추가했을 때, 0보다 작기 때문에 그 요소 위로 갈 것을 기대했습니다. 슬프게도 내가 얻은 것이 아니라 그 이유에 대해 알지 못해 좌절감을 느낍니다.

내 결과 : 앞서 말했듯이

enter image description here

, 나는 JFrames와 그 많은 경험을 필요가 없습니다. 나는 정말로 누군가의 경험에 감사 할 것이고, 이것을 고치고 이유에 대한 설명을하도록 도울 것입니다.

(1/2) : 여기

내 소스 코드

package colorSamplerApp; 

import java.awt.*; 
import javax.swing.*; 



public class WindowApplication extends JFrame { 
    final static boolean shouldFill = true; 
    final static boolean shouldWeightX = true; 
    final static boolean RIGHT_TO_LEFT = false; 

    protected static JLabel redLabel; 
    protected static JLabel greenLabel; 
    protected static JLabel blueLabel; 

    protected static JTextField redTextField; 
    protected static JTextField greenTextField; 
    protected static JTextField blueTextField; 

    protected static JButton redButtonM; 
    protected static JButton greenButtonM; 
    protected static JButton blueButtonM; 

    protected static JButton redButtonP; 
    protected static JButton greenButtonP; 
    protected static JButton blueButtonP; 

    protected static JButton saveButton; 
    protected static JButton resetButton; 

    protected static JList listColors; 

    protected static DrawingTester drawTest; 



    public static void addComponentsToPane(Container pane) 
    { 
     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 


     pane.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     if (shouldFill) { 
     //natural height, maximum width 
     c.fill = GridBagConstraints.HORIZONTAL; 
     } 


     /** 
     * Setup the RGB Labels 
     * 
     * R <0,1> 
     * G <0,2> 
     * B <0,3> 
     * 
     */ 

     c.insets = new Insets(0,10,0,5); 

     c.gridwidth = 1; 
     redLabel = new JLabel("Red:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 1; 
     pane.add(redLabel, c); 

     greenLabel = new JLabel("Green:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 2; 
     pane.add(greenLabel, c); 

     blueLabel = new JLabel("Blue:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 3; 
     pane.add(blueLabel, c); 

     /** 
     * Setup the RGB Text Fields 
     * 
     * R<1,1> 
     * G<1,2> 
     * B<1,3> 
     * 
     */ 


     c.insets.set(0, 0, 0, 5); 

     redTextField = new JTextField("255"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 1; 
     pane.add(redTextField, c); 

     greenTextField = new JTextField("0"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 2; 
     pane.add(greenTextField, c); 

     blueTextField = new JTextField("0"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 3; 
     pane.add(blueTextField, c); 

     /** 
     * Setup the RGB (-) Button Fields 
     * 
     * R<2,1> 
     * G<2,2> 
     * B<2,3> 
     * 
     */ 

     c.insets.set(5,5,0,10); 

     redButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 1; 
     pane.add(redButtonM, c); 

     greenButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 2; 
     pane.add(greenButtonM, c); 

     blueButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 3; 
     pane.add(blueButtonM, c); 

     /** 
     * Setup the RGB (+) Button Fields 
     * 
     * R<3,1> 
     * G<3,2> 
     * B<3,3> 
     * 
     */ 

     c.insets.set(5,0,0,10); 

     redButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 1; 
     pane.add(redButtonP, c); 

     greenButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 2; 
     pane.add(greenButtonP, c); 

     blueButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 3; 
     pane.add(blueButtonP, c); 

     /** 
     * Setup the Save/Reset Buttons 
     * 
     * save <0,4> 
     * reset<1,4> 
     * 
     */ 

     c.insets.set(10,10,10,5); 

     saveButton = new JButton("Save"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 10;  //make this component tall 
     c.ipadx = 10; 
     //c.weightx = 0.0; 
     c.gridwidth = 2; 
     c.gridx = 0; 
     c.gridy = 4; 
     pane.add(saveButton, c); 


     c.insets.set(10,5,10,10); 

     resetButton = new JButton("Reset"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 10;  
     c.ipadx = 10; 
     //c.weighty = 1.0; //request any extra vertical space 
     //c.anchor = GridBagConstraints.PAGE_END; //bottom of space 
     c.gridx = 2;  //aligned with button 2 
     c.gridwidth = 2; //2 columns wide 
     c.gridy = 4;  //third row 
     pane.add(resetButton, c); 

     /** 
     * Setup the Color Selection List 
     * 
     * <4,0> 
     * 
     */ 

     c.insets.set(10,0,10,10); 

     listColors = new JList(); 
     /** 
      File reading shenanigans to fill up the list 
     */ 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 150;  
     c.ipadx = 100; 
     //c.weighty = 1.0; //request any extra vertical space 
     //c.anchor = GridBagConstraints.PAGE_END; //bottom of space 
     c.gridx = 4;  //aligned with button 2 
     c.gridheight = 5; //2 columns wide 
     c.gridy = 1;  //third row 
     pane.add(listColors, c); 

     /** 
     * Setup the Graphics 
     * 
     * <0,0> 
     * 
     */ 

     drawTest = new DrawingTester(); 
     c.insets.set(0,0,0,0); 
     c.gridx = 0;  //top leftw 
     c.gridy = 0;  //bottom 
     c.gridwidth = 0 ; 
     pane.add(drawTest,c); 
    } 


    public static void main (String argv[]) 
    { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI("Color Sampler"); 
      } 
     }); 
    } 

    public static void createAndShowGUI(String title) 
    { 
     // Create and setup the window 
     JFrame frame = new JFrame (title); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Setup the content pane 
     addComponentsToPane(frame.getContentPane()); 

     // Display the window 
     frame.pack(); 
     frame.setVisible(true); 

    } 


} 

(2/2)

package colorSamplerApp; 

import java.awt.*; 
import javax.swing.*; 

import javax.swing.JComponent; 

public class DrawingTester extends JComponent { 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.setColor(Color.red); 
     g.fillRect(0,0, 170, 70); 
    } 
} 
+0

각 JComponent에 대한 필드를 만드는 대신 배열을 사용하고 이러한 구성 요소를 만드는 것과 관련하여 디자인 패턴 "팩토리 패턴"을 조사하는 것이 좋습니다. 동일한 코드를 연속 3-5 회 타이핑하면 자신이하는 일을 재고하십시오. 때때로 그것을 피할 수는 없지만 99 %의 시간을 더 잘 할 수 있습니다. –

답변

3

당신이를 재사용 관련이 직면하고있는 문제 같은 GridBagConstraints 개체. 꽤 일반적인 하나이 레이아웃을 다루는 고통 중 하나입니다.

우선, 나는 insets을 변경 한 모든 호출을 주석 처리했습니다.

그런 다음 gridxgridy이 올바르게 설정되었는지 확인했습니다. 또한 gridheightgridwidth을 구성 요소 그리드의 셀 수로 생각하면 셀에 배치 할 내부 구성 요소의 실제 widthheight과 관련하여 생각하지 마십시오.

DrawingTester에서 나는 또한 모든 것을 그림으로 그려야한다고했습니다.

체크 아웃 코드 및 결과 :

import java.awt.*; 
import javax.swing.*; 

public class WindowApplication extends JFrame { 
    final static boolean shouldFill = true; 
    final static boolean shouldWeightX = true; 
    final static boolean RIGHT_TO_LEFT = false; 

    protected static JLabel redLabel; 
    protected static JLabel greenLabel; 
    protected static JLabel blueLabel; 

    protected static JTextField redTextField; 
    protected static JTextField greenTextField; 
    protected static JTextField blueTextField; 

    protected static JButton redButtonM; 
    protected static JButton greenButtonM; 
    protected static JButton blueButtonM; 

    protected static JButton redButtonP; 
    protected static JButton greenButtonP; 
    protected static JButton blueButtonP; 

    protected static JButton saveButton; 
    protected static JButton resetButton; 

    protected static JList listColors; 

    protected static DrawingTester drawTest; 



    public static void addComponentsToPane(Container pane) 
    { 
     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 


     pane.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     if (shouldFill) { 
     //natural height, maximum width 
     c.fill = GridBagConstraints.HORIZONTAL; 
     } 


     /** 
     * Setup the RGB Labels 
     * 
     * R <0,1> 
     * G <0,2> 
     * B <0,3> 
     * 
     */ 

//  c.insets = new Insets(0,10,0,5); 

     c.gridwidth = 1; 
     redLabel = new JLabel("Red:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 1; 
     pane.add(redLabel, c); 

     greenLabel = new JLabel("Green:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 2; 
     pane.add(greenLabel, c); 

     blueLabel = new JLabel("Blue:"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 3; 
     pane.add(blueLabel, c); 

     /** 
     * Setup the RGB Text Fields 
     * 
     * R<1,1> 
     * G<1,2> 
     * B<1,3> 
     * 
     */ 


//  c.insets.set(0, 0, 0, 5); 

     redTextField = new JTextField("255"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 1; 
     pane.add(redTextField, c); 

     greenTextField = new JTextField("0"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 2; 
     pane.add(greenTextField, c); 

     blueTextField = new JTextField("0"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1; 
     c.gridy = 3; 
     pane.add(blueTextField, c); 

     /** 
     * Setup the RGB (-) Button Fields 
     * 
     * R<2,1> 
     * G<2,2> 
     * B<2,3> 
     * 
     */ 

//  c.insets.set(5,5,0,10); 

     redButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 1; 
     pane.add(redButtonM, c); 

     greenButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 2; 
     pane.add(greenButtonM, c); 

     blueButtonM = new JButton("-"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 2; 
     c.gridy = 3; 
     pane.add(blueButtonM, c); 

     /** 
     * Setup the RGB (+) Button Fields 
     * 
     * R<3,1> 
     * G<3,2> 
     * B<3,3> 
     * 
     */ 

//  c.insets.set(5,0,0,10); 

     redButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 1; 
     pane.add(redButtonP, c); 

     greenButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 2; 
     pane.add(greenButtonP, c); 

     blueButtonP = new JButton("+"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 3; 
     pane.add(blueButtonP, c); 

     /** 
     * Setup the Save/Reset Buttons 
     * 
     * save <0,4> 
     * reset<1,4> 
     * 
     */ 

     saveButton = new JButton("Save"); 
     c.insets.set(10,10,10,5); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 10;  //make this component tall 
     c.ipadx = 10; 
     //c.weightx = 0.0; 
     c.gridwidth = 2; 
     c.gridx = 0; 
     c.gridy = 4; 
     pane.add(saveButton, c); 


     resetButton = new JButton("Reset"); 
     c.insets.set(10,5,10,10); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 10;  
     c.ipadx = 10; 
     //c.weighty = 1.0; //request any extra vertical space 
     //c.anchor = GridBagConstraints.PAGE_END; //bottom of space 
     c.gridx = 2;  //aligned with button 2 
     c.gridwidth = 2; //2 columns wide 
     c.gridy = 4;  //third row 
     pane.add(resetButton, c); 

     /** 
     * Setup the Color Selection List 
     * 
     * <4,0> 
     * 
     */ 

     c.insets.set(5,5,5,5); 

     listColors = new JList(new Object[] {"Test 1", "Test 2", "Test 3"}); 
     /** 
      File reading shenanigans to fill up the list 
     */ 
     c.fill = GridBagConstraints.BOTH; 
     c.gridy = 0;  
     c.gridx = 4;  
     c.gridheight = 5; 
     c.gridwidth = 1; 

     pane.add(listColors, c); 

     /** 
     * Setup the Graphics 
     * 
     * <0,0> 
     * 
     */ 

     drawTest = new DrawingTester(); 
     c.insets = new Insets(5,5,5,5); 
     c.fill = GridBagConstraints.BOTH; 
     c.gridx = 0;  //top leftw 
     c.gridy = 0;  //bottom 
     c.gridheight = 1; 
     c.gridwidth = 4; 
     pane.add(drawTest,c); 
    } 


    public static void main (String argv[]) 
    { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI("Color Sampler"); 
      } 
     }); 
    } 

    public static void createAndShowGUI(String title) 
    { 
     // Create and setup the window 
     JFrame frame = new JFrame (title); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Setup the content pane 
     addComponentsToPane(frame.getContentPane()); 

     // Display the window 
     frame.pack(); 
     frame.setVisible(true); 

    } 


} 

class DrawingTester extends JComponent { 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.setColor(Color.red); 
     g.fillRect(0,0, g.getClipBounds().width, g.getClipBounds().height); 
    } 
} 

체크 아웃 결과 : 당신이 당신의 필요에 맞게 변경을 계속 할 수 있어야에 그 시점에서

enter image description here

을, insets을 모두 다시 가져오고 구성 요소 중 heightwidth을 변경하십시오. GridBagConstraints을 재사용한다는 점에 유의해야하므로주의해야합니다. 확실하지 않은 경우 새 인스턴스를 만들고 코드를 테스트하십시오.