我想以第一个面板位于两个并排面板顶部的方式将3Panels排列到我的JFrame中。
我不能放截图,因为我只是这个网站的新手。

这是我程序的代码。只是问我这里是否缺少什么。

package images;
import javax.swing.*;

import java.awt.Event.*;
import java.awt.*;

public class B1 extends JFrame{

public static final long serialVersionUID = 0L;

public static void main(String[] args){

    new Display();

}

}

class Display extends JFrame{

public static final long serialVersionUID = 0L;

private JButton Bnum1,Bnum2,Bnum3,Bnum4,Bnum5,Bnum6,Bnum7,Bnum8,Bnum9,Bnum0,clear,enter;
private JLabel Laccount,Lpin,Lcash,Ltake;
private JTextField Taccount, Tpin, Tcash,Ttake;

public Display(){

    JFrame Frame = new JFrame();

    Frame.setTitle("ATM Graphic User Interface");
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.setVisible(true);
    Frame.setSize(600, 400);


    JPanel buttons = new JPanel(new GridLayout(4,4));
    Frame.add(buttons);
    buttons.setVisible(true);
    buttons.setBounds(40, 100, 200, 200);

    JPanel upper = new JPanel(new GridLayout(2,2));
    Frame.add(upper);
    upper.setVisible(true);
    upper.setBounds(40, 20, 350, 50);
    upper.setBackground(Color.cyan);

    JPanel lower = new JPanel(new GridLayout(2,2));
    Frame.add(lower);
    lower.setVisible(true);
    lower.setBounds(270, 100,300, 50);;
    lower.setBackground(Color.CYAN);


    Bnum1 = new JButton("1");
    Bnum2 = new JButton("2");
    Bnum3 = new JButton("3");
    Bnum4 = new JButton("4");
    Bnum5 = new JButton("5");
    Bnum6 = new JButton("6");
    Bnum7 = new JButton("7");
    Bnum8 = new JButton("8");
    Bnum9 = new JButton("9");
    Bnum0 = new JButton("0");
    clear = new JButton("CLEAR");
    enter = new JButton("ENTER");
    Laccount = new JLabel("Account Number  : ");
    Lpin = new JLabel("Pin Number           :");
    Lcash = new JLabel("Transfer Cash");
    Ltake = new JLabel("Take ENV");
    Taccount = new JTextField(15);
    Tpin = new JTextField(15);
    Tcash = new JTextField(5);
    Ttake = new JTextField(5);

    clear.setMargin(new Insets(2,2,2,2));
    enter.setMargin(new Insets(2,2,2,2));

    buttons.add(Bnum1);
    buttons.add(Bnum2);
    buttons.add(Bnum3);
    buttons.add(Bnum4);
    buttons.add(Bnum5);
    buttons.add(Bnum6);
    buttons.add(Bnum7);
    buttons.add(Bnum8);
    buttons.add(Bnum9);
    buttons.add(Bnum0);
    buttons.add(clear);
    buttons.add(enter);
    upper.add(Laccount);
    upper.add(Taccount);
    upper.add(Lpin);
    upper.add(Tpin);
    lower.add(Lcash);
    lower.add(Tcash);
    lower.add(Ltake);
    lower.add(Ttake);



    Bnum1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            Bnum1.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            Bnum1.setBackground(UIManager.getColor("control"));
        }
    });
    Bnum2.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            Bnum2.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            Bnum2.setBackground(UIManager.getColor("control"));
        }
    });
    Bnum3.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            Bnum3.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            Bnum3.setBackground(UIManager.getColor("control"));
        }
    });
    Bnum4.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            Bnum4.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            Bnum4.setBackground(UIManager.getColor("control"));
        }
    });
    Bnum5.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            Bnum5.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            Bnum5.setBackground(UIManager.getColor("control"));
        }
    });
    Bnum6.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            Bnum6.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            Bnum6.setBackground(UIManager.getColor("control"));
        }
    });
    Bnum7.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            Bnum7.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            Bnum7.setBackground(UIManager.getColor("control"));
        }
    });
    Bnum8.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            Bnum8.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            Bnum8.setBackground(UIManager.getColor("control"));
        }
    });
    Bnum9.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            Bnum9.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            Bnum9.setBackground(UIManager.getColor("control"));
        }
    });
    Bnum0.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            Bnum0.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            Bnum0.setBackground(UIManager.getColor("control"));
        }
    });
    clear.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            clear.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            clear.setBackground(UIManager.getColor("control"));
        }
    });
    enter.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            enter.setBackground(Color.cyan);
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            enter.setBackground(UIManager.getColor("control"));
        }
    });










}


}

最佳答案

这样可以解决您的问题。但是尝试使用正确的LayoutManager

添加此行

Frame.setLayout(new BorderLayout());


更换

Frame.add(buttons);
Frame.add(upper);
Frame.add(lower);


通过

Frame.add(buttons,BorderLayout.NORTH);
Frame.add(upper,BorderLayout.WEST);
Frame.add(lower,BorderLayout.EAST);

09-29 23:46