下面列出了怎么用java.awt.FlowLayout的API类实例代码及写法,或者点击链接到github查看源代码。
private JTextField createEntry(String labelText, int cols, String text,
String toolTipText, JComponent labels,
JComponent values)
{
Box boxLabel = Box.createHorizontalBox();
boxLabel.add(Box.createHorizontalGlue());
JLabel label = new JLabel(i18n(labelText));
label.setAlignmentY(Component.CENTER_ALIGNMENT);
boxLabel.add(label);
labels.add(boxLabel);
JPanel fieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
JTextField field = new JTextField(cols);
field.setHorizontalAlignment(JTextField.CENTER);
field.setToolTipText(i18n(toolTipText));
field.setText(text);
fieldPanel.add(field);
values.add(fieldPanel);
return field;
}
/**
* Create an info component with the given text
*
* @param title The title for the component
* @param text The text
* @return The component
*/
JComponent createTextInfoPanel(String title, String text)
{
JPanel textInfoPanel = new JPanel(new BorderLayout());
JPanel controlPanel = new JPanel(new FlowLayout());
controlPanel.add(new JLabel(title));
JButton saveButton = new JButton("Save as...");
saveButton.addActionListener(
e -> saveTextAs(textInfoPanel, text));
controlPanel.add(saveButton);
textInfoPanel.add(controlPanel, BorderLayout.NORTH);
JTextArea textArea = new JTextArea(text);
textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
textInfoPanel.add(new JScrollPane(textArea), BorderLayout.CENTER);
return textInfoPanel;
}
private JPanel center() {
JPanel panel = new JPanel(new BorderLayout());
panel.setOpaque(false);
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel tableHeader = new JPanel(new FlowLayout(FlowLayout.LEADING, 10, 5));
tableHeader.setOpaque(false);
tableHeader.add(new JLabel(MessageUtils.getLocalizedMessage("add_document.label.fields")));
panel.add(tableHeader, BorderLayout.PAGE_START);
JScrollPane scrollPane = new JScrollPane(fieldsTable());
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
panel.add(scrollPane, BorderLayout.CENTER);
JPanel tableFooter = new JPanel(new FlowLayout(FlowLayout.TRAILING, 10, 5));
tableFooter.setOpaque(false);
addBtn.setEnabled(true);
tableFooter.add(addBtn);
tableFooter.add(closeBtn);
panel.add(tableFooter, BorderLayout.PAGE_END);
return panel;
}
/**
* Adds the file field.
*
* @param label
* the label
* @param defaultPath
* the default path
* @param columns
* the columns
*/
public void addFileField(String label, String defaultPath, int columns) {
addStringField(label, defaultPath, columns);
if (isHeadless())
return;
TextField text = (TextField) stringField.lastElement();
GridBagLayout layout = (GridBagLayout) getLayout();
GridBagConstraints constraints = layout.getConstraints(text);
Button button = new Button("Browse...");
FileListener listener = new FileListener("Browse for " + label, text);
button.addActionListener(listener);
button.addKeyListener(this);
Panel panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
panel.add(text);
panel.add(button);
layout.setConstraints(panel, constraints);
add(panel);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("JRadioButton Example");
check = new JRadioButton("Check here ");
check2 = new JRadioButton("Or check here");
ButtonGroup bg = new ButtonGroup();
bg.add(check);
bg.add(check2);
frame.getContentPane().add(check);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(check2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Thread.sleep(2000);
bg.clearSelection();
}
/**
* Adds the directory field.
*
* @param label
* the label
* @param defaultPath
* the default path
* @param columns
* the columns
*/
public void addDirectoryField(String label, String defaultPath, int columns) {
addStringField(label, defaultPath, columns);
if (isHeadless())
return;
TextField text = (TextField) stringField.lastElement();
GridBagLayout layout = (GridBagLayout) getLayout();
GridBagConstraints constraints = layout.getConstraints(text);
Button button = new Button("Browse...");
DirectoryListener listener = new DirectoryListener("Browse for " + label, text);
button.addActionListener(listener);
button.addKeyListener(this);
Panel panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
panel.add(text);
panel.add(button);
layout.setConstraints(panel, constraints);
add(panel);
}
/**
* DOCUMENT ME!
*/
@Test
@Ignore // Needs an update
public void testSimpleFrameSize() {
JFrame frame = new JFrame();
JPanel content = new JPanel();
content.setLayout(new FlowLayout(FlowLayout.CENTER));
content.setBackground(new Color(250, 250, 250));
content.setPreferredSize(new Dimension(400,400));
frame.add(BorderLayout.CENTER, content);
frame.pack();
Dimension frameSize = frame.getSize();
Dimension contentSize = content.getSize();
int titleHeight = PlatformUtils.isMac() ? 22 : 25;
assertEquals("Window height must correspond to content width + title height", 400 + titleHeight, frameSize.height);
assertEquals("Window width must correspond to content width", 400, frameSize.width);
assertEquals("Content height must be 400", 400, contentSize.height);
assertEquals("Content width must be 400", 400, contentSize.width);
}
/** Creates the ui. */
private void createUI() {
groupPanel = new JPanel();
FlowLayout flowLayout = (FlowLayout) groupPanel.getLayout();
flowLayout.setVgap(0);
flowLayout.setHgap(0);
TitledBorder titledBorder =
BorderFactory.createTitledBorder(
Localisation.getString(MapBoxTool.class, "MapBoxTool.groupTitle"));
groupPanel.setBorder(titledBorder);
// Export to SLD
exportToSLDButton =
new ToolButton(
Localisation.getString(MapBoxTool.class, "MapBoxTool.exportToSLD"),
"tool/exporttosld.png");
exportToSLDButton.setEnabled(false);
exportToSLDButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
exportToSLD();
}
});
groupPanel.setPreferredSize(new Dimension(PANEL_WIDTH, ToolPanel.TOOL_PANEL_HEIGHT));
groupPanel.add(exportToSLDButton);
}
@Override
public void init() {
tf = new TextField(20);
tf.setText("0123456789");
tf.select(0, 6);
final TextArea ta = new TextArea("INSTRUCTIONS:\n"
+ "The text 012345 should be selected in the TextField.\n"
+ "If this is what you observe, then the test passes.\n"
+ "Otherwise, the test fails.", 40, 5,
TextArea.SCROLLBARS_NONE);
ta.setEditable(false);
ta.setPreferredSize(new Dimension(300, 70));
final Panel panel = new Panel();
panel.setLayout(new FlowLayout());
panel.add(tf);
setLayout(new BorderLayout());
add(ta, BorderLayout.CENTER);
add(panel, BorderLayout.PAGE_END);
}
@Override
public void init() {
tf = new TextField(20);
tf.setText("0123456789");
tf.select(0, 6);
final TextArea ta = new TextArea("INSTRUCTIONS:\n"
+ "The text 012345 should be selected in the TextField.\n"
+ "If this is what you observe, then the test passes.\n"
+ "Otherwise, the test fails.", 40, 5,
TextArea.SCROLLBARS_NONE);
ta.setEditable(false);
ta.setPreferredSize(new Dimension(300, 70));
final Panel panel = new Panel();
panel.setLayout(new FlowLayout());
panel.add(tf);
setLayout(new BorderLayout());
add(ta, BorderLayout.CENTER);
add(panel, BorderLayout.PAGE_END);
}
public TestFrame() {
super("TestFrame");
// The change of the orientation and the reverse order of
// adding the buttons to the panel is because in Container.removeNotify()
// the child components are removed in the reverse order.
// We want that the focus owner (b0) would be removed first and
// that the next traversable component would be b1.
panel0.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
panel0.add(b2);
panel0.add(b1);
panel0.add(b0);
panel1.add(b3);
panel1.add(b4);
setLayout(new FlowLayout());
add(panel0);
add(panel1);
pack();
panel0.setBackground(Color.red);
panel1.setBackground(Color.blue);
}
public TestFrame() {
super("TestFrame");
// The change of the orientation and the reverse order of
// adding the buttons to the panel is because in Container.removeNotify()
// the child components are removed in the reverse order.
// We want that the focus owner (b0) would be removed first and
// that the next traversable component would be b1.
panel0.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
panel0.add(b2);
panel0.add(b1);
panel0.add(b0);
panel1.add(b3);
panel1.add(b4);
setLayout(new FlowLayout());
add(panel0);
add(panel1);
pack();
panel0.setBackground(Color.red);
panel1.setBackground(Color.blue);
}
public ExportPanel(int nrLayers) {
setBorder(createEmptyBorder(10, 10, 10, 10));
setLayout(new VerticalLayout(10));
add(new JLabel(" Animation frames are based on the layers of the image. "));
var settingsPanel = new JPanel();
settingsPanel.setLayout(new FlowLayout(LEFT, 10, 10));
settingsPanel.add(new JLabel("Delay Between Frames (Milliseconds):"));
delayTF = new JTextField("200", 4);
settingsPanel.add(delayTF);
add(settingsPanel);
if (nrLayers > 2) {
pingPongCB = new JCheckBox("Ping Pong Animation");
} else {
pingPongCB = new JCheckBox("Ping Pong Animation (min 3 layers needed)");
pingPongCB.setEnabled(false);
}
add(pingPongCB);
}
/**
* Creates the main frame for <code>this</code> sample.
*/
public TextFlipSelectOnEscape() {
super("Text flip select on ESC");
this.setLayout(new BorderLayout());
final JTextField jtf = new JTextField("sample text");
jtf.setColumns(20);
JPanel main = new JPanel(new FlowLayout(FlowLayout.CENTER));
this.add(main, BorderLayout.CENTER);
main.add(jtf);
JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
final JCheckBox hasSelectOnFocus = new JCheckBox("Has \"flip select on ESC\" behaviour");
hasSelectOnFocus.addActionListener((ActionEvent e) -> SubstanceCortex.ComponentScope
.setFlipTextSelectionOnEscape(jtf, hasSelectOnFocus.isSelected()));
controls.add(hasSelectOnFocus);
this.add(controls, BorderLayout.SOUTH);
this.setSize(400, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* Simple constructor.
*/
public SchemaTreeCellRenderer() {
FlowLayout fl = new FlowLayout(FlowLayout.LEFT, 1, 1);
this.setLayout(fl);
this.iconLabel = new JLabel();
this.iconLabel.setOpaque(false);
this.iconLabel.setBorder(null);
this.add(this.iconLabel);
// add some space
this.add(Box.createHorizontalStrut(5));
this.nameLabel = new JLabel();
this.nameLabel.setOpaque(false);
this.nameLabel.setBorder(null);
this.nameLabel.setFont(nameFont);
this.add(this.nameLabel);
this.isSelected = false;
this.setOpaque(false);
this.setBorder(null);
}
/**
* Adds a link-style edit list label to the bottom of the popup of the specified combo box.<br>
* When the link is activated (clicked), the editTask will be run.
* @param comboBox combo box to add the edit link to
* @param editTask edit task to be performed when the link is activated
*/
public static void addEditListLinkToComboBoxPopup( final JComboBox< ? > comboBox, final Runnable editTask ) {
final Object child = comboBox.getUI().getAccessibleChild( comboBox, 0 );
if ( child instanceof JPopupMenu ) {
final JPanel editPanel = new JPanel( new FlowLayout( FlowLayout.RIGHT, 3, 1 ) );
final JLabel editLinkLabel = GeneralUtils.createLinkStyledLabel( Language.getText( "general.editList" ) );
editLinkLabel.addMouseListener( new MouseAdapter() {
public void mousePressed( final MouseEvent event ) {
comboBox.hidePopup();
editTask.run();
};
} );
editPanel.add( editLinkLabel );
( (JPopupMenu) child ).add( editPanel );
}
}
public AttributeEditorPanel() {
super(new BorderLayout());
okc = new JPanel(new GridLayout(1, 3, 5, 0));
JButton ok = new JButton(okAction);
cancel = new JButton(cancelAction);
apply = new JButton(applyAction);
addOk(ok, okc);
okc.add(apply);
okc.add(cancel);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
panel.add(okc);
this.add(panel, BorderLayout.SOUTH);
setAttributeEditor(null, null, null, null, false, null);
}
/**
* Opens a sample frame under the specified skin.
*
* @param skin
* Skin.
*/
private void openSampleFrame(SubstanceSkin skin) {
JFrame sampleFrame = new JFrame(skin.getDisplayName());
sampleFrame.setLayout(new FlowLayout());
JButton defaultButton = new JButton("active");
JButton button = new JButton("default");
JButton disabledButton = new JButton("disabled");
disabledButton.setEnabled(false);
sampleFrame.getRootPane().setDefaultButton(defaultButton);
sampleFrame.add(defaultButton);
sampleFrame.add(button);
sampleFrame.add(disabledButton);
sampleFrame.setVisible(true);
sampleFrame.pack();
sampleFrame.setLocationRelativeTo(null);
sampleFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
sampleFrame.setIconImage(new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR));
SubstanceCortex.RootPaneScope.setSkin(sampleFrame.getRootPane(), skin);
SwingUtilities.updateComponentTreeUI(sampleFrame);
sampleFrame.repaint();
}
private JPanel createButtons()
{
JPanel innerPanel = new JPanel(new GridLayout(1, 0, GuiUtil.PAD, 0));
m_runButton = new JButton(i18n("LB_RUN"));
m_runButton.setToolTipText(i18n("TT_ANALYZE_RUN"));
m_runButton.setActionCommand("run");
m_runButton.addActionListener(this);
m_runButton.setMnemonic(KeyEvent.VK_R);
m_runButton.setEnabled(false);
GuiUtil.setMacBevelButton(m_runButton);
innerPanel.add(m_runButton);
m_clearButton = new JButton(i18n("LB_ANALYZE_CLEAR"));
m_clearButton.setToolTipText(i18n("TT_ANALYZE_CLEAR"));
m_clearButton.setActionCommand("clear");
m_clearButton.addActionListener(this);
m_clearButton.setMnemonic(KeyEvent.VK_C);
GuiUtil.setMacBevelButton(m_clearButton);
innerPanel.add(m_clearButton);
JPanel outerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
outerPanel.add(innerPanel);
return outerPanel;
}
public TurnsPanel() {
FlowLayout flowLayout = (FlowLayout) getLayout();
flowLayout.setVgap(1);
flowLayout.setHgap(1);
flowLayout.setAlignment(FlowLayout.LEFT);
lblTurnNumber = new JLabel("Turn " + GameManager.getInstance().getTurns().size());
add(lblTurnNumber);
add(new JButton(new UntapPhase()));
add(new JButton(new UpkeepPhase()));
add(new JButton(new DrawPhase()));
add(new JButton(new MainPhase(1)));
add(new JButton(new CombatPhase()));
add(new JButton(new AttackPhase()));
add(new JButton(new BlockPhase()));
add(new JButton(new DamagePhase()));
add(new JButton(new EndCombatPhase()));
add(new JButton(new MainPhase(2)));
add(new JButton(new EndPhase()));
add(new JButton(new CleanUpPhase()));
add(new JButton(new EndTurnPhase()));
}
/**
* @return Page components.
*/
private Component createPageComponents() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
addTextPageName(panel);
JToolBar toolbar = new JToolBar(SwingConstants.HORIZONTAL);
toolbar.setFloatable(false);
toolbar.setBorderPainted(false);
addButtonReload(toolbar, true);
buttonView = ActionExternalViewer.addButton(
toolbar, getWikipedia(), getPageName(), false, true, false);
buttonViewHistory = ActionExternalViewer.addButton(
toolbar, getWikipedia(), getPageName(), ActionExternalViewer.ACTION_HISTORY, true, true);
addButtonSend(toolbar, true);
buttonWatch = ActionWatchPage.addButton(
getParentComponent(), toolbar, getWikipedia(), getPageName(), true, true);
addButtonFullAnalysis(toolbar, true);
panel.add(toolbar);
return panel;
}
public static void init() throws Throwable {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame();
frame.setLayout(new FlowLayout());
JPanel p1 = new JPanel();
button = new JButton("Test");
p1.add(button);
frame.add(p1);
text = new WeakReference<JTextField>(new JTextField("Text"));
p = new WeakReference<JPanel>(new JPanel(new FlowLayout()));
p.get().add(text.get());
frame.add(p.get());
frame.setBounds(500, 400, 200, 200);
frame.setVisible(true);
}
});
Util.focusComponent(text.get(), 500);
Util.clickOnComp(button, new Robot());
//References to objects testes for memory leak are stored in Util.
//Need to clean them
Util.cleanUp();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame.remove(p.get());
}
});
Util.waitForIdle(null);
//After the next caret blink it automatically TextField references
Thread.sleep(text.get().getCaret().getBlinkRate() * 2);
Util.waitForIdle(null);
assertGC();
}
private JPanel buttons() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
panel.setOpaque(false);
panel.setBorder(BorderFactory.createEmptyBorder(3, 3, 10, 20));
JButton okBtn = new JButton(MessageUtils.getLocalizedMessage("button.ok"));
okBtn.addActionListener(listeners::openIndexOrDirectory);
panel.add(okBtn);
JButton cancelBtn = new JButton(MessageUtils.getLocalizedMessage("button.cancel"));
cancelBtn.addActionListener(e -> dialog.dispose());
panel.add(cancelBtn);
return panel;
}
@PostConstruct
protected void init() {
setLayout(new BorderLayout());
JPanel jp_north = new JPanel(new FlowLayout(FlowLayout.LEFT));
jp_north.add(jp_content_type_charset.getComponent());
jb_body.setToolTipText("Select file having body content");
jb_body.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
fileOpen();
}
});
jp_north.add(jb_body);
add(jp_north, BorderLayout.NORTH);
jta.setFont(UIUtil.FONT_MONO_PLAIN);
jta.setEditable(false);
add(new JScrollPane(jta), BorderLayout.CENTER);
// DnD:
FileDropTargetListener l = new FileDropTargetListener();
l.addDndAction(new DndAction() {
@Override
public void onDrop(List<File> files) {
fileOpen(files.get(0));
}
});
new DropTarget(jta, l);
new DropTarget(jb_body, l);
}
CardPanel(ActionListener actionListener) {
listener = actionListener;
setLayout(new CardLayout());
add("one", create(new FlowLayout()));
add("two", create(new BorderLayout()));
add("three", create(new GridLayout(2, 2)));
add("four", create(new BorderLayout(10, 10)));
add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
add("six", create(new GridLayout(2, 2, 10, 10)));
}
private StatusBar() {
super(new BorderLayout(0, 0));
leftPanel = new JPanel(new FlowLayout(LEFT, 5, 0));
statusBarLabel = new JLabel("Pixelitor started");
leftPanel.add(statusBarLabel);
add(leftPanel, CENTER);
add(ZoomControl.get(), EAST);
setBorder(createEtchedBorder());
}
ControlPanel( Regression_108965_swing siv )
{
this.siv = siv;
setLayout( new GridLayout( 0, 1, 0, 0 ) );
JPanel jp = new JPanel( );
jp.setLayout( new FlowLayout( FlowLayout.LEFT, 3, 3 ) );
}
/**
* Instantiates a new tool panel.
*
* @param parentObj the parent obj
* @param toolMap the tool map
*/
public ToolPanel(ToolSelectionInterface parentObj, Map<Class<?>, List<ToolInterface>> toolMap) {
this.toolSelection = parentObj;
theToolPanel = new JPanel();
FlowLayout flowLayout = (FlowLayout) theToolPanel.getLayout();
flowLayout.setAlignOnBaseline(true);
flowLayout.setVgap(1);
flowLayout.setHgap(1);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(theToolPanel);
this.toolMap = toolMap;
JPanel optionsPanel = new JPanel();
add(optionsPanel);
JCheckBox chckbxRecursive =
new JCheckBox(Localisation.getString(ToolPanel.class, "ToolPanel.recursive"));
chckbxRecursive.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
toolSelection.setRecursiveFlag(chckbxRecursive.isSelected());
}
});
optionsPanel.add(chckbxRecursive);
this.setPreferredSize(new Dimension(50, EMPTY_TOOL_PANEL_HEIGHT));
}
CardPanel(ActionListener actionListener) {
listener = actionListener;
setLayout(new CardLayout());
add("one", create(new FlowLayout()));
add("two", create(new BorderLayout()));
add("three", create(new GridLayout(2, 2)));
add("four", create(new BorderLayout(10, 10)));
add("five", create(new FlowLayout(FlowLayout.LEFT, 10, 10)));
add("six", create(new GridLayout(2, 2, 10, 10)));
}
/**
* Creates the main frame for <code>this</code> sample.
*/
public ShowExtraWidgets() {
super("Show extra widgets");
this.setLayout(new BorderLayout());
JPanel centerPanel = new JPanel(new FlowLayout());
final JTextField readOnlyTextField = new JTextField("read-only");
readOnlyTextField.setEditable(false);
centerPanel.add(readOnlyTextField);
this.add(centerPanel, BorderLayout.CENTER);
JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
final JCheckBox showExtraWidgets = new JCheckBox("show extra widgets");
showExtraWidgets.addActionListener((ActionEvent e) -> SwingUtilities.invokeLater(() -> {
// based on the checkbox selection status, update the visibility of the lock icon.
SubstanceCortex.GlobalScope.setExtraWidgetsPresence(showExtraWidgets.isSelected());
SubstanceCortex.ComponentScope.setLockIconVisible(readOnlyTextField,
showExtraWidgets.isSelected());
SwingUtilities.updateComponentTreeUI(ShowExtraWidgets.this);
}));
controls.add(showExtraWidgets);
this.add(controls, BorderLayout.SOUTH);
this.setSize(400, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}