[英]Adding a JPanel into a BorderLayout of a JApplet
我在將此JPanel添加到窗格的Center塊時遇到問題。 本質上,此主窗口是BorderLayout,其Center位於Center的中心,而東西方的塊將是單獨的BorderLayouts。 我已經搜索了這個問題,並瀏覽了我教授的示例代碼以及關於stackoverflow的示例代碼,但是在我的代碼中找不到該問題。
我在Eclipse中進行所有編碼,因此我使用了集成的AppletViewer。 唯一出現的是一個空的灰色框,我希望在其中看到包含JLabels和JTextAreas的centerPanel。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lab7 extends JApplet implements ItemListener, ActionListener{
//variables
private JLabel currentOrder, total;
private JTextArea descTop, currentOrderTA, totalTA;
private JRadioButton sizeSmall, sizeMedium, sizeLarge, typeDeep, typePan, typeHand;
private JCheckBox pepperoni, bacon, extra_cheese, mushroom, pepper, sausage, tomato, olive;
private JButton orderButton, resetButton;
private ButtonGroup sizeBG, typeBG;
private BorderLayout borderLayoutMain, borderLayoutWest, borderLayoutEast;
private JPanel westPanel, centerPanel, eastPanel;
public void init(){
Container pane = getContentPane();
pane.setLayout(borderLayoutMain);
//borderLayoutMain centerPanel
centerPanel = new JPanel();
centerPanel.setLayout(null);
currentOrder.setSize(200, 25);
currentOrder.setLocation(100, 25);
currentOrderTA.setSize(600, 400);
currentOrderTA.setLocation(100, 50);
currentOrderTA.setEditable(false);
total.setSize(200, 25);
totalTA.setLocation(100, 450);
totalTA.setEditable(false);
orderButton.setSize(100, 50);
orderButton.setLocation(100, 500);
resetButton.setSize(100, 50);
resetButton.setLocation(400, 500);
centerPanel.add(currentOrder);
centerPanel.add(currentOrderTA);
centerPanel.add(total);
centerPanel.add(totalTA);
centerPanel.add(orderButton);
centerPanel.add(resetButton);
pane.add(centerPanel, BorderLayout.CENTER);
}
由於您已經取消了布局管理器( centerPanel.setLayout(null);
),因此BorderLayout
不再知道面板的大小是多少(因為容器的首選大小由其布局確定)經理)
考慮在centerPanel
上使用布局管理器,有關更多詳細信息,請參見在容器中布置組件 。
避免使用null
布局,像素完美布局是現代ui設計中的一種幻覺。 有太多因素會影響組件的單個大小,您無法控制。 Swing旨在與布局經理為核心一起工作,舍棄這些問題不會導致問題和問題的終結,您將花費越來越多的時間來嘗試糾正
請參閱為什么在SWING中使用空布局會讓人皺眉? 更多細節
更新...
你打電話時
pane.setLayout(borderLayoutMain);
borderLayoutMain
為null
,因此您現在在頂級容器中具有null
布局。
嘗試使用...
pane.setLayout(new BorderLayout());
代替...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.