本文整理了Java中java.awt.GridBagLayout
类的一些代码示例,展示了GridBagLayout
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GridBagLayout
类的具体详情如下:
包路径:java.awt.GridBagLayout
类名称:GridBagLayout
[英]The GridBagLayout
class is a flexible layout manager that aligns components vertically and horizontally, without requiring that the components be of the same size. Each GridBagLayout
object maintains a dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display area.
Each component managed by a GridBagLayout
is associated with an instance of GridBagConstraints. The constraints object specifies where a component's display area should be located on the grid and how the component should be positioned within its display area. In addition to its constraints object, the GridBagLayout
also considers each component's minimum and preferred sizes in order to determine a component's size.
Grid coordinate (0,0) is in the upper left corner of the container with x increasing to the right and y increasing downward.
To use a grid bag layout effectively, you must customize one or more of the GridBagConstraints
objects that are associated with its components. You customize a GridBagConstraints
object by setting one or more of its instance variables:
GridBagConstraints#gridx, GridBagConstraints#gridySpecifies the cell at the upper left of the component's display area, where the cell at the origin of the grid has address gridx = 0
, gridy = 0
. Use GridBagConstraints.RELATIVE
(the default value) to specify that the component be placed immediately following (along the x axis for gridx
or the y axis for gridy
) the component that was added to the container just before this component was added. GridBagConstraints#gridwidth, GridBagConstraints#gridheightSpecifies the number of cells in a row (for gridwidth
) or column (for gridheight
) in the component's display area. The default value is 1. Use GridBagConstraints.REMAINDER
to specify that the component be the last one in its row (for gridwidth
) or column (for gridheight
). Use GridBagConstraints.RELATIVE
to specify that the component be the next to last one in its row (for gridwidth
) or column (for gridheight
). GridBagConstraints#fillUsed when the component's display area is larger than the component's requested size to determine whether (and how) to resize the component. Possible values are GridBagConstraints.NONE
(the default), GridBagConstraints.HORIZONTAL
(make the component wide enough to fill its display area horizontally, but don't change its height), GridBagConstraints.VERTICAL
(make the component tall enough to fill its display area vertically, but don't change its width), and GridBagConstraints.BOTH
(make the component fill its display area entirely). GridBagConstraints#ipadx, GridBagConstraints#ipadySpecifies the component's internal padding within the layout, how much to add to the minimum size of the component. The width of the component will be at least its minimum width plus (ipadx * 2)
pixels (since the padding applies to both sides of the component). Similarly, the height of the component will be at least the minimum height plus (ipady * 2)
pixels. GridBagConstraints#insetsSpecifies the component's external padding, the minimum amount of space between the component and the edges of its display area. GridBagConstraints#anchorUsed when the component is smaller than its display area to determine where (within the display area) to place the component.
Valid values are:
GridBagConstraints.NORTH
GridBagConstraints.SOUTH
GridBagConstraints.WEST
GridBagConstraints.EAST
GridBagConstraints.NORTHWEST
GridBagConstraints.NORTHEAST
GridBagConstraints.SOUTHWEST
GridBagConstraints.SOUTHEAST
GridBagConstraints.CENTER
(the default)GridBagConstraints#weightx, GridBagConstraints#weightyUsed to determine how to distribute space, which is important for specifying resizing behavior. Unless you specify a weight for at least one component in a row (weightx
) and column (weighty
), all the components clump together in the center of their container. This is because when the weight is zero (the default), the GridBagLayout
object puts any extra space between its grid of cells and the edges of the container.
The following figure shows ten components (all buttons) managed by a grid bag layout:
Each of the ten components has the fill
field of its associated GridBagConstraints
object set to GridBagConstraints.BOTH
. In addition, the components have the following non-default constraints:
weightx = 1.0
weightx = 1.0
, gridwidth = GridBagConstraints.REMAINDER
gridwidth = GridBagConstraints.REMAINDER
gridwidth = GridBagConstraints.RELATIVE
gridwidth = GridBagConstraints.REMAINDER
gridheight = 2
, weighty = 1.0
gridwidth = GridBagConstraints.REMAINDER
Note: The following code example includes classes that do not appear in this specification. Their inclusion is purely to serve as a demonstration.
Here is the code that implements the example shown above:
import java.awt.*;
import java.util.*;
import java.applet.Applet;
public class GridBagEx1 extends Applet {
protected void makebutton(String name,
GridBagLayout gridbag,
GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
}
public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setFont(new Font("Helvetica", Font.PLAIN, 14));
setLayout(gridbag);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c);
c.weightx = 0.0; //reset to the default
makebutton("Button5", gridbag, c); //another row
c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
makebutton("Button6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button7", gridbag, c);
c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c);
c.weighty = 0.0; //reset to the default
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c);
setSize(300, 100);
}
public static void main(String args[]) {
Frame f = new Frame("GridBag Layout Example");
GridBagEx1 ex1 = new GridBagEx1();
ex1.init();
f.add("Center", ex1);
f.pack();
f.setSize(f.getPreferredSize());
f.show();
}
}
[中]GridBagLayout
类是一个灵活的布局管理器,它可以垂直和水平对齐组件,而不需要组件具有相同的大小。每个GridBagLayout
对象维护一个动态的矩形单元格网格,每个组件占用一个或多个单元格,称为其显示区域。
由GridBagLayout
管理的每个组件都与GridBagConstraints的一个实例相关联。约束对象指定零部件的显示区域在栅格上的位置以及零部件在其显示区域内的定位方式。除了约束对象外,GridBagLayout
还考虑每个组件的最小和首选尺寸,以确定组件的尺寸。
栅格坐标(0,0)位于容器的左上角,x向右增加,y向下增加。
要有效使用网格包布局,必须自定义与其组件关联的一个或多个GridBagConstraints
对象。通过设置GridBagConstraints
对象的一个或多个实例变量,可以自定义该对象:
GridBagConstraints#gridx,GridBagConstraints#Gridy指定组件显示区域左上方的单元格,其中位于网格原点的单元格具有地址gridx = 0
、gridy = 0
。使用GridBagConstraints.RELATIVE
(默认值)指定在添加此组件之前添加到容器中的组件紧随其后(沿gridx
的x轴或gridy
的y轴)。GridBagConstraints#gridwidth、GridBagConstraints#GridHeights指定组件显示区域中的行(对于gridwidth
)或列(对于gridheight
)中的单元格数。默认值为1。使用GridBagConstraints.REMAINDER
指定组件是其行(对于gridwidth
)或列(对于gridheight
)中的最后一个组件。使用GridBagConstraints.RELATIVE
指定组件是其行(对于gridwidth
)或列(对于gridheight
)中倒数第二个组件。GridBagConstraints#Fill当组件的显示区域大于组件请求的大小时,用于确定是否(以及如何)调整组件的大小。可能的值有GridBagConstraints.NONE
(默认值)、GridBagConstraints.HORIZONTAL
(使组件宽度足以水平填充其显示区域,但不更改其高度)、GridBagConstraints.VERTICAL
(使组件高度足以垂直填充其显示区域,但不更改其宽度)和GridBagConstraints.BOTH
(使组件完全填充其显示区域)。GridBagConstraints#ipadx,GridBagConstraints#iPads指定布局中组件的内部填充,以及添加到组件最小尺寸的量。组件的宽度将至少为其最小宽度加上(ipadx * 2)
像素(因为填充应用于组件的两侧)。类似地,组件的高度将至少为最小高度加上(ipady * 2)
像素。GridBagConstraints#Insets指定组件的外部填充、组件与其显示区域边缘之间的最小空间量。GridBagConstraints#锚点当组件小于其显示区域时用于确定放置组件的位置(在显示区域内)。
有效值为:
GridBagConstraints.NORTH
GridBagConstraints.SOUTH
GridBagConstraints.WEST
GridBagConstraints.EAST
GridBagConstraints.NORTHWEST
GridBagConstraints.NORTHEAST
GridBagConstraints.SOUTHWEST
GridBagConstraints.SOUTHEAST
GridBagConstraints.CENTER
(默认值)weightx
和列(weighty
中的至少一个组件指定权重,否则所有组件都会在其容器的中心聚集在一起。这是因为当权重为零(默认值)时,GridBagLayout
对象在其单元格网格和容器边缘之间放置任何额外的空间。GridBagConstraints
对象的fill
字段设置为GridBagConstraints.BOTH
。此外,组件具有以下非默认约束:weightx = 1.0
weightx = 1.0
,gridwidth = GridBagConstraints.REMAINDER
gridwidth = GridBagConstraints.REMAINDER
gridwidth = GridBagConstraints.RELATIVE
gridwidth = GridBagConstraints.REMAINDER
gridheight = 2
,weighty = 1.0
gridwidth = GridBagConstraints.REMAINDER
import java.awt.*;
import java.util.*;
import java.applet.Applet;
public class GridBagEx1 extends Applet {
protected void makebutton(String name,
GridBagLayout gridbag,
GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
}
public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setFont(new Font("Helvetica", Font.PLAIN, 14));
setLayout(gridbag);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c);
c.weightx = 0.0; //reset to the default
makebutton("Button5", gridbag, c); //another row
c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
makebutton("Button6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button7", gridbag, c);
c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c);
c.weighty = 0.0; //reset to the default
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c);
setSize(300, 100);
}
public static void main(String args[]) {
Frame f = new Frame("GridBag Layout Example");
GridBagEx1 ex1 = new GridBagEx1();
ex1.init();
f.add("Center", ex1);
f.pack();
f.setSize(f.getPreferredSize());
f.show();
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
private JPanel makeBrowseButtonBox() {
JPanel buttonBox = new JPanel();
buttonBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
buttonBox.setLayout(new GridBagLayout());
browseButton = new JButton("Browse Trees");
browseButton.addActionListener(this);
JLabel sizeLabel = new JLabel("Tree size:");
JSlider fontSlider = new JSlider(2, 64, 12);
fontSlider.addChangeListener(this);
GridBagConstraints buttonConstraints = new GridBagConstraints();
buttonConstraints.fill = GridBagConstraints.HORIZONTAL;
buttonConstraints.weightx = 0.2;
buttonConstraints.weighty = 0.2;
buttonBox.add(browseButton,buttonConstraints);
buttonConstraints.weightx = 0.6;
buttonBox.add(fontSlider, buttonConstraints);
buttonConstraints.weightx = 0.2;
buttonBox.add(sizeLabel, buttonConstraints);
return buttonBox;
}
代码示例来源:origin: libgdx/libgdx
public void actionPerformed (ActionEvent event) {
boolean visible = !highMaxSlider.isVisible();
highMaxSlider.setVisible(visible);
highRangeButton.setText(visible ? "<" : ">");
GridBagLayout layout = (GridBagLayout)formPanel.getLayout();
GridBagConstraints constraints = layout.getConstraints(highRangeButton);
constraints.gridx = visible ? 5 : 4;
layout.setConstraints(highRangeButton, constraints);
Slider slider = visible ? highMaxSlider : highMinSlider;
value.setHighMax((Float)slider.getValue());
}
});
代码示例来源:origin: libgdx/libgdx
public void setEmbedded () {
GridBagLayout layout = (GridBagLayout)getLayout();
GridBagConstraints constraints = layout.getConstraints(contentPanel);
constraints.insets = new Insets(0, 0, 0, 0);
layout.setConstraints(contentPanel, constraints);
titlePanel.setVisible(false);
}
代码示例来源:origin: libgdx/libgdx
private void initializeComponents () {
getContentPane().setLayout(new GridBagLayout());
JPanel leftSidePanel = new JPanel();
leftSidePanel.setLayout(new GridBagLayout());
getContentPane().add(leftSidePanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
JPanel fontPanel = new JPanel();
leftSidePanel.add(fontPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
fontPanel.setLayout(new GridBagLayout());
unicodePanel = new JPanel(new GridBagLayout());
bitmapPanel = new JPanel(new GridBagLayout());
JPanel renderingPanel = new JPanel(new GridBagLayout());
samplePanel.setLayout(new GridBagLayout());
renderingPanel.setLayout(new GridBagLayout());
glyphCachePanel.setLayout(new GridBagLayout());
radioButtonsPanel.setLayout(new GridBagLayout());
rightSidePanel.setLayout(new GridBagLayout());
getContentPane().add(rightSidePanel, new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0, GridBagConstraints.CENTER,
paddingPanel.setLayout(new GridBagLayout());
代码示例来源:origin: igniterealtime/Openfire
frame.setTitle(appName);
JPanel mainPanel = new JPanel();
JLabel splashLabel = null;
cardPanel.setLayout(cardLayout);
splashLabel = new JLabel("", splash, JLabel.CENTER);
frame.setIconImage(offIcon.getImage());
mainPanel.setLayout(new BorderLayout());
cardPanel.setBackground(Color.white);
final JButton startButton = new JButton("Start");
startButton.setActionCommand("Start");
quitButton.setActionCommand("Quit");
toolbar.setLayout(new GridBagLayout());
toolbar.add(startButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
toolbar.add(stopButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
toolbar.add(browserButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
toolbar.add(quitButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
代码示例来源:origin: libgdx/libgdx
effectsList.getListSelectionListeners()[0].valueChanged(null);
setLayout(new GridBagLayout());
setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, java.awt.Color.black));
appliedEffectsPanel.add(this, constrains);
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new LayoutManager() {
public void removeLayoutComponent (Component comp) {
upButton = new JButton();
titlePanel.add(upButton);
upButton.setText("Up");
upButton.setMargin(new Insets(0, 0, 0, 0));
downButton.setMargin(new Insets(0, 0, 0, 0));
deleteButton.setMargin(new Insets(0, 0, 0, 0));
nameLabel = new JLabel(effect.toString());
titlePanel.add(nameLabel);
Font font = nameLabel.getFont();
nameLabel.setFont(new Font(font.getName(), Font.BOLD, font.getSize()));
add(titlePanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
valuesPanel.setLayout(new GridBagLayout());
add(valuesPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
代码示例来源:origin: libgdx/libgdx
public ValueDialog (JComponent component, String name, String description) {
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLayout(new GridBagLayout());
setModal(true);
((JSpinner.DefaultEditor)((JSpinner)component).getEditor()).getTextField().setColumns(4);
JPanel descriptionPanel = new JPanel();
descriptionPanel.setLayout(new GridBagLayout());
getContentPane().add(
descriptionPanel,
new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
0), 0, 0));
descriptionPanel.setBackground(Color.white);
descriptionPanel.add(descriptionText, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
descriptionText.setWrapStyleWord(true);
descriptionText.setLineWrap(true);
getContentPane().add(
panel,
new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 0,
5), 0, 0));
panel.add(new JLabel(name + ":"));
getContentPane().add(
JButton cancelButton = new JButton("Cancel");
代码示例来源:origin: chewiebug/GCViewer
Panel logoPanel = new Panel();
ImageIcon logoIcon = ImageHelper.loadImageIcon(LocalisationHelper.getString("about_dialog_image"));
JLabel la_icon = new JLabel(logoIcon);
la_icon.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
logoPanel.add(la_icon);
JPanel versionPanel = new JPanel();
versionPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
versionPanel.setLayout(new GridBagLayout());
JLabel copyright = new JLabel("\u00A9" + " 2011-2018: Joerg Wuethrich and contributors", JLabel.CENTER);
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTH;
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
Insets insetsGapOnTop = new Insets(10, 0, 0, 0);
GridBagConstraints gridBagConstraintsGapOnTop = new GridBagConstraints();
gridBagConstraintsGapOnTop.gridy = 1;
gridBagConstraintsGapOnTop.insets = insetsGapOnTop;
getContentPane().add("North", logoPanel);
getContentPane().add("Center", versionPanel);
getContentPane().add("South", buttonPanel);
pack();
setResizable(false);
代码示例来源:origin: bonnyfone/vectalign
panelInput = new JPanel(new GridLayout(1, 2, 10, 0));
JPanel panelFrom = new JPanel(new BorderLayout());
JPanel panelTo = new JPanel(new BorderLayout());
svgFrom = new SVGDrawingPanel();
svgFrom.setPreferredSize(new Dimension(190, 200));
svgTo.setPreferredSize(new Dimension(190, 200));
btnEditFrom = new JButton("Edit Path");
btnEditTo = new JButton("Edit Path");
btnSvgFrom = new JButton("Load SVG");
panelStrokeSize.add(new JLabel("Stroke size: "), BorderLayout.WEST);
SpinnerModel spinnerModel =
new SpinnerNumberModel(2, //initial value
btnMorphAnimation.setMargin(new Insets(0, 0, 0, 0));
panelOutput.setLayout(new GridBagLayout());
GridBagConstraints gc1 = new GridBagConstraints();
gc1.fill = GridBagConstraints.BOTH;
gc1.gridx = 0;
gc1.weightx = 0.975f;
gc1.weighty = 0.5f;
GridBagConstraints gcBtn1 = new GridBagConstraints();
gcBtn1.fill = GridBagConstraints.BOTH;
gcBtn1.gridx = 1;
代码示例来源:origin: libgdx/libgdx
protected void initializeComponents () {
setLayout(new GridBagLayout());
titlePanel = new JPanel(new GridBagLayout());
add(titlePanel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(3, 0, 3, 0), 0, 0));
titlePanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
descriptionLabel = new JLabel(description);
titlePanel.add(descriptionLabel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(3, 6, 3, 6), 0, 0));
removeButton = new JButton("X");
titlePanel.add(removeButton, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.NONE, new Insets(0, 0, 0, 6), 0, 0));
contentPanel = new JPanel(new GridBagLayout());
add(contentPanel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 6, 6, 6), 0, 0));
advancedPanel = new JPanel(new GridBagLayout());
add(advancedPanel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 6, 6, 6), 0, 0));
removeButton.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent event) {
removePanel();
代码示例来源:origin: libgdx/libgdx
getContentPane().add(splitPane, BorderLayout.CENTER);
JPanel propertiesPanel = new JPanel(new GridBagLayout());
rightSplit.add(propertiesPanel, JSplitPane.TOP);
propertiesPanel.setBorder(new CompoundBorder(BorderFactory.createEmptyBorder(3, 0, 6, 6), BorderFactory
.createTitledBorder("Editor Properties")));
editorPropertiesPanel = new JPanel(new GridBagLayout());
scroll.setViewportView(editorPropertiesPanel);
scroll.getVerticalScrollBar().setUnitIncrement(70);
rightSplit.add(rightSplitPane, JSplitPane.BOTTOM);
JPanel propertiesPanel = new JPanel(new GridBagLayout());
JPanel influencersPanel = new JPanel(new GridBagLayout());
influencerBox = new JComboBox(new DefaultComboBoxModel());
JButton addInfluencerButton = new JButton("Add");
addInfluencerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed (ActionEvent e) {
propertiesPanel = new JPanel(new GridBagLayout());
controllerPropertiesPanel = new JPanel(new GridBagLayout());
scroll.setViewportView(controllerPropertiesPanel);
scroll.getVerticalScrollBar().setUnitIncrement(70);
代码示例来源:origin: stackoverflow.com
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
JPanel panel = new JPanel();
panel.add(new JLabel("This is a label"));
panel.setBorder(new LineBorder(Color.BLACK)); // make it easy to see
frame.add(panel, new GridBagConstraints());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
代码示例来源:origin: libgdx/libgdx
private void uiLayout () {
topPanel = new JPanel(new GridBagLayout());
topPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
warningNotice = new JLabel("List of third party extensions for LibGDX");
warningNotice2 = new JLabel("These are not maintained by the LibGDX team, please see the support links for info and help");
warningNotice.setHorizontalAlignment(JLabel.CENTER);
warningNotice2.setHorizontalAlignment(JLabel.CENTER);
topPanel.add(warningNotice, new GridBagConstraints(0, 0, 1, 1, 1, 0, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
topPanel.add(warningNotice2, new GridBagConstraints(0, 1, 1, 1, 1, 0, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
separator.setBackground(new Color(85, 85, 85));
topPanel.add(separator, new GridBagConstraints(0, 2, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
bottomPanel = new JPanel(new GridBagLayout());
buttonPanel = new JPanel(new GridBagLayout());
buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
buttonOK = new SetupButton("Save");
代码示例来源:origin: jMonkeyEngine/jmonkeyengine
GridBagConstraints gbc;
JPanel mainPanel = new JPanel(new GridBagLayout());
JButton ok = new JButton(resourceBundle.getString("button.ok"));
JButton cancel = new JButton(resourceBundle.getString("button.cancel"));
icon = new JLabel(imageFile != null ? new ImageIcon(imageFile) : null);
gammaBox.setSelected(source.isGammaCorrection());
gbc = new GridBagConstraints();
gbc.weightx = 0.5;
gbc.gridx = 0;
mainPanel.add(vsyncBox, gbc);
gbc = new GridBagConstraints();
gbc.weightx = 0.5;
gbc.gridx = 3;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.weightx = 0.5;
gbc.gridx = 0;
ok.addActionListener(new ActionListener() {
this.getContentPane().add(mainPanel);
代码示例来源:origin: libgdx/libgdx
private void initializeComponents (String chartTitle, boolean hasIndependent) {
JPanel contentPanel = getContentPanel();
formPanel = new JPanel(new GridBagLayout());
contentPanel.add(formPanel, new GridBagConstraints(5, 5, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 6), 0, 0));
JLabel label = new JLabel("High:");
formPanel.add(label, new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 6), 0, 0));
formPanel.add(highMinSlider, new GridBagConstraints(3, 1, 1, 1, 0, 0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
highRangeButton = new JButton("<");
highRangeButton.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
formPanel.add(highRangeButton, new GridBagConstraints(5, 1, 1, 1, 0.0, 0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(0, 1, 0, 0), 0, 0));
JLabel label = new JLabel("Low:");
formPanel.add(label, new GridBagConstraints(2, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 6), 0, 0));
lowRangeButton = new JButton("<");
lowRangeButton.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
formPanel.add(lowRangeButton, new GridBagConstraints(5, 2, 1, 1, 0.0, 0, GridBagConstraints.WEST,
代码示例来源:origin: libgdx/libgdx
private void initializeComponents () {
setLayout(new GridBagLayout());
titlePanel = new JPanel(new GridBagLayout());
add(titlePanel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(3, 0, 3, 0), 0, 0));
titlePanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
JLabel label = new JLabel(name);
titlePanel.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(3, 6, 3, 6), 0, 0));
label.setFont(label.getFont().deriveFont(Font.BOLD));
descriptionLabel = new JLabel(description);
titlePanel.add(descriptionLabel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(3, 6, 3, 6), 0, 0));
contentPanel = new JPanel(new GridBagLayout());
add(contentPanel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 6, 6, 6), 0, 0));
advancedPanel = new JPanel(new GridBagLayout());
add(advancedPanel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 6, 6, 6), 0, 0));
代码示例来源:origin: log4j/log4j
public LogFactor5ErrorDialog(JFrame jframe, String message) {
super(jframe, "Error", true);
JButton ok = new JButton("Ok");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hide();
}
});
JPanel bottom = new JPanel();
bottom.setLayout(new FlowLayout());
bottom.add(ok);
JPanel main = new JPanel();
main.setLayout(new GridBagLayout());
wrapStringOnPanel(message, main);
getContentPane().add(main, BorderLayout.CENTER);
getContentPane().add(bottom, BorderLayout.SOUTH);
show();
}
//--------------------------------------------------------------------------
代码示例来源:origin: stackoverflow.com
private JTextField nameField = new JTextField(10);
private JComboBox searchTermsCombo = new JComboBox();
private JButton addNewFieldBtn = new JButton("Add New Field");
private JButton submitBtn = new JButton("Submit");
private JPanel centerPanel = new JPanel(new GridBagLayout());
private int gridY = 0;
centerPanel.add(new JLabel("Name:"), gbc);
gbc = createGBC(1, gridY);
centerPanel.add(nameField, gbc);
gridY++;
centerPanel.add(new JLabel("Search Terms:"), gbc);
win.pack();
win.setLocationRelativeTo(null);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.anchor = (x == 0) ? gbc.LINE_START : gbc.LINE_END;
gbc.fill = (x == 0) ? gbc.BOTH : gbc.HORIZONTAL;
gbc.insets = (x == 0) ? new Insets(5, 0, 5, 5) : new Insets(5, 5, 5, 0);
return gbc;
代码示例来源:origin: cmusphinx/sphinx4
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints constraints;
Insets insets;
contentPane.setLayout(gridBag);
filename = new JTextField(12);
JLabel filenameLabel = new JLabel("Filename:");
filenameLabel.setLabelFor(filename);
insets = new Insets(12, 12, 0, 0); // top, left, bottom, right
constraints = new GridBagConstraints(
0, 0, 1, 1, // x, y, width, height
0.0, 0.0, // weightx, weighty
gridBag.setConstraints(filenameLabel, constraints);
contentPane.add(filenameLabel);
insets = new Insets(12, 7, 0, 12); // top, left, bottom, right
constraints = new GridBagConstraints(
1, 0, 1, 1, // x, y, width, height
1.0, 1.0, // weightx, weighty
gridBag.setConstraints(filename, constraints);
contentPane.add(filename);
gridBag.setConstraints(okButton, constraints);
contentPane.add(okButton);
代码示例来源:origin: stackoverflow.com
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JLabel(new ImageIcon(img)));
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(new JLabel("Hello world"), gbc);
frame.add(new JLabel("I'm on top"), gbc);
frame.add(new JButton("Clickity-clackity"), gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
内容来源于网络,如有侵权,请联系作者删除!