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

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

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

ActionListener介绍

[英]The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.
[中]用于接收操作事件的侦听器接口。对处理动作事件感兴趣的类实现此接口,并且使用组件的addActionListener方法向组件注册使用该类创建的对象。当动作事件发生时,将调用该对象的actionPerformed方法。

代码示例

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

for(ActionListener a: buttonExample.getActionListeners()) {
  a.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null) {
     //Nothing need go here, the actionPerformed method (with the
     //above arguments) will trigger the respective listener
  });
}

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

JFrame frame = new JFrame("Testing");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.add(new TestPane());
  frame.pack();
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
ActionMap am = ta.getActionMap();
Action proxy = am.get("insert-break");
am.put("insert-break", new AbstractAction() {
  @Override
  public void actionPerformed(ActionEvent e) {
    proxy.actionPerformed(e);
setLayout(new BorderLayout());
add(ta);

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

Color color = (Color) newValue;
  editor.setForeground(color);
  editor.setText(String.valueOf(newValue));
} else {
    Color color = Color.decode(newValue.toString());
    editor.setForeground(color);
    editor.setText(String.valueOf(newValue));
  } catch (NumberFormatException e) {
for (int i = listeners.length - 2; i >= 0; i -= 2) {
  if (listeners[i] == ActionListener.class) {
    ActionEvent actionEvent = new ActionEvent(editor, ActionEvent.ACTION_PERFORMED, color.toString());
    ((ActionListener) listeners[i + 1]).actionPerformed(actionEvent);
if (value instanceof Color) {
  renderer.setBackground(Color.white);
  renderer.setForeground((Color) value);
frame.setSize(300, 200);
frame.setVisible(true);

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

header.getActionMap().put("toggleSortOrder2", new AbstractAction() {
  public void actionPerformed(ActionEvent e) {
    e.setSource(table);
    table.getActionMap().get("focusHeader").actionPerformed(e);
    if (selectedColumn != null) {
      int i = header.getColumnModel().getColumnIndex(selectedColumn);
InputMap im = header.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0), "toggleSortOrder2");
header.addMouseListener(new MouseAdapter() {
header.setToolTipText("Sorting by F8");
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(table));
return p;
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new SelectedColumnTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);

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

endAction = table.getActionMap().get("selectLastRow");
  endAction.actionPerformed( e );
else
  invokeOriginalAction( e );
new UpAction(table, KeyStroke.getKeyStroke("UP"));
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane(table) );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );

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

editorPane.setEditorKit(styledEditorKit);
JScrollPane scrollpane = new JScrollPane(editorPane);
scrollpane.setPreferredSize(new Dimension(500, 400));
JPanel comboPanel = new JPanel();
comboPanel.add(fontBox);
setLayout(new BorderLayout());
add(scrollpane, BorderLayout.CENTER);
add(comboPanel, BorderLayout.SOUTH);
   Action fontAction = new StyledEditorKit.FontSizeAction(String
      .valueOf(size), size);
   fontAction.actionPerformed(e);
JFrame frame = new JFrame("EditorPaneFun");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new EditorPaneFun());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

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

InputMap inputMap = textArea.getInputMap(condition);
ActionMap actionMap = textArea.getActionMap();
Action originalUpAction = actionMap.get(upKey); // and get the action map's original action for this key
actionMap.put(upKey, newUpAction); // and set this into our ActionMap
add(new JScrollPane(textArea));
    originalUpAction.actionPerformed(e);
TextAreaTrapUp mainPanel = new TextAreaTrapUp();
JFrame frame = new JFrame("TextAreaTrapUp");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

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

this.setOpaque(false);
this.setFocusable(true);
this.setPreferredSize(new Dimension(50 * TILE, 50 * TILE));
JFrame f = new JFrame("ScrollAction");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane(this);
final ScrollTimer left = new ScrollTimer(scrollPane, "scrollLeft");
f.add(scrollPane);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
  this.cmd = action;
  this.timer = new Timer(DELAY, this);
  this.action = scrollPane.getActionMap().get(action);
  this.scrollPane = scrollPane;
public void actionPerformed(ActionEvent e) {
  if (count++ < N) {
    action.actionPerformed(new ActionEvent(scrollPane, 0, cmd));
  } else {
    timer.stop();

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

@Override
public void run() {
 JFrame frame = new JFrame("FilterTree");
 final FilterPair pair = new FilterPair( frame ); 
 final JScrollPane jsp = new JScrollPane( pair.getCurrCoupling().m_tree );
   pair.toggle_filter();
   jsp.getViewport().setView( pair.getCurrCoupling().m_tree );
   jsp.requestFocus();
  }};
 JPanel cpane = (JPanel)frame.getContentPane(); 
 cpane.getActionMap().put("toggle between views", toggle_between_views );
 InputMap new_im = new InputMap();
 new_im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, InputEvent.CTRL_DOWN_MASK), "toggle between views");
 cpane.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, new_im);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.getContentPane().add(jsp);
 frame.pack();
 frame.setBounds(50, 50, 800, 500);
 frame.setVisible(true);
 toggle_between_views.actionPerformed( null );

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

private JFrame frame;
public Test() {
  frame = new JFrame();
  initActions();
  frame.setLayout(new BorderLayout());
  frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
      frame.getRootPane().getActionMap().get(ACTION_CLOSE).actionPerformed(null);
  content.add(new JLabel("Creature"));
  JButton badger = new JButton("Badger");
  badger.setName("badger");
  JButton close = new JButton(frame.getRootPane().getActionMap().get(ACTION_CLOSE));
  buttonPanel.add(close);
  frame.add(buttonPanel, BorderLayout.SOUTH);
  frame.setSize(200, 150);
  frame.pack();
  frame.setLocationByPlatform(true);
  frame.setVisible(true);
  frame.getRootPane().getActionMap().put(ACTION_CLOSE, close);

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

count = String.valueOf(input);
System.out.println("Update Count " + count);
refreshListener.actionPerformed(new ActionEvent(this, input, "Update"));
JFrame f = new JFrame("JUST A TEST");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
setLayout(null);// 
helpButton = new Button("?");
helpButton.setLocation(217, 8); // set X, Y
helpButton.setSize(16, 14); //Set Size X, Y //
add(helpButton);
setBackground(Color.black);
helpButton.setBackground(Color.black);
screenColor = Color.black;

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

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] headers = new String[]{"apples", "bananas"};
String[][] data = new String[][]{{"1", "2"}, {"3", "4"}, {"5", "6"}};
  public void editingStopped(ChangeEvent e) {
    super.editingStopped(e);
    this.getActionMap().get(name).actionPerformed(
      new ActionEvent(this, ActionEvent.ACTION_FIRST, name));
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
  .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), name);
table.setCellSelectionEnabled(true);
f.add(new JScrollPane(table));
f.pack();
f.setSize(new Dimension(320, 240));
f.setLocationRelativeTo(null);
f.setVisible(true);

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

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(200, 200);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
setLayout(new BorderLayout());
add(body);
JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
if (e.getActionCommand().equals("counterFinished")) {
  System.out.println("Counter finished");
  Pocitadlo tmp = (Pocitadlo) e.getSource();
if (e.getActionCommand().equals("valueTimer")) {
  value--;
  System.out.println("value = " + value);
    actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "counterTick"));
    isRunning = false;
    valueTimer.stop();
    actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "counterFinished"));
    blinkTimer.stop();
  } else if (value < maxValue / 2 && !blinkTimer.isRunning()) {

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

private JButton button = new JButton("Close Meeee");
private JPanel myPanel = new JPanel();
private JFrame frame = new JFrame();
  myPanel.add(button);
  frame.add(myPanel);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
      KeyStroke.getKeyStroke("ENTER"), "clickENTER");
  frame.getRootPane().getActionMap().put("clickENTER", updateBol());
  frame.pack();
  frame.setVisible(true);
  updateCol().setEnabled(false);
  updateDol().setEnabled(false);
      System.out.println("updateBol is " + updateCol().isEnabled());
      System.out.println("updateDol is " + updateDol().isEnabled());
      updateCol().actionPerformed(e);
      updateDol().actionPerformed(e);

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

this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 1, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(one);
panel.add(two);
panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
  .put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
panel.getActionMap().put("up", new AbstractAction() {
      panel.getAction().actionPerformed(e);
this.add(panel);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);

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

JFrame frame=new JFrame("Clock");
JLabel l1=new JLabel(), l2=new JLabel();
ActionListener updater=ev->{
  l2.setText(format(toEvening, "evening"));
};
updater.actionPerformed(null);
frame.getContentPane().add(l1, BorderLayout.NORTH);
frame.getContentPane().add(l2, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
new Timer(1000, updater).start();

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

baisvalue.setVisible(true); //label
      plusbais.setVisible(true);  //button
      minisbais.setVisible(true); //button
add(checkbox);
add(baisvalue);
add(plusbais);
add(minisbais);
l.actionPerformed(new ActionEvent(checkbox, ActionEvent.ACTION_PERFORMED, ""));
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPanel());
frame.pack();
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

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

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
p1.setLayout(new GridLayout(1, 2));
p1.add(p2);
p2.add(p3);
p2.setLayout(new GridLayout(3, 1));
add(p1);
pack();
java.awt.EventQueue.invokeLater(new Runnable() {
 public void run() {
  new Gui().setVisible(true);
if (!("Delete".equals(e.getActionCommand()))) {
 System.out.println("doing " + e.getActionCommand());
} else if (delegate != null) {
 delegate.actionPerformed(e);
if ("Delete".equals(e.getActionCommand())) {
 deleteNext = true;
} else if (deleteNext) {
 deleteNext = false;
} else if (delegate != null) {
 delegate.actionPerformed(e);

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

JFrame frm = new JFrame("Test");
final JComboBox<ColorData> cmb = new JComboBox<>(ColorData.values());
cmb.setRenderer(new ColorRenderer());
final JPanel p = new JPanel();
p.setOpaque(true);
p.setPreferredSize(new Dimension(200, 100));
ActionListener l = new ActionListener() {
    ColorData cd = (ColorData) cmb.getSelectedItem();
    if (cd != null) {
      p.setBackground(cd.getColor());
l.actionPerformed(null); // update current background
frm.add(cmb, BorderLayout.NORTH);
frm.add(p);
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.pack();
frm.setLocationRelativeTo(null);
frm.setVisible(true);

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

public void actionPerformed(ActionEvent e) {
  if (action1 != null) {
    action1.actionPerformed(e);
    action2.actionPerformed(e);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final ActionListener ctrlVAction = editorPane.getActionForKeyStroke(ctrlV);
editorPane.registerKeyboardAction(new CombinedAction(ctrlVAction, new ActionListener() {
scroll1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scroll1);
frame.setSize(400, 400);
frame.setVisible(true);

相关文章

ActionListener类方法