java.awt.FlowLayout类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(15.6k)|赞(0)|评价(0)|浏览(153)

本文整理了Java中java.awt.FlowLayout类的一些代码示例,展示了FlowLayout类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FlowLayout类的具体详情如下:
包路径:java.awt.FlowLayout
类名称:FlowLayout

FlowLayout介绍

[英]A flow layout arranges components in a left-to-right flow, much like lines of text in a paragraph. Flow layouts are typically used to arrange buttons in a panel. It will arrange buttons left to right until no more buttons fit on the same line. Each line is centered.

For example, the following picture shows an applet using the flow layout manager (its default layout manager) to position three buttons:

Here is the code for this applet:

Note: The following code example includes classes that do not appear in this specification. Their inclusion is purely to serve as a demonstration.

import java.awt.*;  
import java.applet.Applet; 
public class myButtons extends Applet { 
Button button1, button2, button3; 
public void init() { 
button1 = new Button("Ok"); 
button2 = new Button("Open"); 
button3 = new Button("Close"); 
add(button1); 
add(button2); 
add(button3); 
} 
}

A flow layout lets each component assume its natural (preferred) size.
[中]流程布局以从左到右的流程排列组件,非常类似于段落中的文本行。流程布局通常用于在面板中排列按钮。它将从左到右排列按钮,直到同一行上没有更多的按钮。每条线都居中。
例如,下图显示了一个小程序使用流布局管理器(其默认布局管理器)定位三个按钮:
以下是此小程序的代码:
注意:下面的代码示例包括本规范中未出现的类。他们的加入纯粹是为了证明

import java.awt.*;  
import java.applet.Applet; 
public class myButtons extends Applet { 
Button button1, button2, button3; 
public void init() { 
button1 = new Button("Ok"); 
button2 = new Button("Open"); 
button3 = new Button("Close"); 
add(button1); 
add(button2); 
add(button3); 
} 
}

流布局允许每个组件假定其自然(首选)大小。

代码示例

代码示例来源:origin: apache/ignite

/**
 * @param components Components.
 * @return Panel.
 */
private JPanel createPanel(JComponent... components) {
  JPanel panel = new JPanel();
  panel.setLayout(new FlowLayout());
  for (JComponent component : components)
    panel.add(component);
  return panel;
}

代码示例来源:origin: cmusphinx/sphinx4

JPanel buttonPanel = new JPanel();
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
buttonPanel.setLayout(layout);
playButton = new JButton("Play");
playButton.setEnabled(true);
playButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    player.play(audioPanel.getSelectionStart(),
buttonPanel.add(recordButton);
buttonPanel.add(playButton);
buttonPanel.add(zoomInButton);

代码示例来源:origin: stanfordnlp/CoreNLP

JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.WHITE);
buttonPanel.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
FlowLayout fl = new FlowLayout();
fl.setAlignment(FlowLayout.RIGHT);
buttonPanel.setLayout(fl);
tagButton.setText("Tag sentence!");
tagButton.setBackground(Color.WHITE);
buttonPanel.add(tagButton);
tagButton.addActionListener(e -> performTagAction(e));

代码示例来源:origin: libgdx/libgdx

JPanel fontPanel = new JPanel();
    fontPanel.add(new JLabel("Size:"), new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.EAST,
      GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
      GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0));
      bitmapPanel.add(new JLabel("Gamma:"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST,
        GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
    browseButton = new JButton("...");
    browseButton.setMargin(new Insets(0, 0, 0, 0));
    fontPanel.add(new JLabel("Rendering:"), new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST,
      GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
    sampleNeheButton = new JButton();
getContentPane().add(rightSidePanel, new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0, GridBagConstraints.CENTER,
  GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    FlowLayout advancePanelLayout = new FlowLayout();

代码示例来源:origin: pmd/pmd

private void init() {
  JTextArea errorArea = new JTextArea();
  errorArea.setEditable(false);
  errorArea.setText(exc.getMessage() + "\n");
  getContentPane().setLayout(new BorderLayout());
  JPanel messagePanel = new JPanel(new BorderLayout());
  messagePanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory
      .createTitledBorder(BorderFactory.createEtchedBorder(), NLS.nls("COMPILE_ERROR.PANEL.TITLE"))));
  messagePanel.add(new JScrollPane(errorArea), BorderLayout.CENTER);
  getContentPane().add(messagePanel, BorderLayout.CENTER);
  JPanel btnPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  okBtn = new JButton(NLS.nls("COMPILE_ERROR.OK_BUTTON.CAPTION"));
  okBtn.addActionListener(this);
  btnPane.add(okBtn);
  getRootPane().setDefaultButton(okBtn);
  getContentPane().add(btnPane, BorderLayout.SOUTH);
  pack();
  setLocationRelativeTo(getParent());
  setVisible(true);
}

代码示例来源:origin: apache/shiro

public void afterPropertiesSet() throws Exception {
  ClassPathResource resource = new ClassPathResource("logo.png");
  ImageIcon icon = new ImageIcon(resource.getURL());
  JLabel logo = new JLabel(icon);
  saveButton = new JButton("Save Value");
  saveButton.addActionListener(this);
  refreshButton = new JButton("Refresh Value");
  refreshButton.addActionListener(this);
  JPanel valuePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
  valuePanel.add(valueField);
  valuePanel.add(saveButton);
  valuePanel.add(refreshButton);
  secureMethod3Button.addActionListener(this);
  JPanel methodPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
  methodPanel.add(secureMethod1Button);
  methodPanel.add(secureMethod2Button);
  methodPanel.add(secureMethod3Button);
  frame = new JFrame("Apache Shiro Sample Application");
  frame.setSize(500, 200);

代码示例来源:origin: libgdx/libgdx

@Override
public void run () {
  JPanel panel = new JPanel(new FlowLayout());
  JPanel textPanel = new JPanel() {
    public boolean isOptimizedDrawingEnabled () {
      return false;
  };
  textPanel.setLayout(new OverlayLayout(textPanel));
  panel.add(textPanel);
  textPanel.add(textField);
  final JLabel placeholderLabel = new JLabel(hint);
  placeholderLabel.setForeground(Color.GRAY);
  placeholderLabel.setAlignmentX(0.0f);
  textPanel.add(placeholderLabel, 0);

代码示例来源:origin: RipMeApp/ripme

private void setupHandlers() {
  ripButton.addActionListener(new RipButtonHandler());
  ripTextfield.addActionListener(new RipButtonHandler());
  ripTextfield.getDocument().addDocumentListener(new DocumentListener() {
    logPanel.setVisible(!logPanel.isVisible());
    emptyPanel.setVisible(!logPanel.isVisible());
      checkChoise.setLayout(new FlowLayout());
      JFrame.setDefaultLookAndFeelDecorated(true);
      JFrame frame = new JFrame("Are you sure?");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    changeLocale();
  });
  configSaveDirLabel.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
      return;
    configSaveDirLabel.setText(Utils.shortenPath(chosenPath));
    Utils.setConfigString("rips.directory", chosenPath);
  });

代码示例来源: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: skylot/jadx

String name = node.makeLongString();
final JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 3, 0));
panel.setOpaque(false);
final JLabel label = new JLabel(name);
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
label.setIcon(node.getIcon());
final JButton button = new JButton();
button.setIcon(ICON_CLOSE_INACTIVE);
button.setRolloverIcon(ICON_CLOSE);
button.setRolloverEnabled(true);
button.setOpaque(false);
button.addActionListener(e -> closeCodePanel(contentPanel));
panel.addMouseListener(new MouseAdapter() {
  @Override
  public void mouseClicked(MouseEvent e) {

代码示例来源: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);
JLabel contributorsLabel = new JLabel("contributors (alphabetically ordered):", JLabel.CENTER);
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
if (UrlDisplayHelper.displayUrlIsSupported()) {
  JButton homePageButton = new JButton("Homepage");
  homePageButton.setActionCommand(ACTION_HOMEPAGE);
  homePageButton.addActionListener(this);
  buttonPanel.add(homePageButton);
okButton.addActionListener(this);
buttonPanel.add(okButton);
getContentPane().add("North", logoPanel);
getContentPane().add("Center", versionPanel);
getContentPane().add("South", buttonPanel);
pack();
setResizable(false);

代码示例来源:origin: wiztools/rest-client

jb_add.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
jb_addAndClose.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
jb_cancel.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
c.setLayout(new BorderLayout());
  JPanel jp = new JPanel(new BorderLayout());
  c.add(jp, BorderLayout.NORTH);
  c.add(jsp, BorderLayout.CENTER);
  JPanel jp = new JPanel();
  jp.setLayout(new FlowLayout(FlowLayout.RIGHT));
  jp.add(jb_cancel);
  jp.add(jb_add);

代码示例来源:origin: pmd/pmd

ASTPanel astPanel = new ASTPanel(model);
xPathPanel = new XPathPanel(model);
getContentPane().setLayout(new BorderLayout());
JSplitPane editingPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sourcePanel, astPanel);
editingPane.setResizeWeight(0.5d);
JPanel interactionsPane = new JPanel(new BorderLayout());
interactionsPane.add(xPathPanel, BorderLayout.SOUTH);
interactionsPane.add(editingPane, BorderLayout.CENTER);
getContentPane().add(interactionsPane, BorderLayout.CENTER);
JButton compileBtn = new JButton(NLS.nls("MAIN.FRAME.COMPILE_BUTTON.TITLE"));
compileBtn.setActionCommand(ActionCommands.COMPILE_ACTION);
compileBtn.addActionListener(this);
evalBtn = new JButton(NLS.nls("MAIN.FRAME.EVALUATE_BUTTON.TITLE"));
evalBtn.setActionCommand(ActionCommands.EVALUATE_ACTION);
evalBtn.addActionListener(this);
evalBtn.setEnabled(false);
statusLbl = new JLabel();
statusLbl.setHorizontalAlignment(SwingConstants.RIGHT);
JPanel btnPane = new JPanel(new FlowLayout(FlowLayout.LEFT));
btnPane.add(compileBtn);
btnPane.add(evalBtn);
btnPane.add(statusLbl);
getContentPane().add(btnPane, BorderLayout.SOUTH);

代码示例来源:origin: skylot/jadx

private void initUI() {
    JLabel lbl = new JLabel(NLS.str("usage_dialog.label"));
    JLabel nodeLabel = new JLabel(this.node.makeLongString(), this.node.getIcon(), SwingConstants.LEFT);
    lbl.setLabelFor(nodeLabel);

    JPanel searchPane = new JPanel();
    searchPane.setLayout(new FlowLayout(FlowLayout.LEFT));
    searchPane.add(lbl);
    searchPane.add(nodeLabel);
    searchPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    initCommon();
    JPanel resultsPanel = initResultsTable();
    JPanel buttonPane = initButtonsPanel();

    Container contentPane = getContentPane();
    contentPane.add(searchPane, BorderLayout.PAGE_START);
    contentPane.add(resultsPanel, BorderLayout.CENTER);
    contentPane.add(buttonPane, BorderLayout.PAGE_END);

    setTitle(NLS.str("usage_dialog.title"));
    pack();
    setSize(800, 500);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    setModalityType(ModalityType.MODELESS);
  }
}

代码示例来源:origin: wildfly/wildfly

cache.addChangeListener(model);
add(new JScrollPane(table));
JPanel key=new JPanel(new FlowLayout(FlowLayout.LEFT));
key.add(new JLabel("Key  "));
key.add(key_field);
add(key);
JPanel value=new JPanel(new FlowLayout(FlowLayout.LEFT));
value.add(new JLabel("Value"));
JPanel repl_count=new JPanel(new FlowLayout(FlowLayout.LEFT));
repl_count.add(new JLabel("Replication count"));
JPanel timeout=new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel prefix=new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel keys=new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel size=new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel perf_repl_count=new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel perf_timeout=new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel perf_buttons=new JPanel(new FlowLayout(FlowLayout.LEFT));

代码示例来源:origin: log4j/log4j

protected JPanel createStatusArea() {
 JPanel statusArea = new JPanel();
 JLabel status =
   new JLabel("No log records to display.");
 _statusLabel = status;
 status.setHorizontalAlignment(JLabel.LEFT);
 statusArea.setBorder(BorderFactory.createEtchedBorder());
 statusArea.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
 statusArea.add(status);
 return (statusArea);
}

代码示例来源:origin: chewiebug/GCViewer

/**
 * @param gcResource resource to be tracked
 */
public GCModelLoaderView(GCResource gcResource) {
  super(new BorderLayout());
  setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  JPanel parserInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
  progressBar = new JProgressBar(0, 100);
  progressBar.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  progressBar.setVisible(true);
  progressBar.setValue(0);
  progressBar.setStringPainted(true);
  cancelButton = new JButton(new SquareIcon());
  cancelButton.setActionCommand(CMD_CANCEL);
  cancelButton.addActionListener(this);
  messageLabel = new JLabel();
  messageLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  messageLabel.setVisible(false);
  parserInfo.add(progressBar);
  parserInfo.add(cancelButton);
  parserInfo.add(messageLabel);
  add(parserInfo, BorderLayout.NORTH);
  JTextArea textArea = textAreaLogHandler.getTextArea();
  textArea.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
  JScrollPane textAreaScrollPane = new JScrollPane(textArea);
  textAreaScrollPane.setPreferredSize(new Dimension(700, 500));
  add(textAreaScrollPane, BorderLayout.CENTER);
  setGCResource(gcResource);
}

代码示例来源:origin: stackoverflow.com

JFrame frame = new JFrame();
frame.setTitle("Welcome!");
frame.setSize(520, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel heroShotPanel = new JPanel();
JLabel heroShot = new JLabel(heroShotImage);
heroShotPanel.add(heroShot);
JPanel submitPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton start = new JButton("Start");
start.setToolTipText("Click to use library");
submitPanel.add(start);
frame.getContentPane().add(heroShotPanel, BorderLayout.NORTH);
frame.getContentPane().add(submitPanel, BorderLayout.SOUTH);
frame.setVisible(true);
frame.getRootPane().setDefaultButton(start);
start.requestFocus();

代码示例来源:origin: stackoverflow.com

final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JPanel plafComponents = new JPanel(
  new FlowLayout(FlowLayout.RIGHT, 3,3));
plafComponents.setBorder(
  new TitledBorder("FlowLayout(FlowLayout.RIGHT, 3,3)") );
plafComponents.add(plafChooser);
plafComponents.add(pack);
gui.add(plafComponents, BorderLayout.NORTH);
  new TitledBorder("GridLayout(0,2,3,3)") );
JButton addNew = new JButton("Add Another Label");
    labels.add( new JLabel("Label " + ++labelCount) );
frame.setContentPane(gui);
frame.pack();

代码示例来源:origin: wiztools/rest-client

private void initComponents() {
  JPanel jp = new JPanel();
  jp.setLayout(new BorderLayout());
  jta_error = new JTextArea(10, 40);
  jta_error.setEditable(false);
  JScrollPane jsp = new JScrollPane(jta_error);
  jp.add(jsp, BorderLayout.CENTER);
  
  JPanel jp_south = new JPanel();
  jp_south.setLayout(new FlowLayout(FlowLayout.CENTER));
  
  jb_ok = new JButton("Ok");
  jb_ok.setMnemonic('o');
  getRootPane().setDefaultButton(jb_ok);
  jb_ok.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
      jb_okActionPerformed(event);
    }
  });
  jp_south.add(jb_ok);
  
  jp.add(jp_south, BorderLayout.SOUTH);
  this.setContentPane(jp);
  pack();
}

相关文章