Hello guys, I created this article in order to talk about how to add buttons to a Java frame and keep adding one after another. Those new buttons also perform an action such as grouping all the button names and showing the names in the developer windows. Developers will come to a point where they have to add more of the elements to a Java application for a purpose and perform an action that a user wants to do. Hope this code will help you Java developers start off in performing advanced applications to better users.
As promised from the Youtube description, the code to run this application is below here:
________________________________________________________
import java.awt.Color;import java.awt.Color;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.HashMap;
import javax.swing.*; public class addbuttons implements ActionListener {private static final ActionListener addbuttons = null;public String ark;public String ara; public JButton kan;JButton[] wer;public static JFrame frame;
static ArrayList<JButton> bkk=new ArrayList<JButton>();
static ArrayList<String> bok=new ArrayList<String>();static ArrayList<Integer> bone;
public static JButton []qwe=new JButton[6];private static JButton JButton;public static int im;
static HashMap<Integer,String> hm;
public addbuttons(){ hm=new HashMap<Integer,String>(); kan=new JButton(); frame=new JFrame();frame.setTitle(“Adding Buttons”);FlowLayout lay=new FlowLayout();frame.setLayout(lay);frame.setSize(500,500);JButton butt=new JButton();butt.setText(“Look at Button”);frame.add(butt);JButton buaa=new JButton();buaa.addActionListener(e->anotherbutton());buaa.setText(“Add JButtons”);frame.add(buaa);frame.setVisible(true);
}
public static void main(String[]args){addbuttons tton=new addbuttons(); }
void add(){ String []arm={“one”,”two”,”three”, “four”, “five”}; for(String aa:arm){ ark+=aa.toString()+” “; } ara=ark.replace(“null”, “”); System.out.println(ara); }
static void addbuttonss(JButton[]eon){
int an=1;for(JButton cr:eon){cr=new JButton(); cr.setText(“NEW ERA Mode “+an);frame.add(cr);cr.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent g){ if(g.getActionCommand().contains(“Mode 5”)||g.getActionCommand().contains(“Mode 2”)){ //Execute when button is pressed System.out.println(g.getActionCommand()); //System.out.println(“Print” +btnarray[i]); } }});an++;} }
public static void addbuttone(JButton[]eri){bkk.add(new JButton());bkk.add(new JButton());bkk.add(new JButton());bkk.add(new JButton());bkk.add(new JButton());bkk.add(new JButton());bkk.add(new JButton());
im=0;
for(JButton sz:bkk){sz.setText(“New Loop “+im);frame.add(sz);sz.addActionListener(e-> message(e.getActionCommand()));im++;}
int an=1; for(JButton cra:eri){ cra=new JButton(); cra.setText(“NEW ERA Mode “+an); bok.add(“New ERA Mode “+an); cra.setBackground(Color.GREEN); an++; frame.add(cra); cra.addActionListener(e -> message(e.getActionCommand())); }
}
public static void anotherbutton(){
bkk.add(new JButton());
bkk.get(bkk.size()-1).setText(“Button Greatly “+ im);bkk.get(bkk.size()-1).setBackground(Color.blue);
bkk.get(bkk.size()-1).addActionListener(e->message(e.getActionCommand()));frame.add(bkk.get(bkk.size()-1));frame.setVisible(true);frame.repaint();im++;}
public static void message(String dan){
for(JButton bu:bkk){ System.out.println(bu.getText()); } }
@Overridepublic void actionPerformed(ActionEvent ee) { System.out.println(ee.getSource());
}}
________________________________________________________
If you want to pay for my services for advance Java codes, click here:
https://www.fiverr.com/towcal/java-program-tuning-and-customizing?context&context_referrer=search_gigs&context_type=new&pckg_id=1&pos=1&ref_ctx_id=b41c46ec-8d96-4002-90dc-dfd241a2eff9&funnel=10272682-a650-42e0-b0bd-2ee1c9d6ba59
To become a Facebook fan, click here:
https://www.facebook.com/ExcelQualityProgramCustomize34?ref=aymt_homepage_panel
To check out my videos, click here:
https://www.youtube.com/channel/UCdhZjVbM-pS3sL1iqtjyDgQ