0

How to add a button to my GUI Application in Java

Hello everyone, hope your day is going well so far! I wan to add a button to my GUI application but I'm not sure on how i would do that. Here the code that I already have: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AddressBook extends JPanel{ public static void main(String[] args) { //Creating the frame JFrame frame = new JFrame("AddressBook"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Creating a panel JPanel panel = new JPanel(); panel.setBackground(Color.white); panel.setPreferredSize(new Dimension(250,250)); //Creating labels and text fields JLabel label1 = new JLabel("Username"); JLabel label2 = new JLabel("Password"); JTextField text1 = new JTextField(30); JTextField text2 = new JTextField(30); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); //Adding the panel to the frame frame.getContentPane().add(panel); //Crucial frame.pack(); frame.setVisible(true); //Creating a submit button } }

8th Apr 2019, 3:10 PM
Leo Hunter
Leo Hunter - avatar
3 ответов
+ 2
JButton is the swing component used for creating buttons. Create it like, JButton btn= new JButton("Click me" ); btn.addActionListener(this); And then you'll have a method called actionPerformed which will be called by default by clicking any button(You need not to create one for each button). public void actionPerformed(ActionEvent e){ // your logic goes here } With Java 9+ Syntaxes are more easier. You can do the same by var btn= new JButton("Click me") btn.addActionListener((e)->{ // your logic goes here }); If you'd have worked with JS, you'd realize that syntaxes are more or less similar to ES6.
8th Apr 2019, 3:53 PM
Шащи Ранжан
Шащи Ранжан - avatar
+ 2
To add a button you have to create a JButton and add it to the JPanel. JButton b = new JButton (" Click me "); panel.add(b); But the button don't responses. You have to add the following to define what to do: b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // What to do. }}); I hope help you. Sorry if *bad English* Regards. Nahuel
8th Apr 2019, 4:06 PM
Nahuel Ourthe
Nahuel Ourthe - avatar
+ 2
You are creating an instance of JPanel class: JPanel panel = new JPanel(); so there is no need to inherit the JPanel class ( extends JPanel ). If your class inherit JPanel class then you cant use it in your main, you either need to create an method or use constructor. And to add the panel to your frame you can just do this: frame.add(panel); You can either use this in your main: yourButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ // This get the text you entered in your textfield String text = text1.getText(); // When you click the button the text will prints out at the aLabel position aLabel.setText(text); }}); Or you can also use the actionPerformed method outside your main but then you need to implement the ActionListener interface and add action to your button in your main: yourButton.addActionListener(this);
8th Apr 2019, 10:51 PM
JavaBobbo
JavaBobbo - avatar