我想在java中創建一個JPanel
,其佈局類似於下面的佈局。有任何想法嗎?哪個layoutmanager使用?
2
A
回答
8
使用垂直BoxLayout並設置在各行項目的定位。 (使每一行都是自己的JPanel
)
另一種選擇是使用SpringLayout
或GridBoxLayout
以及每個縮進的設置間距。
2
看看miglayout。有了這個佈局管理器,你將永遠不會有問的具體情況使用的佈局管理器:-)
1
這是使用jgoodies forms的FormLayout(庫從谷歌)的實現:
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import javax.swing.*;
import java.awt.*;
public class FormSample {
private static JPanel generatePanel() {
FormLayout layout = new FormLayout(
"3dlu, 15dlu, max(50dlu;p), 2dlu, p, 3dlu", // column definition
"5dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 5dlu" // row definition
);
JPanel panel = new JPanel(layout);
CellConstraints cc = new CellConstraints();
int row = 0;
panel.add(new JLabel("Amplitude"), cc.xyw(2, row * 4 + 2, 4));
panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
panel.add(new JLabel("V"), cc.xy(5, row * 4 + 4));
row++;
panel.add(new JLabel("Off-set"), cc.xyw(2, row * 4 + 2, 4));
panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
panel.add(new JLabel("V"), cc.xy(5, row * 4 + 4));
row++;
panel.add(new JLabel("Frequentie"), cc.xyw(2, row * 4 + 2, 4));
panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
panel.add(new JLabel("Hz"), cc.xy(5, row * 4 + 4));
row++;
panel.add(new JLabel("Fase"), cc.xyw(2, row * 4 + 2, 4));
panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
panel.add(new JLabel("rad"), cc.xy(5, row * 4 + 4));
return panel;
}
public static void main(String[] args) {
// create frame
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add content panel
frame.setContentPane(generatePanel());
// shring/expand to optimal size
frame.pack();
// center frame
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension resolution = toolkit.getScreenSize();
Dimension frameSize = frame.getSize();
int xLocation = resolution.width/2 - frameSize.width/2;
int yLocation = resolution.height/2 - frameSize.height/2;
frame.setLocation(xLocation, yLocation);
// show frame
frame.setVisible(true);
}
}
-1
的GroupLayout是NetBeans中的GUI設計工具的默認佈局管理器(以及其他一些IDE,如果我沒有弄錯的話)。
0
我用GroupLayout
管理器創建了一個示例。縮進 組件使用LayoutStyle.ComponentPlacement.INDENT
組件放置類型創建。
package com.zetcode;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
public class GroupLayoutIndent extends JFrame {
public GroupLayoutIndent() {
initUI();
setTitle("Indents");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
JLabel ampLbl = new JLabel("Amplitude");
JLabel offLbl = new JLabel("Off-set");
JLabel freqLbl = new JLabel("Frequency");
JLabel phsLbl = new JLabel("Phase");
JLabel unit1 = new JLabel("V");
JLabel unit2 = new JLabel("V");
JLabel unit3 = new JLabel("Hz");
JLabel unit4 = new JLabel("rad");
JTextField field1 = new JTextField(12);
JTextField field2 = new JTextField(12);
JTextField field3 = new JTextField(12);
JTextField field4 = new JTextField(12);
gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup()
.addComponent(ampLbl)
.addGroup(gl.createSequentialGroup()
.addPreferredGap(ampLbl, field1,
LayoutStyle.ComponentPlacement.INDENT)
.addComponent(field1)
.addComponent(unit1))
.addComponent(offLbl)
.addGroup(gl.createSequentialGroup()
.addPreferredGap(offLbl, field2,
LayoutStyle.ComponentPlacement.INDENT)
.addComponent(field2)
.addComponent(unit2))
.addComponent(freqLbl)
.addGroup(gl.createSequentialGroup()
.addPreferredGap(freqLbl, field3,
LayoutStyle.ComponentPlacement.INDENT)
.addComponent(field3)
.addComponent(unit3))
.addComponent(phsLbl)
.addGroup(gl.createSequentialGroup()
.addPreferredGap(phsLbl, field4,
LayoutStyle.ComponentPlacement.INDENT)
.addComponent(field4)
.addComponent(unit4))
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(ampLbl)
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(field1)
.addComponent(unit1))
.addComponent(offLbl)
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(field2)
.addComponent(unit2))
.addComponent(freqLbl)
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(field3)
.addComponent(unit3))
.addComponent(phsLbl)
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(field4)
.addComponent(unit4))
);
gl.linkSize(field1, field2, field3, field4);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
GroupLayoutIndent ex = new GroupLayoutIndent();
ex.setVisible(true);
}
});
}
}
相關問題
- 1. 我應該使用哪個LayoutManager?
- 2. 我應該爲我的Java GUI使用哪個LayoutManager?
- 3. 不使用JTree沒有LayoutManager
- 4. 使用requirejs加載LayoutManager中的問題
- 5. LayoutManager的建議
- 6. 保存LayoutManager
- 7. RecyclerView autofit LayoutManager
- 8. LayoutManager不會滾動到firstCompletelyVisibleItem
- 9. JFrame的LayoutManager的contentPane的
- 10. 錯開的Grid LayoutManager結束
- 11. 如何將另一個LayoutManager應用於JComboBox? (多列JComboBox嘗試)
- 12. 使用哪個「diagnostics.wadcfg」?
- 13. 無法使用LayoutManager恢復RecyclerView中的滾動位置
- 14. 爲什麼我的JScrollPane在使用空的LayoutManager時顯示JTextArea?
- 15. 如何檢查什麼LayoutManager是RecyclerView使用?
- 16. System.Configuration.ConfigurationManager.AppSettings使用哪個文件?
- 17. 要使用哪個SmtpStatusCodes?
- 18. XMPP使用哪個端口?
- 19. 要使用哪個CMS?
- 20. MySQL:使用哪個API?
- 21. AssetManager - 使用哪一個
- 22. 哪個Catch Execption要使用?
- 23. 何時使用哪個()?
- 24. 使用哪個wordpress插件?
- 25. GWT使用哪個JavaScript庫?
- 26. OpenID使用哪個URL?
- 27. Eclipse(Helios)使用哪個keystore?
- 28. strlen,mb_strlen,哪個使用?
- 29. rack.session.options - 何時使用哪個?
- 30. 要使用哪個endpointscfg.py?
我的回答標題的所有問題將是['MiGLayout'(http://www.miglayout.com/);-) – 2011-04-22 12:04:40
或JGoodies數據的FormLayout。 http://www.jgoodies.com/freeware/forms/ – MeBigFatGuy 2011-04-22 12:09:16
或訪問[本地商店](http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html)。 – trashgod 2011-04-22 12:24:17