将两个JPanel添加到JFrame

时间:2017-02-11 21:24:24

标签: java swing

我试图将两个JPanel添加到Jframe中,但看起来它们看起来像一个。我试着像这张图片一样将它们叠在一起。

layout image

我想我可能需要查看布局管理器?我只需要朝着正确的方向微调一点。

package projectTwo;

import javax.swing.*;

public class checkFrame 
{
    public static void main (String[] args)
    {
       JFrame frame = new JFrame("Compose Message");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      checkPanel bob  = new checkPanel();
       //frame.add(bob);
       frame.getContentPane().add(bob);
       frame.setResizable(false);
       frame.setSize(750, 500);
       frame.setVisible(true);
   }

}
package projectTwo;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class checkPanel extends JPanel implements ActionListener
{
    private JPanel entry, display;
    private JLabel name, checkAmount, payOrderOf, numPrint, numWords;
    private JTextField nameT, checkAmountT;
    private JButton Submit;

   public checkPanel()
   {
       entryComponents();
        checkDisplay();
   }
    private void entryComponents(){

     name = new JLabel("Name:");
    checkAmount = new JLabel("Check Amount:");


    nameT = new JTextField(20);
    nameT.addActionListener(this);

    checkAmountT = new JTextField(20);
    checkAmountT.addActionListener(this);

    Submit = new JButton("Submit");
    Submit.addActionListener(this);


    add(name);
    add(nameT);
    add(checkAmount);
    add(checkAmountT);
    add(Submit);

    setPreferredSize(new Dimension(750, 75));
    setBackground(new Color(200,200,200));
}

private void checkDisplay(){
    payOrderOf = new JLabel("Pay to the Order of: ");


    add(payOrderOf);

    setBackground(new Color(220,255,225));
}

public void actionPerformed (ActionEvent event)
    {

    }
}

2 个答案:

答案 0 :(得分:0)

使用Gridbag Layout可以帮助你很多我会在面板之间放置一个分隔符

答案 1 :(得分:0)

你一定要看一下布局管理器。目前,您只是简单地将JPanels添加到彼此,而没有任何规范它们应该是什么。

在这种情况下,您有几个选择。您可以使用GridLayout,但这会导致所有面板的大小相同。如果您只想让两个面板彼此相同,我建议您使用BorderLayout。我调整了你的代码如下:

public class checkPanel extends JPanel implements ActionListener
{
private JPanel entry, display;
private JLabel name, checkAmount, payOrderOf, numPrint, numWords;
private JTextField nameT, checkAmountT;
private JButton Submit;

public checkPanel()
{
    this.setPreferredSize(new Dimension(750, 75));
    entryComponents();
    checkDisplay();

    this.setLayout(new BorderLayout());
    this.add(entry, BorderLayout.NORTH);
    this.add(display, BorderLayout.CENTER);
}
private void entryComponents(){

    entry = new JPanel();
    // You should specify entry's layout as well FlowLayout are used by default

    name = new JLabel("Name:");
    checkAmount = new JLabel("Check Amount:");


    nameT = new JTextField(20);
    nameT.addActionListener(this);

    checkAmountT = new JTextField(20);
    checkAmountT.addActionListener(this);

    Submit = new JButton("Submit");
    Submit.addActionListener(this);


    entry.add(name);
    entry.add(nameT);
    entry.add(checkAmount);
    entry.add(checkAmountT);
    entry.add(Submit);


    entry.setBackground(new Color(200,200,200));
}

private void checkDisplay(){

    display = new JPanel();
    // You should specify display's layout as well FlowLayout are used by       default

    payOrderOf = new JLabel("Pay to the Order of: ");


    display.add(payOrderOf);

    display.setBackground(new Color(220,255,225));
}
@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}

}

为您创建的每个JPanel分配布局通常是个好主意。布局的选择取决于面板应如何运作。