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);
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());
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]); } }
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();
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);
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);
JButton myButton = new JButton(); myButton.setActionCommand("myButtonCommand"); public void actionPerformed(ActionEvent ae) { String actionCommand = ae.getActionCommand(); if (actionCommand.equals("myButtonCommand")) { // do something... } }
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); } }
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);
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(); } } }
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);
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"); } }
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(); } }
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);
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();
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");
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"); }
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);
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!"); }
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);