I want to add actionlistener to all four buttons so that when ever i click the b
ID: 3655614 • Letter: I
Question
I want to add actionlistener to all four buttons so that when ever i click the button, the name in the button will display in the middle. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class FavoriteTeams extends JFrame { /** * Serial version UID. */ private static final long serialVersionUID = -4833137698862539762L; // Create the buttons. private JButton northButton = new JButton("Steaua"); private JButton southButton = new JButton("Dinamo"); private JButton westButton = new JButton("Poli Timisoara"); private JButton eastButton = new JButton("UTA"); // Create the label. JLabel centerText = new JLabel(); public FavoriteTeams(){ setLayout(new BorderLayout()); // Add the buttons and the label to the frame. add(northButton, BorderLayout.NORTH); add(southButton, BorderLayout.SOUTH); add(westButton, BorderLayout.WEST); add(eastButton, BorderLayout.EAST); add(centerText, BorderLayout.CENTER); } public void showFrame(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setVisible(true); } public static void main(String args[]) { FavoriteTeams favoriteTeamsFrame = new FavoriteTeams(); favoriteTeamsFrame.showFrame(); } }Explanation / Answer
//replace N , E, W, S team name with yours //file named FavoriteTeams.java //hope it help you :P import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class FavoriteTeams { /** * @param args */ public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("FavoriteTeams"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. addComponentsToPane(frame.getContentPane()); //Use the content pane's default BorderLayout. No need for //setLayout(new BorderLayout()); //Display the window. frame.pack(); frame.setVisible(true); } public static void addComponentsToPane(Container pane) { JButton buttonN = new JButton("North"); pane.add(buttonN, BorderLayout.NORTH); JButton buttonS = new JButton("South"); pane.add(buttonS, BorderLayout.SOUTH); JButton buttonE = new JButton("East"); pane.add(buttonE, BorderLayout.EAST); JButton buttonW = new JButton("West"); pane.add(buttonW, BorderLayout.WEST); JLabel lb = new JLabel(); pane.add(lb, BorderLayout.CENTER); ClickListener lis = new ClickListener(lb); buttonN.addActionListener(lis); buttonS.addActionListener(lis); buttonE.addActionListener(lis); buttonW.addActionListener(lis); } } class ClickListener implements ActionListener{ JLabel lb; public ClickListener(JLabel lb){ this.lb = lb; } @Override public void actionPerformed(ActionEvent e) { JButton b = (JButton)e.getSource(); lb.setText(b.getText()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.