2017-03-28 18 views
0

저는 Windows 10 계산기를 기반으로하는 계산기에서 작업하고 있습니다. 나는 그것의 모양과 느낌이 마음에 들고 자바를 얼마나 잘 알고 있는지 테스트하기 위해이 프로젝트에 도전하고 있습니다. 그러나 주 디자인이 끝나면 나는 창문 주변에 여분의 패딩이 있음을 발견했다. 디버깅을하고 알아 낸 후, 필자는 내 메인 JPanel (mainPanel) 내부에 내 3 개의 JPanels (topPanel, middlePanel, bottomPanel)이 추가로 들어 있다는 것을 알게되었습니다. gridBagLayout을 사용하고 모든 인세 트를 위쪽, 아래쪽, 왼쪽 및 오른쪽으로 0으로 설정했습니다. 여분의 패딩을 제거하는 방법을 잘 모르겠습니다. 이것은 나의 계산기 클래스자바 스윙의 JPanel에서 여분의 패딩을 제거하는 방법은 무엇입니까

package javacalculator.calculator; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.BorderFactory; 

import javax.swing.border.Border; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.text.Caret; 
import javax.swing.text.DefaultCaret; 

public class Calculator { 
    private final String NAME = "Calculator"; 

    private JFrame frame; 
    private JPanel mainPanel, topPanel, middlePanel, bottomPanel; 
    private JTextField storedData; 
    private JTextField data; 
    private Caret caret; 
    private Font font; 
    private Font font2; 
    private Font font3; 
    private Font font4; 
    private Color color; 
    private JButton zero, one, two, three, four, five, six, seven, eight, nine; 
    private JButton plus, minus, multiply, divide, equals; 
    private JButton negative, decimal; 
    private JButton ce, c, backspace; 
    private JButton percent, squareRoot, squared, divideFrom1; 
    private JButton mc, mr, mplus, mminus, ms, mh; 

    public Calculator() { 
     frame = new JFrame(NAME); 
     mainPanel = new JPanel(); 
     topPanel = new JPanel(); 
     middlePanel = new JPanel(); 
     bottomPanel = new JPanel(); 

     storedData = new JTextField(); 
     data = new JTextField(); 
     caret = new DefaultCaret() { 
      @Override 
      public void paint(Graphics g) { 

      } 

      @Override 
      public boolean isVisible() { 
       return false; 
      } 

      @Override 
      public boolean isSelectionVisible() { 
       return false; 
      } 
     }; 
     font = new Font("Sans-Serif", Font.BOLD, 30); 
     font2 = new Font("Sans-Serif", Font.PLAIN, 20); 
     font3 = new Font("Sans-Serif", Font.PLAIN, 15); 
     font4 = new Font("Sans-Serif", Font.PLAIN, 13); 
     color = new Color(238, 238, 238); 

     mc = new JButton("MC"); 
     mr = new JButton("MR"); 
     mplus = new JButton("M+"); 
     mminus = new JButton("M-"); 
     ms = new JButton("MS"); 
     mh = new JButton("MH"); 

     percent = new JButton("%"); 
     squareRoot = new JButton("SQRT"); 
     squared = new JButton("x^2"); 
     divideFrom1 = new JButton("1/x"); 

     ce = new JButton("CE"); 
     c = new JButton("C"); 
     backspace = new JButton("<="); 

     plus = new JButton("+"); 
     minus = new JButton("-"); 
     multiply = new JButton("X"); 
     divide = new JButton("/"); 
     equals = new JButton("="); 

     decimal = new JButton("."); 
     negative = 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"); 

     Init(); 
    } 

    private void Init() { 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     ConfigureComponents(); 

     AddComponent(topPanel, storedData, 0, 0, 4, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(topPanel, data, 0, 1, 4, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 

     AddComponent(middlePanel, mc, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(middlePanel, mr, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(middlePanel, mplus, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(middlePanel, mminus, 3, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(middlePanel, ms, 4, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(middlePanel, mh, 5, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 

     AddComponent(bottomPanel, percent, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, squareRoot, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, squared, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, divideFrom1, 3, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 

     AddComponent(bottomPanel, ce, 0, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, c, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, backspace, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, divide, 3, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 

     AddComponent(bottomPanel, seven, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, eight, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, nine, 2, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, multiply, 3, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 

     AddComponent(bottomPanel, four, 0, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, five, 1, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, six, 2, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, minus, 3, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 

     AddComponent(bottomPanel, one, 0, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, two, 1, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, three, 2, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, plus, 3, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 

     AddComponent(bottomPanel, negative, 0, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, zero, 1, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, decimal, 2, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(bottomPanel, equals, 3, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 

     AddComponent(mainPanel, topPanel, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(mainPanel, middlePanel, 0, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 
     AddComponent(mainPanel, bottomPanel, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE); 

     frame.add(mainPanel); 

     frame.pack(); 
     frame.setResizable(false); 

     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private void AddComponent(JPanel panel, JComponent component, int x, int y, int width, int height, int position, int stretch) { 
     GridBagConstraints gbc = new GridBagConstraints(); 

     gbc.gridx = x; 
     gbc.gridy = y; 

     gbc.gridwidth = width; 
     gbc.gridheight = height; 

     gbc.weightx = 0; 
     gbc.weighty = 0; 

     gbc.anchor = position; 
     gbc.fill = stretch; 

     gbc.insets = new Insets(0, 0, 0, 0); 

     panel.add(component, gbc); 
    } 

    private void ConfigureComponents() { 
     mainPanel.setLayout(new GridBagLayout()); 
     topPanel.setLayout(new GridBagLayout()); 
     middlePanel.setLayout(new GridBagLayout()); 
     bottomPanel.setLayout(new GridBagLayout()); 

     Dimension dim = new Dimension(77, 54); 
     Dimension dim2 = new Dimension(dim.width * 4, dim.height); 
     Dimension dim3 = new Dimension(dim.width * 4, dim.height/2); 
     Dimension dim4 = new Dimension(dim.width * 4/6, dim.height/2); 

     Border noBorder = BorderFactory.createEmptyBorder(0, 0, 0, 0); 

     mainPanel.setBorder(noBorder); 
     topPanel.setBorder(noBorder); 
     middlePanel.setBorder(noBorder); 
     bottomPanel.setBorder(noBorder); 

     //TESTING 
     topPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 1, false)); 
     mainPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 3, false)); 
     //END TESTING 

     storedData.setPreferredSize(dim3); 
     storedData.setFont(font3); 
     storedData.setBackground(Color.WHITE); 
     storedData.setHorizontalAlignment(JTextField.RIGHT); 
     storedData.setBorder(noBorder); 
     //TESTING 
     storedData.setBorder(BorderFactory.createLineBorder(Color.RED, 1, false)); 
     //END TESTING 
     storedData.setCaret(caret); 
     storedData.setEditable(false); 

     data.setPreferredSize(dim2); 
     data.setFont(font); 
     data.setBackground(Color.WHITE); 
     data.setHorizontalAlignment(JTextField.RIGHT); 
     data.setBorder(noBorder); 
     data.setCaret(caret); 
     data.setEditable(false); 

     ConfigureComponent(mc, dim4, font4, Color.WHITE, Color.BLACK, noBorder, true, false, false); 
     mc.setPreferredSize(new Dimension(dim4.width + 1, dim4.height)); 
     ConfigureComponent(mr, dim4, font4, Color.WHITE, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(mplus, dim4, font4, Color.WHITE, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(mminus, dim4, font4, Color.WHITE, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(ms, dim4, font4, Color.WHITE, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(mh, dim4, font4, Color.WHITE, Color.BLACK, noBorder, true, false, false); 
     mh.setPreferredSize(new Dimension(dim4.width + 1, dim4.height)); 

     ConfigureComponent(percent, dim, font2, Color.WHITE, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(squareRoot, dim, font2, Color.WHITE, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(squared, dim, font2, Color.WHITE, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(divideFrom1, dim, font2, Color.WHITE, Color.BLACK, noBorder, true, false, false); 

     ConfigureComponent(ce, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(c, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(backspace, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(divide, dim, font2, color, Color.BLACK, noBorder, true, false, false); 

     ConfigureComponent(seven, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(eight, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(nine, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(multiply, dim, font2, color, Color.BLACK, noBorder, true, false, false); 

     ConfigureComponent(four, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(five, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(six, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(minus, dim, font2, color, Color.BLACK, noBorder, true, false, false); 

     ConfigureComponent(one, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(two, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(three, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(plus, dim, font2, color, Color.BLACK, noBorder, true, false, false); 

     ConfigureComponent(negative, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(zero, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(decimal, dim, font2, color, Color.BLACK, noBorder, true, false, false); 
     ConfigureComponent(equals, dim, font2, color, Color.BLACK, noBorder, true, false, false); 

     middlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, color)); 

    } 

    private void ConfigureComponent(JButton component, Dimension size, Font font, Color backgroundColor, Color foreGroundColor, Border border, boolean contentAreaFilled, boolean borderPainted, boolean focusPainted) { 
     component.setPreferredSize(size); 
     component.setFont(font); 
     component.setOpaque(true); 
     component.setBackground(backgroundColor); 
     component.setForeground(foreGroundColor); 
     component.setBorder(border); 
     component.setContentAreaFilled(contentAreaFilled); 
     component.setBorderPainted(borderPainted); 
     component.setFocusPainted(focusPainted); 
    } 
} 

내가 JPanel.setMargin 또는 JPanel.setInsets을 찾고 시도하고 아무것도 찾을 수 없습니다에서 내 전체 코드입니다. 어디서 볼지 잘 모르겠습니다.

빨간색 테두리는 topPanel의 내부에, 내 JTextField를 주위에있다. 녹색 테두리는 topPanel 주위에 있습니다. 파란색 테두리는 mainPanel 주위에 있습니다. 녹색 테두리와 파란색 테두리 사이의 간격을 제거하고 싶습니다. 나는 원래 국경을 빈 경계로 설정했다. 나는이 프로그램을 테스트하는데 사용하고있다.

편집 : 그냥 topPanel.setLocation(0, 0);을 시도했지만 아무 것도하지 않았습니다.

Edit2가 : 난 그냥 대신 GridBagLayout에의 BorderLayout를 일하기 mainPanel을 변경, 녹색과 파란색 테두리 사이의 공간을 없애있어하는 PAGE_END하는 센터와 bottomPanel에 PAGE_START, middlePanel에 topPanel을 정렬됩니다. 하지만 지금은 빨강과 초록색 사이에 간격이 있습니다.

답변

3

모양이 좋지 않을 수 있지만 창의 장식이 변경되어 크기 조정이 Window의 기본 크기에 영향을 줄 수 있습니다. frame.setResizable전에으로 전화 하시려면 frame.pack()으로 전화하십시오.

+0

흠. 이상한. 나는 결코 그것을 알지 못했다. 그것은 위대한 작품. – Vince