Button Array in java swing with Action Perform
import java.awt.GridLayout;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
class MultiButton extends JFrame implements ActionListener
{
int i;
int n=20;
JButton[] b1 = new JButton[12];
JTextField[] t1=new JTextField[12];
String s1[]={"A","B","C","D","E","F","G","H","I","J","K","M","N","O","P"};
JPopupMenu jp;
MultiButton()
{
for(i=0;i<12;i++)
{
b1[i]=new JButton(s1[i]);
add(b1[i]);
b1[i].addActionListener(this);
b1[i].setBounds(10,n,70,30);
n+=50;
}
setLayout(null);
//setLayout(new GridLayout(6, 5));
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1[0])
// JOptionPane.showMessageDialog(this,s1[0]);
System.out.println(s1[0]);
if(e.getSource()==b1[1])
//JOptionPane.showMessageDialog(this,s1[1]);
System.out.println(s1[1]);
if(e.getSource()==b1[2])
// JOptionPane.showMessageDialog(this,s1[2]);
System.out.println(s1[3]);
if(e.getSource()==b1[3])
//JOptionPane.showMessageDialog(this,s1[3]);
//JOptionPane.showConfirmDialog(this,"hello");
// System.out.println(s1[4]);
b1[0].setText(s1[2]);
}
public static void main(String[] args)
{
MultiButton mb=new MultiButton();
mb.setVisible(true);
mb.setTitle("buttons");
mb.setSize(550,660);
}
}
0 Comments