2014-04-16 4 views
0

내 문제는이 코드가 스윙 GUI를 생성하고 내 랩톱의 디스플레이 범위를 벗어납니다. 왜? 레이아웃 라이브러리로 DesignGridLayout을 사용하고 있습니다. Why does my components go out of boundaries, please help me to align it with the necessary codeenter image description hereDesignGridLayout 경계선이 벗어납니다

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JSeparator; 
import javax.swing.JTable; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.table.DefaultTableModel; 
import net.java.dev.designgridlayout.DesignGridLayout; 

public class Demo { 

    private void createAndShowGUI() { 

     JLabel i5l1 = new JLabel("FREIGHT DETAILS"); 
     JLabel i5l2 = new JLabel("Date : "); 
     JLabel i5l3 = new JLabel("Vehicle No. : "); 
     JLabel i5l4 = new JLabel("From : "); 
     JLabel i5l5 = new JLabel("Item : "); 
     JLabel i5l6 = new JLabel("Quantity : "); 
     JLabel i5l7 = new JLabel("Kg."); 
     JLabel i5l8 = new JLabel("Rate :           Rs."); 
     JLabel i5l15 = new JLabel("SALE DETAILS"); 
     JLabel i5l16 = new JLabel("Cash Sales :        Rs. "); 
     JLabel i5l17 = new JLabel("Credit :          Rs. "); 
     JLabel i5l18 = new JLabel("EXPENSES"); 
     JLabel i5l19 = new JLabel("Food & Tea :       Rs. "); 
     JLabel i5l20 = new JLabel("Wages :         Rs. "); 
     JLabel i5l21 = new JLabel("Miscellaneous Expenses :  Rs. "); 

     JTextField i5t1 = new JTextField(20); 
     JTextField i5t2 = new JTextField(20); 
     JTextField i5t3 = new JTextField(20); 
     JTextField i5t4 = new JTextField(20); 
     JTextField i5t11 = new JTextField(20); 
     JTextField i5t12 = new JTextField(20); 
     JTextField i5t13 = new JTextField(20); 
     JTextField i5t14 = new JTextField(20); 

     JComboBox i5cb1 = new JComboBox<>(); 
     JComboBox i5cb2 = new JComboBox<>(); 
     JComboBox i5cb3 = new JComboBox<>(); 

     JButton i5b1 = new JButton("Save"); 
     JButton i5b2 = new JButton("Reset"); 
     JButton i5b3 = new JButton("Close"); 

     JSeparator i5sep1 = new JSeparator(); 
     JSeparator i5sep2 = new JSeparator(); 
     JSeparator i5sep3 = new JSeparator(); 
     JSeparator i5sep4 = new JSeparator(); 
     JSeparator i5sep5 = new JSeparator(); 
     JSeparator i5sep6 = new JSeparator(); 

     Object[] columnNames = new Object[]{"Column # 1", "Column # 2", "Column # 3", "Column # 4"}; 
     DefaultTableModel model = new DefaultTableModel(columnNames, 10); 
     JTable table = new JTable(model); 

     JScrollPane i5t1sp1 = new JScrollPane(table); 

     JPanel freightPanel = new JPanel(); 
     DesignGridLayout layout1 = new DesignGridLayout(freightPanel); 

     layout1.row().left().add(i5sep1).fill().withOwnRowWidth(); 
     layout1.row().center().add(i5l1); 
     layout1.row().left().add(i5sep2).fill().withOwnRowWidth(); 
     layout1.row().grid(i5l2).add(i5t1); 
     layout1.row().grid(i5l3).add(i5t2); 
     layout1.row().grid(i5l4).add(i5cb1); 
     layout1.row().grid(i5l5).add(i5cb2); 
     layout1.row().grid(i5l6).add(i5t3).add(i5l7); 
     layout1.row().grid(i5l8).add(i5t4); 

     layout1.row().left().add(i5sep5).fill().withOwnRowWidth(); 
     layout1.row().center().add(i5l18); 
     layout1.row().left().add(i5sep6).fill().withOwnRowWidth(); 
     layout1.row().grid(i5l19).add(i5t12); 
     layout1.row().grid(i5l20).add(i5t13); 
     layout1.row().grid(i5l21).add(i5t14); 

     JPanel salePanel = new JPanel(); 
     DesignGridLayout layout2 = new DesignGridLayout(salePanel); 

     layout2.row().left().add(i5sep3).fill().withOwnRowWidth(); 
     layout2.row().center().add(i5l15); 
     layout2.row().left().add(i5sep4).fill().withOwnRowWidth(); 
     layout2.row().grid(i5l16).add(i5t11); 
     layout2.row().grid(i5l17).add(i5cb3); 
     layout2.row().grid().add(i5t1sp1); 

     JInternalFrame internalFrame = new JInternalFrame("Daily Analysis",true,true, true, true);   

     DesignGridLayout mainLayout = new DesignGridLayout(internalFrame.getContentPane()); 
     mainLayout.row().grid().add(freightPanel).add(salePanel); 
     mainLayout.row().left().add(new JSeparator()).fill().withOwnRowWidth(); 
     mainLayout.row().center().add(i5b1).add(i5b2).add(i5b3); 

     internalFrame.pack(); 
     internalFrame.setVisible(true); 

     JFrame frame = new JFrame("Demo"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(internalFrame); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

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

답변

1

글쎄, 왜 안 :

는 코드는 답변에서 가져옵니다?

pack() 프레임이 원하는 하위 크기를 모두 표시하고 크기/최소 제한을 충족하는 데 필요한 크기가되도록 프레임을 표시하거나, 표시 할 항목이 많거나 해상도가 충분하지 않은 경우 창이 화면 밖으로 벗어날 수 있습니다. 말하자면 프레임이 최대화되면 이는 놀라운 문제입니다.

+0

날씨 코드에 pack() 또는 not가 있으며 GUI가 범위를 벗어납니다. 왜 그 구성 요소들은 많은 공간을 차지합니까? – user3422747

+0

@ user3422747 둘 다 (!) 팩이없는 나를 위해, 프레임의 크기는 0입니다. 그러나 그렇습니다. 핵심 "문제"는 구성 요소가 많은 공간을 차지하려고한다는 것입니다. 왜 그것은 컴포넌트의 특성과'LayoutManager' (DesignGridLayout)에 달려 있습니다. 예를 들어'mainLayout'에 add를 써서 처음 두 라인을 비활성화 시키면'mainLayout.row(). center(). add (i5b1) .add (i5b2) .add (i5b3); '만 볼 수 있습니다. 이제는 프레임이 작습니다. 버튼이 요구하는 공간이기 때문입니다. – DSquare

+0

네, 실제로 mainpanel의 첫 번째 줄 2 개를 제거하면 추가 된 버튼 만 남아 있고 패널은 매우 작습니다. 사실, 왜 다른 컴포넌트들이 텍스트 필드와 같이 많은 공간을 차지하는 지 궁금합니다. – user3422747

관련 문제