如何设置gridlayout jpanel的大小

时间:2012-07-25 17:07:03

标签: java swing layout grid-layout

我正在尝试设置gridlayout jpanel的大小。这是代码:

JFrame myFrame = new JFrame();    
    myFrame.setLayout(new FlowLayout());  
    myFrame.setLocation(400, 100);
    myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    JLabel jlMins = new JLabel("Number of minutes for tutoring session (should be a positive decimal number):  0.0");
    JLabel jlEarnings = new JLabel("Earnings in dollars and cents received (should be positive decimal number):  0.0");
    jtfMins = new JTextField(20); 
    jtfEarnings = new JTextField(20);
    JPanel jpMins = new JPanel(new BorderLayout());
    JPanel jpEarnings = new JPanel(new BorderLayout());
    jpMins.setPreferredSize(new Dimension(300,50));
    jpEarnings.setPreferredSize(new Dimension(300,50));
    jpMins.add(jlMins,BorderLayout.NORTH);
    jpMins.add(jtfMins,BorderLayout.CENTER);
    jpEarnings.add(jlEarnings,BorderLayout.NORTH);
    jpEarnings.add(jtfEarnings,BorderLayout.CENTER);
    JButton jbQuit = new JButton("Quit");
    JButton jbEnter = new JButton("Enter");
    JButton jbReport = new JButton("Run Report");
    jbQuit.setActionCommand("quit");
    jbEnter.setActionCommand("enter");
    jbReport.setActionCommand("report");
    jbQuit.addActionListener(this);
    jbEnter.addActionListener(this);
    jbReport.addActionListener(this);
    JPanel jpButtons = new JPanel(new GridLayout(4,1,0,20)); 
    jpButtons.setSize(new Dimension(50,150));
    jpButtons.add(jbEnter);  
    jpButtons.add(jbReport);
    jpButtons.add(jbQuit); 
    JPanel jpNorth = new JPanel(new BorderLayout());
    jpNorth.add(jpMins,BorderLayout.NORTH); 
    jpNorth.add(jpEarnings,BorderLayout.CENTER);
    jpNorth.add(jpButtons,BorderLayout.SOUTH);
    jtaReports = new JTextArea();
    jtaReports.setColumns(40);
    jtaReports.setRows(10);
    jtaReports.setLineWrap(true);
    JScrollPane jspReports = new JScrollPane(jtaReports);
    jspReports.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    JPanel jpSouth = new JPanel();
    jpSouth.setPreferredSize(new Dimension(350,200)); 
    jpSouth.add(jspReports); 
    JPanel jpMain = new JPanel(new BorderLayout()); 
    jpMain.add(jpNorth,BorderLayout.NORTH);
    jpMain.add(jpSouth,BorderLayout.SOUTH);
    jpMain.setPreferredSize(new Dimension(500,500));
    myFrame.setContentPane(jpMain);  
    myFrame.pack();
    myFrame.setVisible(true);  

面板名称为jpButtons。在上面的代码中,我主要谈论的是这一部分:

   JButton jbQuit = new JButton("Quit");
JButton jbEnter = new JButton("Enter");
JButton jbReport = new JButton("Run Report");
jbQuit.setActionCommand("quit");
jbEnter.setActionCommand("enter");
jbReport.setActionCommand("report");
jbQuit.addActionListener(this);
jbEnter.addActionListener(this);
jbReport.addActionListener(this);
JPanel jpButtons = new JPanel(new GridLayout(4,1,0,20)); 
jpButtons.setSize(new Dimension(50,150));
jpButtons.add(jbEnter);  
jpButtons.add(jbReport);
jpButtons.add(jbQuit); 

setSize和setPreferredSize究竟是如何工作的,或者如何让它们在jpanel,组件等上正常工作。

1 个答案:

答案 0 :(得分:4)

缩放和定位由布局管理器处理;让它发挥作用。