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

x33g5p2x  于2022-01-15 转载在 其他  
字(15.7k)|赞(0)|评价(0)|浏览(554)

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

ActionEvent介绍

[英]A semantic event which indicates that a component-defined action occured. This high-level event is generated by a component (such as a Button) when the component-specific action occurs (such as being pressed). The event is passed to every every ActionListener object that registered to receive such events using the component's addActionListener method.

The object that implements the ActionListener interface gets this ActionEvent when the event occurs. The listener is therefore spared the details of processing individual mouse movements and mouse clicks, and can instead process a "meaningful" (semantic) event like "button pressed".

Note: The timestamps used by this class report the difference, measured in milliseconds, between the time of the event and midnight, January 1, 1970 UTC (similar to java.lang.System.currentTimeMillis).
[中]一个语义事件,指示发生了组件定义的操作。此高级事件由组件(如按钮)在特定于组件的操作发生(如被按下)时生成。该事件将传递给使用组件的addActionListener方法注册以接收此类事件的每个ActionListener对象。
当事件发生时,实现ActionListener接口的对象将获得此ActionEvent。因此,监听器不需要处理单个鼠标移动和鼠标单击的细节,而是可以处理“有意义”(语义)事件,如“按下按钮”。
注意:此类使用的时间戳报告事件时间与UTC 1970年1月1日午夜之间的差异(以毫秒为单位)(类似于java.lang.System.currentTimeMillis)

代码示例

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

public class Calc extends JFrame implements ActionListener {

  private Button button1;

  public Calc()
  {
    super();
    this.setSize(100, 100);
    this.setVisible(true);

    this.button1 = new JButton("1");
    this.button1.addActionListener(this);
    this.add(button1);
  }

  public static void main(String[] args) {

    Calc calc = new Calc();
    calc.setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == button1)
  }  

}

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

Control control = new Control(model, view);
  JLabel label = new JLabel("Guess what color!", JLabel.CENTER);
  this.add(label, BorderLayout.NORTH);
  this.add(view, BorderLayout.CENTER);
  this.add(control, BorderLayout.SOUTH);
private JButton reset = new JButton("Reset");
  this.view = view;
  this.add(reset);
  reset.addActionListener(new ButtonHandler());
    String cmd = e.getActionCommand();
    if ("Reset".equals(cmd)) {
      model.reset();
  JPanel panel = new JPanel();
  for (Piece p : Piece.values()) {
    PieceButton pb = new PieceButton(p);
    pb.addActionListener(new ButtonHandler());
    panel.add(pb);
    this.setIcon(new ColorIcon(16, piece.color));

代码示例来源:origin: kiegroup/optaplanner

private JComponent createQuickOpenPanel(JList<QuickOpenAction> listPanel, String title, List<File> fileList) {
  listPanel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  listPanel.addListSelectionListener(event -> {
    if (event.getValueIsAdjusting()) {
      return;
    }
    int selectedIndex = listPanel.getSelectedIndex();
    if (selectedIndex < 0) {
      return;
    }
    QuickOpenAction action = listPanel.getModel().getElementAt(selectedIndex);
    action.actionPerformed(new ActionEvent(listPanel, -1, null));
  });
  refreshQuickOpenPanel(listPanel, fileList);
  JScrollPane scrollPane = new JScrollPane(listPanel);
  scrollPane.getVerticalScrollBar().setUnitIncrement(25);
  scrollPane.setMinimumSize(new Dimension(100, 80));
  // Size fits into screen resolution 1024*768
  scrollPane.setPreferredSize(new Dimension(180, 200));
  JPanel titlePanel = new JPanel(new BorderLayout());
  titlePanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
  JLabel titleLabel = new JLabel(title);
  titleLabel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
  titlePanel.add(titleLabel, BorderLayout.NORTH);
  titlePanel.add(scrollPane, BorderLayout.CENTER);
  return titlePanel;
}

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

@Override
 public void actionPerformed(ActionEvent e) {
  if (focusOwner == null)
   return;
  String action = e.getActionCommand();
  Action a = focusOwner.getActionMap().get(action);
  if (a != null) {
   a.actionPerformed(new ActionEvent(focusOwner,
     ActionEvent.ACTION_PERFORMED,
     null));
  }
 }
}

代码示例来源:origin: JetBrains/ideavim

@Nullable
public static KeyStroke convert(@NotNull ActionEvent event) {
 String cmd = event.getActionCommand();
 int mods = event.getModifiers();
 if (cmd != null && cmd.length() > 0) {
  char ch = cmd.charAt(0);
  if (ch < ' ') {
   if (mods == KeyEvent.CTRL_MASK) {
    return KeyStroke.getKeyStroke(KeyEvent.VK_A + ch - 1, mods);
   }
  }
  else {
   return KeyStroke.getKeyStroke(new Character(ch), mods);
  }
 }
 return null;
}

代码示例来源:origin: com.h2database/h2

/**
   * INTERNAL
   */
//## AWT ##
  @Override
  public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if ("exit".equals(command)) {
      shutdown();
    } else if ("console".equals(command)) {
      startBrowser();
    } else if ("status".equals(command)) {
      showWindow();
    } else if (startBrowser == e.getSource()) {
      // for some reason, IKVM ignores setActionCommand
      startBrowser();
    }
  }
//*/

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

private void setupHandlers() {
  ripButton.addActionListener(new RipButtonHandler());
  ripTextfield.addActionListener(new RipButtonHandler());
  ripTextfield.getDocument().addDocumentListener(new DocumentListener() {
  stopButton.addActionListener(event -> {
    if (ripper != null) {
      ripper.stop();
      isRipping = false;
      stopButton.setEnabled(false);
      statusProgress.setValue(0);
      statusProgress.setVisible(false);
    logPanel.setVisible(!logPanel.isVisible());
    emptyPanel.setVisible(!logPanel.isVisible());
      JFrame.setDefaultLookAndFeelDecorated(true);
      JFrame frame = new JFrame("Are you sure?");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  });
  configLogLevelCombobox.addActionListener(arg0 -> {
    String level = ((JComboBox) arg0.getSource()).getSelectedItem().toString();
    setLogLevel(level);
  });
  configSelectLangComboBox.addActionListener(arg0 -> {
    String level = ((JComboBox) arg0.getSource()).getSelectedItem().toString();
    rb = Utils.getResourceBundle(level);
    changeLocale();

代码示例来源:origin: ron190/jsql-injection

this.setLayout(borderLayoutPreferences);
final JButton buttonCheckIp = new JButton("Check your IP");
buttonCheckIp.addActionListener(new ActionCheckIP());
buttonCheckIp.setToolTipText(
  "<html><b>Verify what public IP address is used by jSQL</b><br>"
  + "Usually it's your own public IP if you don't use a proxy. If you use a proxy<br>"
  if (actionEvent.getSource() != this.checkboxProcessCsrf) {
    this.checkboxProcessCsrf.setSelected(!this.checkboxProcessCsrf.isSelected());
});
JPanel panelTamperingPreferences = new JPanel();
panelTamperingPreferences.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
panelTampering.setBorder(BorderFactory.createEmptyBorder(10, 15, 0, 15));
panelTampering.add(new JLabel("<html><b>Tampering</b> / SQL expression alteration to bypass Web Application Firewall</html>"), BorderLayout.NORTH);
panelTampering.add(panelTamperingPreferences, BorderLayout.CENTER);
  if (actionEvent.getSource() != this.checkboxIsCheckingAllParam) {
    this.checkboxIsCheckingAllParam.setSelected(!this.checkboxIsCheckingAllParam.isSelected());

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

String actionCommand = e.getActionCommand();
  System.out.println("Key Binding: " + actionCommand);
ArrowTest mainPanel = new ArrowTest();
JFrame frame = new JFrame("ArrowTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

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

JButton enter = new JButton("Enter");
JLabel conceptLabel = new JLabel("Concept: ");
  setSize(350, 180);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
  JPanel pane = new JPanel();          
  pane.add(conceptLabel);
  pane.add(concept);
  pane.add(enter);
  add(pane);
  enter.addActionListener(listener);
  setVisible(true);            
  if(event.getSource() == enter){
    conceptLabel.setText(concept.getText());

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

editorComponent = new JButton();
editorComponent.setBackground(Color.white);
editorComponent.setBorderPainted(false);
editorComponent.setContentAreaFilled( false );
    popup.setLocation(p.x, p.y + editorComponent.getSize().height);
    popup.show();
    fireEditingStopped();
editorComponent.setText( currentText );
return editorComponent;
  textArea.getInputMap().put(keyStroke, "none");
  JScrollPane scrollPane = new JScrollPane( textArea );
  getContentPane().add( scrollPane );
  JButton cancel = new JButton("Cancel");
  cancel.addActionListener( this );
  JButton ok = new JButton("Ok");
  ok.setPreferredSize( cancel.getPreferredSize() );
  ok.addActionListener( this );
  if ("Ok".equals( e.getActionCommand() ) )
table.getColumnModel().getColumn(1).setCellEditor( popupEditor );

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

JFrame frame = new JFrame("This is a Frame");
button = new JButton("Button");
button.addActionListener(new MyAction());
frame.add(button);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String text = (String)e.getActionCommand();
if (text.equals("Button2")){
button.setText("I am Sectona");
button.setText("Click Me");

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

getEngineByExtension("js");
ui = new JPanel(new BorderLayout(2, 2));
controls = new ArrayList<JButton>();
io.setHorizontalAlignment(SwingConstants.TRAILING);
io.setFocusable(false);
text.add(io);
JButton equals = new JButton("=");
JButton b = new JButton(text);
String command = ae.getActionCommand();
if (command.equals("C")) {
  io.setText("");
  String s = button.getText();
  if (text.endsWith(s)
      || (s.equals("=")
if (b != null) {
  b.requestFocusInWindow();
  b.doClick();

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

import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Alignment;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.LayoutStyle.ComponentPlacement;import javax.swing.border.EmptyBorder;import javax.swing.filechooser.FileFilter;import javax.swing.filechooser.FileNameExtensionFilter;public class ConfigureDialog extends JDialog implements ActionListener{private static final long serialVersionUID=1L;private final JPanel contentPanel=new JPanel();private JTextField driverPathTextField;private JLabel lblDriverPath;private JButton btnBrowse;public static void main(String[]args){try{ConfigureDialog dialog=new ConfigureDialog(new JFrame());dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);dialog.setVisible(true);}catch(Exception e){e.printStackTrace();}}
public ConfigureDialog(JFrame parent){super(parent,"",true);if(parent!=null){Dimension parentSize=parent.getSize();Point p=parent.getLocation();setLocation(p.x+parentSize.width+100,p.y+parentSize.height/1);}
setBounds(100,100,479,141);getContentPane().setLayout(new BorderLayout());contentPanel.setBorder(new EmptyBorder(5,5,5,5));getContentPane().add(contentPanel,BorderLayout.CENTER);{lblDriverPath=new JLabel("Driver Path : ");}
{driverPathTextField=new JTextField(System.getProperty("web.ie.driver"));driverPathTextField.setColumns(10);}
btnBrowse=new JButton("Browse");GroupLayout gl_contentPanel=new GroupLayout(contentPanel);gl_contentPanel.setHorizontalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addContainerGap().addComponent(lblDriverPath).addPreferredGap(ComponentPlacement.RELATED).addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addComponent(btnBrowse).addComponent(driverPathTextField,GroupLayout.DEFAULT_SIZE,207,Short.MAX_VALUE)).addContainerGap()));gl_contentPanel.setVerticalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addGap(5).addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE).addComponent(driverPathTextField,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE).addComponent(lblDriverPath)).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(btnBrowse).addContainerGap(21,Short.MAX_VALUE)));contentPanel.setLayout(gl_contentPanel);{JPanel buttonPane=new JPanel();buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));getContentPane().add(buttonPane,BorderLayout.SOUTH);{JButton okButton=new JButton("OK");okButton.setActionCommand("OK");okButton.addActionListener(this);buttonPane.add(okButton);getRootPane().setDefaultButton(okButton);}
{JButton cancelButton=new JButton("Cancel");cancelButton.setActionCommand("Cancel");cancelButton.addActionListener(this);buttonPane.add(cancelButton);}}
btnBrowse.addActionListener(this);}@Override
public void actionPerformed(ActionEvent e){if("Cancel".contains(e.getActionCommand())){dispose();}else if("Browse".contains(e.getActionCommand())){JFileChooser fileopen=new JFileChooser();FileFilter filter=new FileNameExtensionFilter("exe file","exe");fileopen.addChoosableFileFilter(filter);fileopen.setAcceptAllFileFilterUsed(false);fileopen.setFileFilter(filter);fileopen.setFileSelectionMode(JFileChooser.FILES_ONLY);int ret=fileopen.showOpenDialog(this);if(ret==JFileChooser.APPROVE_OPTION){File file=fileopen.getSelectedFile();System.out.println(file);driverPathTextField.setText(file.getPath());}}else if("OK".contains(e.getActionCommand())){System.setProperty("web.ie.driver",driverPathTextField.getText());dispose();}}}

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

public void start() throws Exception {
  mainFrame=new JFrame("Chat demo");
  mainFrame.setPreferredSize(new Dimension(600,600));
  mainFrame.setBackground(Color.white);
  mainFrame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
  leaveButton=new JButton("Leave");
  leaveButton.setPreferredSize(new Dimension(150, 30));
  buttons.add(leaveButton);
  leaveButton.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
      stomp_client.send(USER_LEFT, USER, username);
  txtField.setToolTipText("type and then press enter to send");
  txtField.addActionListener(e -> {
    String cmd=e.getActionCommand();
    if(cmd != null && !cmd.isEmpty()) {
      send(txtField.getText());

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

JButton button1;
 public static void main(String[] args) {

  JFrame calcFrame = new JFrame();

  calcFrame.setSize(100, 100);
  calcFrame.setVisible(true);

  button1 = new JButton("1");
  button1.addActionListener(this);

  calcFrame.add(button1);
}

public void actionPerformed(ActionEvent e) {
  if(e.getSource() == button1)
}

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

String[] data = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
JPanel panel = new JPanel(new GridLayout(0, 1));
MyButtonGroup myBtnGrp = new MyButtonGroup();
myBtnGrp.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
   System.out.println("Action Command is: " + e.getActionCommand());
  radioBtn.setActionCommand(text);
  myBtnGrp.add(radioBtn);
  panel.add(radioBtn);
JFrame frame = new JFrame("MyButtonGroupTest");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

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

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < letters.length; i++) {
  buttons[i] = new JButton(letters[i]);
  buttons[i].addActionListener(this);
  add(buttons[i]);
add(text);
pack();
setVisible(true);
text.append(event.getActionCommand());

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

JFreeChart chart = createChart(dataset);
final JButton run = new JButton(STOP);
run.addActionListener(new ActionListener() {
    String cmd = e.getActionCommand();
    if (STOP.equals(cmd)) {
      timer.stop();
      run.setText(START);
    } else {
      timer.start();
      run.setText(STOP);
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(run);
btnPanel.add(combo);
this.add(btnPanel, BorderLayout.SOUTH);

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

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  btn = new JButton("Open the other JFrame!");
  btn.addActionListener(this);
  btn.setActionCommand("Open");
  add(btn);
  pack();
public void actionPerformed(ActionEvent e)
  String cmd = e.getActionCommand();
    dispose();
    new AnotherJFrame();
    public void run()
      new StartupWindow().setVisible(true);

相关文章