下面列出了怎么用java.awt.GridBagLayout的API类实例代码及写法,或者点击链接到github查看源代码。
private void initComponents(String text, Font font) {
setBorder(BorderFactory.createEmptyBorder());
setLayout(new GridBagLayout());
setOpaque(false);
label = new JLabel(text);
label.setForeground(getForeground());
if (font != null) label.setFont(font);
else label.setFont(label.getFont().deriveFont(Font.BOLD));
GridBagConstraints c1 = new GridBagConstraints();
c1.weighty = 1d;
add(label, c1);
GridBagConstraints c2 = new GridBagConstraints();
c2.weightx = 1d;
c2.fill = GridBagConstraints.HORIZONTAL;
c2.insets = new Insets(1, 4, 0, 0);
add(new SeparatorLine(), c2);
}
private void addLabeledComponentToGBL( String name,
JComponent c,
GridBagLayout gbl,
GridBagConstraints gbc,
Container target ) {
LabelV2 l = new LabelV2( name );
GridBagConstraints gbcLabel = (GridBagConstraints) gbc.clone();
gbcLabel.insets = new Insets( 2, 2, 2, 0 );
gbcLabel.gridwidth = 1;
gbcLabel.weightx = 0;
if ( c == null )
c = new JLabel( "" );
gbl.setConstraints( l, gbcLabel );
target.add( l );
gbl.setConstraints( c, gbc );
target.add( c );
}
private void initSystemPanel() {
// systems are the hard part, because these are all unit specific
// lets start with a mech
panSystem = new JPanel(new GridBagLayout());
panSystem.setBorder(BorderFactory.createTitledBorder(Messages.getString("UnitEditorDialog.system")));
if (entity instanceof Mech) {
setupMechSystemPanel();
} else if (entity instanceof VTOL) {
setupVtolSystemPanel();
} else if (entity instanceof Tank) {
setupTankSystemPanel();
} else if (entity instanceof Aero) {
setupAeroSystemPanel();
} else if (entity instanceof Protomech) {
setupProtoSystemPanel();
}
}
public UserAccountPanel() {
// Create controls
hyperlink = new Hyperlink();
hyperlink.setText("");
hyperlink.setActionCommand(CMD_SIGN_OUT);
accountLabel = new JLabel();
serverLabel = new JLabel();
iconPanel = new IconPanel();
// Layout controls
setLayout(new GridBagLayout());
SwingHelper.addToGridBag(this, iconPanel, 0, 0, 1, 2, 0, 4);
SwingHelper.addToGridBag(this, serverLabel, 1, 0, 1, 1, 0, 4);
SwingHelper.addToGridBag(this, accountLabel, 1, 1, 1, 1, 0, 4);
SwingHelper.addToGridBag(this, hyperlink, 1, 2, 1, 1, 5, 4);
setWindowsAccount(windowsAccount);
setPreferredSize(new Dimension(64, 32));
}
private JPanel createPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.gridx = 0;
createSection(getBorderSection(), "LBL_Border", panel, c); // NOI18N
createSection(getTitleSection(), "LBL_Header_Footer", panel, c); // NOI18N
createSection(getTextSection(), "LBL_Text", panel, c); // NOI18N
createSection(getZoomSection(), "LBL_Zoom", panel, c); // NOI18N
updateControl();
return panel;
}
private void addLabeledComponentToGBL( String name,
JComponent c,
GridBagLayout gbl,
GridBagConstraints gbc,
Container target ) {
LabelV2 l = new LabelV2( name );
GridBagConstraints gbcLabel = (GridBagConstraints) gbc.clone();
gbcLabel.insets = new Insets( 2, 2, 2, 0 );
gbcLabel.gridwidth = 1;
gbcLabel.weightx = 0;
if ( c == null )
c = new JLabel( "" );
gbl.setConstraints( l, gbcLabel );
target.add( l );
gbl.setConstraints( c, gbc );
target.add( c );
}
public TabHeader( TabButton ... buttons ) {
super( new GridBagLayout() );
setOpaque(false);
JPanel panelButtons = new JPanel( new GridLayout( 1, 0 ) );
panelButtons.setOpaque( false );
for( int i=0; i<buttons.length; i++ ) {
TabButton btn = buttons[i];
btn.setBorder(new TabBorder( i == buttons.length-1 ));
panelButtons.add( btn );
}
add( Logo.createNetBeansLogo(), new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5,12,5,12), 0, 0) );
add( new JLabel(), new GridBagConstraints( 1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0) );
add( panelButtons, new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 0, 0) );
add( new JLabel(), new GridBagConstraints( 3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0) );
add( showNextTime, new GridBagConstraints( 4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(15,12,15,12), 0, 0) );
}
private void init() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.0;
add(new JButton(createBillingAccount), gbc.clone());
gbc.weightx = 1.0;
add(new JButton(updateBillingAccount), gbc.clone());
gbc.weightx = 1.0;
add(new JButton(delete), gbc.clone());
gbc.anchor = GridBagConstraints.EAST;
add(new JButton(refresh), gbc.clone());
gbc.weightx = 0.0;
add(new JButton(save), gbc.clone());
}
public CustomPayloadsOptionsPanel() {
this.tableModel = new CustomPayloadMultipleOptionsTableModel();
this.tablePanel = new CustomPayloadsMultipleOptionsTablePanel(tableModel);
this.setName(OPTIONS_TITLE);
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.weightx = 1.0;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.BOTH;
this.add(new JLabel(OPTIONS_TITLE), gbc);
gbc.weighty = 1.0;
this.add(tablePanel, gbc);
}
ViewModelListener(
String viewType,
JComponent view,
JComponent buttonsPane,
String propertiesHelpID,
Image viewIcon
) {
this.viewType = viewType;
this.view = view;
this.buttonsPane = buttonsPane;
buttonsPane.setLayout(new GridBagLayout());
this.propertiesHelpID = propertiesHelpID;
this.viewIcon = viewIcon;
initView();
setUp();
}
/**
* Creates either a {@link LinkLocalButton} or a {@link LinkRemoteButton} that executes the
* action stored in type.
*
* @param type
* the type
*/
public LinkButtonValueCellEditor(final ParameterTypeLinkButton type) {
super(new JTextField());
this.container = new JPanel(new GridBagLayout());
this.container.setToolTipText(type.getDescription());
GridBagConstraints gbc = new GridBagConstraints();
if (type.isLocalAction()) {
linkButton = new LinkLocalButton(type.getAction());
} else {
linkButton = new LinkRemoteButton(type.getAction());
}
gbc.gridx += 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
container.add(linkButton, gbc);
}
private void addLabeledComponentToGBL( String name,
JComponent c,
GridBagLayout gbl,
GridBagConstraints gbc,
Container target ) {
LabelV2 l = new LabelV2( name );
GridBagConstraints gbcLabel = (GridBagConstraints) gbc.clone();
gbcLabel.insets = new Insets( 2, 2, 2, 0 );
gbcLabel.gridwidth = 1;
gbcLabel.weightx = 0;
if ( c == null )
c = new JLabel( "" );
gbl.setConstraints( l, gbcLabel );
target.add( l );
gbl.setConstraints( c, gbc );
target.add( c );
}
/**
* Create a transparent window
*/
protected JWindow createWindow() {
JWindow window = new JWindow();
window.getRootPane().putClientProperty(PROPERTY_IS_QPOPUP, true);
window.setType(Type.POPUP);
window.getRootPane().putClientProperty("Window.shadow", Boolean.FALSE);
float k = .95f;
window.getRootPane().putClientProperty("Window.opacity", k);
window.setOpacity(k);
window.setBackground(new Color(0, 0, 0, 0));
window.getRootPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
window.getRootPane().add(contents, c);
return window;
}
/**
* @return Apache Commons Compress tab.
*/
private Component createCommonsCompressTab() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = initializeGridBagConstraints();
addPresentation(
panel, constraints,
"<html>" +
"<b>Apache Commons Compress</b> is a component of Commons Apache project." +
"<br>" +
"The Apache Commons Compress library defines an API for working with ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200, bzip2, 7z, arj, lzma, snappy, DEFLATE and Z files." +
"<br>" +
"See <a href='http://commons.apache.org/proper/commons-compress/'>http://commons.apache.org/proper/commons-compress/</a> for more information." +
"</html>");
addFile(panel, constraints, "NOTICE_commons-compress.txt");
addFile(panel, constraints, "LICENSE_commons-compress.txt");
return createScrollPane(panel);
}
/**
* @param wiki Wiki.
* @param errors List of errors.
* @param textPane Text pane where the text is.
*/
public LinterErrorPanel(
EnumWikipedia wiki,
List<LinterError> errors,
JTextComponent textPane) {
super(new GridBagLayout(), true);
this.wiki = wiki;
this.errors = errors;
this.textPane = textPane;
constructContents();
}
public JPanel buildTopPanel() {
final JLabel avatarLabel = new JLabel(PhoneRes.getImageIcon("LARGE_PHONE_ICON"));
final JLabel nameLabel = new JLabel();
nameLabel.setFont(new Font("Arial", Font.BOLD, 19));
nameLabel.setForeground(new Color(64, 103, 162));
String remoteName = getActiveCall().getCall().getRemoteName();
nameLabel.setText(remoteName);
final JPanel topPanel = new JPanel();
topPanel.setOpaque(false);
topPanel.setLayout(new GridBagLayout());
// Add Connected Label
connectedLabel = new JLabel(CONNECTED);
connectedLabel.setFont(new Font("Arial", Font.BOLD, 16));
connectedLabel.setForeground(greenColor);
// Add All Items to Top Panel
topPanel.add(avatarLabel, new GridBagConstraints(0, 0, 1, 2, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
topPanel.add(nameLabel, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
topPanel.add(connectedLabel, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
topPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.lightGray));
return topPanel;
}
/**
* Constructor
*
* @param progressBar The progress bar this has to update
*/
public ProgressDialog(Window parent, String dialogTitle) {
super(parent, dialogTitle, ModalityType.APPLICATION_MODAL);
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
setResizable(false);
bundle = java.util.ResourceBundle.getBundle("course_generator/Bundle");
Container paneGlobal = getContentPane();
paneGlobal.setLayout(new GridBagLayout());
// -- Progress bar
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setPreferredSize(new Dimension(500, 30));
Utils.addComponent(paneGlobal, progressBar, 0, 0, 1, 1, 0, 0, 10, 10, 10, 10,
GridBagConstraints.BASELINE_LEADING, GridBagConstraints.BOTH);
// -- Cancel button
buttonCancel = new JButton(bundle.getString("Global.btCancel.text"));
buttonCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (progressDialogListener != null) {
progressDialogListener.progressDialogCancelled();
}
}
});
Utils.addComponent(paneGlobal, buttonCancel, 0, 1, 1, 1, 0, 0, 0, 10, 10, 10, GridBagConstraints.CENTER,
GridBagConstraints.NONE);
pack();
// -- Center the dialog on the parent
setLocationRelativeTo(parent);
}
public UnitLoadingDialog(JFrame frame) {
super(frame, Messages.getString("UnitLoadingDialog.pleaseWait")); //$NON-NLS-1$
getContentPane().setLayout(new GridBagLayout());
getContentPane().add(lLoading, GBC.eol());
getContentPane().add(lCacheText, GBC.std());
getContentPane().add(lCacheCount, GBC.eol());
getContentPane().add(lFileText, GBC.std());
getContentPane().add(lFileCount, GBC.eol());
getContentPane().add(lZipText, GBC.std());
getContentPane().add(lZipCount, GBC.eol());
setSize(250, 130);
// move to middle of screen
setLocationRelativeTo(frame);
Runnable r = new Runnable() {
public void run() {
while (!loadingDone &&
!MechSummaryCache.getInstance().isInitialized()) {
updateCounts();
try {
Thread.sleep(UPDATE_FREQUENCY);
} catch (InterruptedException e) {
// not supposed to come here
}
}
}
};
MechSummaryCache.getInstance().addListener(mechSummaryCacheListener);
Thread t = new Thread(r, "Unit Loader Dialog"); //$NON-NLS-1$
t.start();
}
private void initGui() {
ButtonGroup group = new ButtonGroup();
cityGMLVersionBox = new JRadioButton[CityGMLVersionType.values().length];
for (int i = 0; i < CityGMLVersionType.values().length; i++) {
cityGMLVersionBox[i] = new JRadioButton();
cityGMLVersionBox[i].setText(CityGMLVersionType.values()[i].toString());
cityGMLVersionBox[i].setIconTextGap(10);
group.add(cityGMLVersionBox[i]);
if (Util.toCityGMLVersion(CityGMLVersionType.values()[i]) == CityGMLVersion.DEFAULT)
cityGMLVersionBox[i].setSelected(true);
// fire property change event
cityGMLVersionBox[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < CityGMLVersionType.values().length; i++) {
if (cityGMLVersionBox[i] == e.getSource()) {
ObjectRegistry.getInstance().getEventDispatcher().triggerEvent(
new PropertyChangeEvent("citygml.version", null, Util.toCityGMLVersion(CityGMLVersionType.values()[i]), VersionPanel.this));
break;
}
}
}
});
}
setLayout(new GridBagLayout());
block1 = new JPanel();
add(block1, GuiUtil.setConstraints(0,0,1.0,0.0,GridBagConstraints.BOTH,5,0,5,0));
block1.setBorder(BorderFactory.createTitledBorder(""));
block1.setLayout(new GridBagLayout());
{
for (int i = 0; i < cityGMLVersionBox.length; i++)
block1.add(cityGMLVersionBox[i], GuiUtil.setConstraints(0,i,1.0,1.0,GridBagConstraints.BOTH,0,5,0,5));
}
}
private static JPanel createCapJoinPanel(StrokeParam sp) {
EnumParam<BasicStrokeCap> capParam = sp.getStrokeCapParam();
EnumParam<BasicStrokeJoin> joinParam = sp.getStrokeJoinParam();
var p = new JPanel();
p.setBorder(createTitledBorder("Line Endpoints"));
p.setLayout(new GridBagLayout());
var gbh = new GridBagHelper(p);
JComponent capSelector = capParam.createGUI();
// Dirty trick: manually set the preferred width so that
// the layout aligns with the layout in the other panel.
// Doubling the width is about OK.
Dimension dim = capSelector.getPreferredSize();
dim.setSize(dim.getWidth() * 2, dim.getHeight());
capSelector.setPreferredSize(dim);
JComponent joinSelector = joinParam.createGUI();
capParam.setToolTip("The shape of the line endpoints");
joinParam.setToolTip("The way lines connect at the corners");
gbh.addLabelAndControl(BasicStrokeCap.NAME + ":", capSelector);
gbh.addLabelAndControl(BasicStrokeJoin.NAME + ":", joinSelector);
return p;
}
/**
* Constructs a Swing JPanel using current builder values. Values that must be set: (requires no
* values to be set)
*/
public JPanel build() {
final JPanel panel = panelBuilder.build();
panel.setLayout(new GridBagLayout());
components.forEach(c -> panel.add(c.component, c.constraints));
return panel;
}
@Override
protected GridBagLayout getAnotherObject() {
GridBagLayout layout = new GridBagLayout();
update(layout, "11", 1, 1);
update(layout, "12", 1, 2);
update(layout, "21", 2, 1);
update(layout, "22", 2, 2);
return layout;
}
public JPanel getArrowButtonBorderPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, GridBagConstraints.RELATIVE, 1, 1.0, 1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
panel.add(mainButton, gbc); // , BorderLayout.CENTER);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 0;
panel.add(arrowButton, gbc); // , BorderLayout.EAST);
mainButton.setBorder(null);
arrowButton.setBorder(null);
panel.setBackground(UIManager.getColor("Button.background"));
return panel;
}
/** Creates a new instance of NoWebServiceClientsPanel */
public NoWebServicesPanel(String text) {
setLayout(new GridBagLayout());
label = new JLabel(text);
label.setVerticalAlignment(SwingConstants.CENTER);
label.setHorizontalAlignment(SwingConstants.CENTER);
GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
add(label, gridBagConstraints);
}
public void createComponents(int gridy, JPanel panel,
GridBagLayout gridbag)
{
m_checkBox = new JCheckBox(getLabel(), m_initialValue);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = gridy;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.weightx = 1.0;
constraints.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(m_checkBox, constraints);
panel.add(m_checkBox);
}
private JLabel addLabel(String text,
GridBagLayout gridbag, GridBagConstraints c)
{
c.gridwidth = 1;
addToGB(new JLabel(text, JLabel.TRAILING), this, gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER;
JLabel label = new JLabel();
addToGB(label, this, gridbag, c);
return label;
}
public static JPanel makeGrid(int cols, GridBagConstraints template, Container... containers) {
JPanel ans = new JPanel(new GridBagLayout());
int i = -1;
for (Container container : containers) {
for (Component comp : container.getComponents()) {
i++;
GridBagConstraints c = (GridBagConstraints) template.clone();
c.gridx = i % cols;
c.gridy = i / cols;
ans.add(comp, c);
}
}
return ans;
}
@Override
protected GridBagLayout getAnotherObject() {
GridBagLayout layout = new GridBagLayout();
update(layout, "11", 1, 1);
update(layout, "12", 1, 2);
update(layout, "21", 2, 1);
update(layout, "22", 2, 2);
return layout;
}
public GameControllerPanel(final KeyboardKempstonAndTapeIn module) {
super(new GridBagLayout());
this.module = module;
final List<Controller> controllers = module.getDetectedControllers().stream()
.sorted(comparing(Controller::getName))
.collect(toList());
final List<Gadapter> activeGadapters = module.getActiveGadapters();
final GridBagConstraints constraints = new GridBagConstraints(
0,
GridBagConstraints.RELATIVE,
1,
1,
1,
1,
GridBagConstraints.NORTHWEST,
GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0),
0,
0);
for (final Controller c : controllers) {
final GadapterRecord newRecord = new GadapterRecord(c, activeGadapters);
this.add(newRecord, constraints);
}
}
@Override
public JPanel getComponent(int width, int height) throws IOException {
// the main panel
final JPanel base = new JPanel();
base.setPreferredSize(new Dimension(width, height));
base.setLayout(new GridBagLayout());
vc = new VideoCaptureComponent(640, 480);
vc.getDisplay().addVideoListener(this);
base.add(vc);
tracker.fcheck = true;
return base;
}