亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

展開 J文本區域在網格包布局

展開 J文本區域在網格包布局

一只斗牛犬 2022-09-28 14:45:40
我從來沒有使用過網格袋布局,但我相信它可能是別的東西。目前,我正在嘗試添加一個 TextArea,以充當正在執行的任何代碼的控制臺視口窗口。就目前而言,文本區域很小,實際上看不到任何文本。我嘗試查閱此頁面以獲取其中一個建議,但我仍然無法正確顯示JTextArea。代碼粘貼在下面,如果您需要其他任何內容,請告訴我。我還附上了一張額外的圖片,顯示了它目前正在做什么。編輯:設置(); 是一個空的正文。@SuppressWarnings("serial")public abstract class MainComponent extends JPanel {protected String componentName = "DEMO";private JLabel title;private GridBagConstraints gbc;private Insets spacing; private Font buttonFont;/* * Redirection manipulation for the project. */private JTextArea localConsole;public MainComponent(Dimension dim) {    setup();    /* Set main body */    setLayout(new GridBagLayout());    gbc = new GridBagConstraints();    spacing = new Insets(100,5,100,5);    buttonFont = new Font("Consolas", Font.ITALIC, 22);    /* Set title */    title = new JLabel(componentName);    title.setFont(new Font("Consolas", Font.BOLD, 48));    gbc.fill = GridBagConstraints.CENTER;    gbc.gridx = 1;    gbc.gridy = 0;    add(title, gbc);    JButton run = new JButton("Run Project");    run.setFont(buttonFont);    gbc.fill = GridBagConstraints.HORIZONTAL;    gbc.insets = spacing;    gbc.gridx = 0;    gbc.gridy = 2;    add(run, gbc);    JButton open = new JButton("Open Codebase");    open.setFont(buttonFont);    gbc.fill = GridBagConstraints.HORIZONTAL;    gbc.insets = spacing;    gbc.gridx = 1;    gbc.gridy = 2;    add(open, gbc);    JButton exit = new JButton("Exit Program");    exit.setFont(buttonFont);    gbc.fill = GridBagConstraints.HORIZONTAL;    gbc.insets = spacing;    gbc.gridx = 2;    gbc.gridy = 2;    add(exit, gbc);}
查看完整描述

1 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

給我們一個它目前所做的事情的圖片并沒有真正的幫助,因為我們不知道你想要實現什么,很難提出一個確切的建議。

因此,所看到的是試圖:

  1. 在頂部顯示 3 個按鈕

  2. 讓文本區域填充底部的剩余空間

因此,我建議您永遠不需要使用單個布局管理器。很多時候,布局管理器的組合更容易。

所以我建議你:

  1. 使用邊界布局作為主布局。

  2. 為按鈕創建一個面板,并添加到邊框布局Page_START

  3. 將滾動窗格添加到“邊界布局”中。CENTER

所以基本代碼是:

setLayout( new BorderLayout() );


JPanel buttonsPanel = new JPanel();

buttonsPanel.add(run);

buttonsPane.add((open);

buttonsPanel.add(exit);

add(buttonsPanel, BorderLayout.PAGE_START);


JTextArea textArea = new JTextArea(10, 30); // give text area a default preferred size

add(new JScrollPane(textArea), BorderLayout.CENTER);

比嘗試使用網格包布局更簡單,代碼更少。


但是,如果您想練習使用網格包布局,請首先閱讀有關如何使用網格包布局的 Swing 教程,以獲取有關約束的更多信息。


基本代碼可能如下所示:


gbc.gridx = 0;

gbc.gridy = 0;

add(run, gbc);


gbc.gridx = 1;

gbc.gridy = 0;

add(open, gbc);


gbc.gridx = 2;

gbc.gridy = 0;

add(exit, gbc);


localConsole = new JTextArea(5, 20);

JScrollPane scrollPane = new JScrollPane(localConsole);


gbc.fill = GridBagConstraints.CENTER;

gbc.gridx = 0;

gbc.gridy = 1;

gbc.gridwidth = 3; // try this with 1 and 2 to see the difference.

add(scrollPane, gbc);

也就是說,您不能只是隨機使用網格x/網格寬度/網格高度值。


組件需要顯示在網格中。你不能在網格中留下間隙。


因此,如果您希望按鈕排成一行,則需要為相同的網格值按順序遞增 gridx。


如果需要按鈕下方的文本區域,請按順序遞增網格,并從新的 gridx 值開始。


查看完整回答
反對 回復 2022-09-28
  • 1 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號