我想以第一个面板位于两个并排面板顶部的方式为我的JFrame安排3个面板。 我无法截取屏幕截图,因为我在这个网站上只是新手。
这是我的程序的代码。只要问我在这里缺少什么。
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"));
}
});
}
}
答案 0 :(得分:2)
这可以解决您的问题。但请尝试使用正确的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);
答案 1 :(得分:1)
您需要使用LayoutManager。例如,BoxLayout可满足您的需求: Java Documenation about BoxLayout 这个网站也有一个例子,可以帮助你入门。因为您可能是LayoutManagers的新手。您应该使用最简单的LayoutManager:FlowLayout FlowLayout不是您问题的解决方案,但会帮助您理解LayoutManagers背后的概念。
编辑:之前我发布过GridLayout。但是你不能让Component产生多个单元格。所以GridLayout真的不是你想要的。