gpt4 book ai didi

java - 尝试将多个 JPanel 添加到一个主 JPanel 中并设置边框布局

转载 作者:行者123 更新时间:2023-11-29 05:05:10 25 4
gpt4 key购买 nike

我目前正在构建一台 ATM 机,但在设置多个 JPanel 的边框布局时遇到了问题。基本上我想让我的照片看起来像这样

但是当我运行它时它看起来像这样:

这是我尝试执行此操作的代码。

public Main() {

try {
atmPicture = ImageIO.read(new File("atmpicture.jpg"));
welcomeScreen = ImageIO.read(new File("Welcome Screen.jpg"));
enterAccScreen = ImageIO.read(new File("Account Number.jpg"));
mainButtons = ImageIO.read(new File("Main Screen.jpg"));
withdrawScreen = ImageIO.read(new File("R2 click.jpg"));
depositScreen = ImageIO.read(new File("R3 click.jpg"));
transferScreen = ImageIO.read(new File("R4 click.jpg"));
accInfoScreen = ImageIO.read(new File("L1 Click.jpg"));
withdrawScreen2 = ImageIO.read(new File("R2.2 Click.jpg"));
} catch (IOException ex) {
System.out.println("The file cannot read2");
System.exit(1);
}


panel1 = new JPanel(new GridLayout(4, 3)); //numpad
panel2 = new JPanel(new GridLayout(3, 1)); //left three buttons
panel3 = new JPanel(new GridLayout(3, 1)); // right three buttons
panelMain = new JPanel(new GridLayout(0, 3)); // textfield


jtf.setHorizontalAlignment(SwingConstants.LEFT);
jtf.setPreferredSize(new Dimension(10, 10));
initButtons();
}

/**
* Initializes all the buttons and adds them to the
*/
private void initButtons() {

panel1.add(b1);
panel1.add(b2);
panel1.add(b3);
panel1.add(b4);
panel1.add(b5);
panel1.add(b6);
panel1.add(b7);
panel1.add(b8);
panel1.add(b9);
panel1.add(ba);
panel1.add(b0);
panel1.add(bp);

panel2.add(l1);
panel2.add(l2);
panel2.add(l3);

panel3.add(r1);
panel3.add(r2);
panel3.add(r3);

Database cData = new Database();
cData.storeData();

ButtonListener listener = new ButtonListener(cData);

b1.addActionListener(listener);
b2.addActionListener(listener);
b3.addActionListener(listener);
b4.addActionListener(listener);
b5.addActionListener(listener);
b6.addActionListener(listener);
b7.addActionListener(listener);
b8.addActionListener(listener);
b9.addActionListener(listener);
b0.addActionListener(listener);
ba.addActionListener(listener);
bp.addActionListener(listener);
l1.addActionListener(listener);
l2.addActionListener(listener);
l3.addActionListener(listener);
r1.addActionListener(listener);
r2.addActionListener(listener);
r3.addActionListener(listener);

setLayout(new BorderLayout());


panelMain.add(panel1, BorderLayout.PAGE_END);
panelMain.add(panel2, BorderLayout.LINE_START);
panelMain.add(panel3, BorderLayout.LINE_END);
panelMain.add(jtf, BorderLayout.CENTER);

add(panelMain);

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {

Image upper = null;
Image right = null;
Image left = null;
Image bottom = null;
//Image middle = null;
JFrame frame = new JFrame();
try {
upper = ImageIO.read(new File("upper portion.jpg"));
right = ImageIO.read(new File("right portion.jpg"));
left = ImageIO.read(new File("left portion.jpg"));
bottom = ImageIO.read(new File("bottom portion.jpg"));
//middle = ImageIO.read(new File("middle portion.jpg"));
} catch (IOException ex) {
System.out.println("The file cannot read");
System.exit(1);
}

JLabel labelTop = new JLabel(new ImageIcon(upper));
JLabel labelRight = new JLabel(new ImageIcon(right));
JLabel labelLeft = new JLabel(new ImageIcon(left));
JLabel labelBottom = new JLabel(new ImageIcon(bottom));
//JLabel labelMiddle = new JLabel(new ImageIcon(middle));

labelTop.setLayout(new BorderLayout());
labelRight.setLayout(new BorderLayout());
labelLeft.setLayout(new BorderLayout());
labelBottom.setLayout(new BorderLayout());
//labelMiddle.setLayout(new BorderLayout());

frame.add(labelTop, BorderLayout.PAGE_START);
frame.add(labelLeft, BorderLayout.LINE_START);
frame.add(labelRight, BorderLayout.LINE_END);
frame.add(labelBottom, BorderLayout.PAGE_END);
frame.add(new Main(), BorderLayout.CENTER);

frame.setSize(1041, 1200);
frame.setVisible(true);
}
});

}

最佳答案

panelMain = new JPanel(new GridLayout(0, 3)); 

您将 panelMain 的布局设置为 GridLayout();

panelMain.add(panel1, BorderLayout.PAGE_END);
panelMain.add(panel2, BorderLayout.LINE_START);
panelMain.add(panel3, BorderLayout.LINE_END);
panelMain.add(jtf, BorderLayout.CENTER);

但随后您将 panelMain 视为 BorderLayout。您不能随意使用 BorderLayout 约束。

如果您希望 panelMain 使用 BorderLayout,则使用 BorderLayout 创建它:

panelMain = new JPanel(new BorderLayoutLayout()); 

关于java - 尝试将多个 JPanel 添加到一个主 JPanel 中并设置边框布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30603461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com