java.awt.event.ActionEvent.getActionCommand()方法的使用及代码示例

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

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

ActionEvent.getActionCommand介绍

[英]Returns the command string associated with this action. This string allows a "modal" component to specify one of several commands, depending on its state. For example, a single button might toggle between "show details" and "hide details". The source object and the event would be the same in each case, but the command string would identify the intended action.
[中]返回与此操作关联的命令字符串。此字符串允许“模态”组件根据其状态指定多个命令之一。例如,单个按钮可能会在“显示详细信息”和“隐藏详细信息”之间切换。源对象和事件在每种情况下都是相同的,但命令字符串将标识预期的操作。

代码示例

代码示例来源: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();
    public void run()
      new StartupWindow().setVisible(true);

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

leaveButton=new JButton("Leave");
leaveButton.setPreferredSize(new Dimension(150, 30));
buttons.add(leaveButton);
sendButton=new JButton("Send");
sendButton.setPreferredSize(new Dimension(150, 30));
buttons.add(sendButton);
clearButton=new JButton("Clear");
clearButton.setPreferredSize(new Dimension(150, 30));
clearButton.addMouseListener(new MouseAdapter() {
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

JTextField[] desc=new JTextField[20];
JButton more=new JButton("More");
 Container c=getContentPane();
 public void actionPerformed(ActionEvent a1)
 {
 String s1=a1.getActionCommand();
 String s="";
     for(i=0;i<cnt;i++)
   s=s+desc[i].getText();
  if(s1.equals("More"))
 {
        System.out.println("Created");
        desc[i]=new JTextField();
        desc[i].setBounds(50,y,150,30);
        c.add(desc[i]);
 }
 }

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

Main main =  new Main();
main.textField= new JTextField("Sample");
JButton btn1 = new JButton("Button1");
btn1.addActionListener(main);
JButton btn2 = new JButton("Button2");
btn2.addActionListener(main);
frame.setTitle("Simple example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.add(main);
  textField.setText(e.getActionCommand());
  repaint();

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

System.out.printf("action command: \"%s\"%n", e.getActionCommand());
panel.add(new JLabel("Field 1:"));
panel.add(field1);
panel.add(new JLabel("Field 2:"));
panel.add(field2);

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

public void run() {
  JFrame f = new JFrame();
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.add(new MainPanel());
  f.pack();
  f.setLocationRelativeTo(null);
  f.setVisible(true);
  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();
  for (Piece p : Piece.values()) {
    PieceButton pb = new PieceButton(p);
    pb.addActionListener(new ButtonHandler());
    panel.add(pb);

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

JButton myButton = new JButton();
myButton.setActionCommand("myButtonCommand");

public void actionPerformed(ActionEvent ae) {
 String actionCommand = ae.getActionCommand();
 if (actionCommand.equals("myButtonCommand")) {
  // do something...
 }
}

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

public class buttons2 implements ActionListener{
 static JLabel jlab;
 public void actionPerformed(ActionEvent Ae){
              jlab.setText(Ae.getActionCommand());
            }
 public static void main(String args[]){
   buttons2 s = new  buttons2();
   JFrame j = new JFrame("HP");
     j.setSize(300,300);
     j.setLayout(new FlowLayout());
     j.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
     j.setVisible(true);    
     jlab=new JLabel("Here");
     j.add(jlab);
     JButton j1=new JButton("Button1");
     j1.setActionCommand("Your pressed Button 1");
     j1.addActionListener(s);
     j.add(j1);
     JButton j2=new JButton("Button2");
     j2.setActionCommand("Your pressed Button 2");
     j2.addActionListener(s);
     j.add(j2);
   }
 }

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

class ButtonClickActionK implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    String str = e.getActionCommand();
    Component source = (Component) e.getSource();

    switch (str) {

    case "TextButton":
      Container parent = source.getParent();
      JXCollapsiblePane testp = new JXCollapsiblePane();
      testp.add(new JLabel("dummy, just to see we are open"));
      parent.add(testp);
      parent.revalidate();
      break;

    default:
      JOptionPane.showMessageDialog(source, 
        "Erm, this is embarrasing! Something went (horribly) wrong...");
    }
  }
}

// usage
JButton newButton= new JButton("TextButton");
newButton.addActionListener(new ButtonClickActionK());
// note: _not_ using a class field
JComponent newPanel = new JPanel(new VerticalLayout());
newPanel.add(newButton);

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

import java.awt.*;
import java.applet.Applet;

class Example extends Applet implements ActionListener
{
  TextField txt = new TextField(10);
  Button goButton = new Button("Go");
  String data = new String ();

  public void init ()
  {
    add(txt);
    add(goButton);
    goButton.addActionListener(this);
  }

  public void actionPerformed (ActionEvent e)
  {
    String cmd = e.getActionCommand();

    if (cmd.equals("Go"))
    {
      // preserve data
      data = txt.getText();

      repaint();
    }
  }
}

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

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

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

JButton hello = new JButton("Hello");
hello.setActionCommand(Actions.HELLO.name());
hello.addActionListener(instance);
frame.add(hello);

JButton goodbye = new JButton("Goodbye");
goodbye.setActionCommand(Actions.GOODBYE.name());
goodbye.addActionListener(instance);
frame.add(goodbye);


 ...
 }

@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand() == Actions.HELLO.name()) {
  JOptionPane.showMessageDialog(null, "Hello");
  } 
else if (evt.getActionCommand() == Actions.GOODBYE.name()) {
  JOptionPane.showMessageDialog(null, "Goodbye");
  }
}

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

public class buttons1 implements ActionListener{
   JLabel jlab;
   buttons1(){
       JFrame j = new JFrame("HP");
       j.setSize(300,300);
       j.setLayout(new FlowLayout());
       j.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
       j.setVisible(true);    
       jlab=new JLabel("Here");
       j.add(jlab);
       JButton j1=new JButton("Button1");
       j1.setActionCommand("Your pressed Button 1");
       j1.addActionListener(this );
       j.add(j1);
       JButton j2=new JButton("Button2");
       j2.setActionCommand("Your pressed Button 2");
       j2.addActionListener(this );
       j.add(j2);
   }
   public void actionPerformed(ActionEvent Ae){
        jlab.setText(Ae.getActionCommand());
   }
   public static void main(String args[]){
     new buttons1();
   } }

代码示例来源: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();
btnPanel.add(run);
btnPanel.add(combo);
this.add(btnPanel, BorderLayout.SOUTH);

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

JButton btn = new JButton();
btn.setText("Click");       
btn.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
    System.out.println(e.getSource());
    System.out.println(e.getActionCommand());
  }
});
btn.doClick();

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

frame.setSize(200, 100);
JButton hello = new JButton("Hello");
hello.setActionCommand(Actions.HELLO.name());
hello.addActionListener(instance);
frame.add(hello);
JButton goodbye = new JButton("Goodbye");
goodbye.setActionCommand(Actions.GOODBYE.name());
goodbye.addActionListener(instance);
frame.add(goodbye);
frame.setVisible(true);
if (evt.getActionCommand() == Actions.HELLO.name()) {
 JOptionPane.showMessageDialog(null, "Hello");
} else if (evt.getActionCommand() == Actions.GOODBYE.name()) {
 JOptionPane.showMessageDialog(null, "Goodbye");

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

JButton temp = new JButton("btn1");
temp.addActionListener(this);
temp.setActionCommand("Btn1");
btns.add(temp); //btn1

temp = new JButton("btn2");
temp.addActionListener(this);
temp.setActionCommand("Btn2");
btns.add(temp); //btn2

public void actionPerformed(ActionEvent e) {
  String command = e.getActionCommand();
  if (e.getActionCommand.equals("Btn1"))
    System.out.println("Button 1 pressed");
  if (e.getActionCommand.equals("Btn2"))
    System.out.println("Button 2 pressed");
}

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

for (String text : TEXTS) {
  JToggleButton toggleBtn = new JToggleButton(text);
  toggleBtn.addActionListener(btnListener);
  toggleBtn.setActionCommand(text);
  btnPanel.add(toggleBtn);
  btnGroup.add(toggleBtn);
otherPanel.add(textField ); // just to take focus elsewhere
add(btnPanel);
add(otherPanel);
  textField.setText(aEvt.getActionCommand());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

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

public static void main(String args[]){
  JButton btn1 = new JButton("btn1");
  btn1.setActionCommand("btn1");

  JButton btn2 = new JButton("btn2");
  btn2.setActionCommand("btn2");

  btn1.addActionListener(this);
  btn2.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent ae) {
  String sourceBtn = ae.getActionCommand();
  System.out.println(sourceBtn + "Button pressed!");
}

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

ActionListener listener = new ActionListener() {
  public void actionPerformed(ActionEvent e) {
   System.out.println("Button selected: " + e.getActionCommand());
  String text = String.format("[%d, %d]", i % SIDE, i / SIDE);
  JToggleButton btn = new JToggleButton(text);
  btn.addActionListener(listener);
  btnGroup.add(btn);
  add(btn);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

相关文章