是否有任何方式將JLabels和JTextField僅添加到中間邊界佈局的下半部分。出於美學原因,我不想在上半部分做任何事情,也沒有找到任何解決方法。我仍然是相當新的使用擺動和AWT ...任何建議將不勝感激。我添加了一張我正在努力實現的圖片。如何將JLabel和JTextField添加到BorderLayout的中心底部?
回答
給出BorderLayout.CENTER
JPanel a new GridLayout(2, 1)
(對於2行,1列)。爲上半部分的空白空間添加一個空白的JLabel到相同的JPanel。將另一個組件JPanel添加到JPanel的BorderLayout.CENTER
,並使用GridBagLayout作爲底部空間,然後使用GridBagConstraints添加標籤和字段。這裏的祕訣是嵌套JPanels,每個都使用選擇的佈局管理器。
例如:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.*;
public class Layouts {
private static final int TF_COLS = 8;
private static void createAndShowGui() {
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.setBorder(BorderFactory
.createTitledBorder("BorderLayout Panel"));
borderLayoutPanel.add(createInnerPanel("North"), BorderLayout.PAGE_START);
borderLayoutPanel.add(createInnerPanel("North"), BorderLayout.PAGE_START);
borderLayoutPanel.add(createInnerPanel("South"), BorderLayout.PAGE_END);
borderLayoutPanel.add(createInnerPanel("East"), BorderLayout.LINE_END);
borderLayoutPanel.add(createInnerPanel("West"), BorderLayout.LINE_START);
JPanel centerPanel = new JPanel(new GridLayout(2, 1));
centerPanel.add(new JLabel());
JPanel gblPanel = new JPanel(new GridBagLayout());
gblPanel.add(new JLabel("Question 1:"), createGbc(0, 0));
gblPanel.add(new JTextField(TF_COLS), createGbc(1, 0));
gblPanel.add(new JLabel("Question 2:"), createGbc(0, 2));
gblPanel.add(new JTextField(TF_COLS), createGbc(1, 2));
centerPanel.add(gblPanel);
borderLayoutPanel.add(centerPanel);
JFrame frame = new JFrame("Layouts");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(borderLayoutPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private static GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
int iGap = 5;
gbc.insets = (x == 0) ? new Insets(iGap, iGap, iGap, 3 * iGap) : new Insets(
iGap, iGap, iGap, iGap);
return gbc;
}
private static JComponent createInnerPanel(String text) {
JPanel panel = new JPanel();
panel.add(new JLabel(text));
panel.setBorder(BorderFactory.createEtchedBorder());
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
是的,這是比簡單的設置範圍更最初的工作,但如果你最終節省時間和挫折是當你有後來因爲如果你使用正確的保持這種GUI佈局,調試,增強或改變GUI會更容易。它也將有更好的機會在所有平臺和屏幕分辨率上體面。
我通常使用一套界限,但隨時看其他佈局一樣,它允許你做出這樣的佈局,你可以把特定組件在網格中的GridBagLayout。看看這個鏈接,看看是什麼好像。 https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
酷,我會檢查了這一點。謝謝! –
*「我通常使用set bounds ..」*答案不應包含那個(壞)建議,更不用說從頭開始。 –
那麼我確實建議網格袋佈局作爲更好的選擇... – Raheel138
這是很容易與EmptyBorder
完成:
centerPanel.setBorder(new EmptyBorder(top, left, bottom, right));
- 1. 在BorderLayout中對齊JLabel和JTextField
- 2. 將圖像添加到borderlayout中心
- 3. 在JPanel上添加JLabel - BorderLayout
- 4. 如何將文字添加到圖例下的圖例底部中心和圖表底部中心?
- 5. 將頂部和底部添加到IFRAME
- 6. 將組件添加到BorderLayout
- 7. 將JPanel添加到JApplet的BorderLayout中
- 8. 如何將JTextField添加到JFrame的MenuBar?
- 9. 將JLabel添加到JFrame中
- 10. 將外部URL添加到JLabel
- 11. 如何在Jlabel頂部使用Jtextfield?
- 12. 中心和別針底部的底部
- 13. 如何將陰影添加到uiview的底部和右側?
- 14. setAlignmentY未將JLabel居中放在BorderLayout中
- 15. 重疊JTextField和JLabel
- 16. 如何將快捷鍵添加到JTextField?
- 17. Java:如何將圖像添加到Jlabel?
- 18. 如何將文本添加到JLabel
- 19. 如何將JLabel添加到JEditorPane?
- 20. 如何將固定按鈕和edittext添加到屏幕底部
- 21. 如何將JLabel文本放在JTextField上
- 22. 將標籤添加到JComboBox和JTextField
- 23. 將數組添加到JTextField
- 24. 將JTextField添加到OverlayLayout
- 25. 如何將按鈕添加到extjs中的底部欄?
- 26. 我如何一起使用JTextField和JLabel?
- 27. 將ImageIcon添加到JLabel?
- 28. Java將ImageIcon添加到JLabel
- 29. 將JLabel添加到JPanel
- 30. 將行添加到表的底部
太棒了!我看到你在說什麼,所以我可以在我的'BorderLayout.CENTER'裏面有一個'GridLayout'? –
@ScruffyNerfherder:例如 - 請參閱代碼。 –
好的,太棒了!非常感謝你,我想我明白了! –