how to build a gui in java

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Foo{

  public static void main(String[] args) {

    JFrame f = new JFrame("A JFrame");
    f.setSize(250, 250);
    f.setLocation(300,200);
    final JTextArea textArea = new JTextArea(10, 40);
    f.getContentPane().add(BorderLayout.CENTER, textArea);
    final JButton button = new JButton("Click Me");
    f.getContentPane().add(BorderLayout.SOUTH, button);
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            textArea.append("Button was clicked\n");

        }
    });

    f.setVisible(true);

  }

}

3.9
10

                                    import java.awt.BorderLayout;//Just some imports that are needed
import java.awt.GridLayout;//Just some imports that are needed
import java.awt.event.ActionEvent;//Just some imports that are needed
import java.awt.event.ActionListener;//Just some imports that are needed

import javax.swing.BorderFactory;//Just some imports that are needed
import javax.swing.JButton;//Just some imports that are needed
import javax.swing.JFrame;//Just some imports that are needed
import javax.swing.JLabel;//Just some imports that are needed
import javax.swing.JPanel;//Just some imports that are needed

public class GUI implements ActionListener{//If there is a red line after writing GUI its cause you have no code for the bit that's after implements so just hover over gui and just press "add unimplemented methods"
	
	int count = 0;//a VAR for the function of the button for the number of clicks
	private JLabel label;
	private JPanel panel;
	private JFrame frame;
	
	
	public GUI(){//This implements for the constructor that is being create in the void main, Here you can make frames for the gooooooey and define its characteristics :P
		
		//JFrame - we have removed this from the line below to make a var that we can use in multiple methods {a}
		frame = new JFrame();//This sets the frame for the goooooooooey so the user can customise it to their needs
		
		JButton button = new JButton("Howdy");//This is used to make a button
		button.addActionListener(this);//This refers to the class that we are using like GUI implements Action listener
		
		//JLabel {a}
		label = new JLabel("Number of clicks: 0"); //This is used to add a label to our program
		
		//JPanel {a}
		panel = new JPanel();//This sets the panel for the gooey for the user so that they can add elements, delete import etc etc
		panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));//This makes a border object for the goooey and the number in the field describes the distance it should be from the borders of the gui
		panel.setLayout(new GridLayout(0,1));// This is the default constructor for the borders/grids
		panel.add(button);//This adds the button ( basically packing like btn.pack())
		panel.add(label);//This adds the button (packs)
		
		frame.add(panel, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("Our GOOEY");//sets the title
		frame.pack();//like py
		frame.setVisible(true);//kinda literal this just lets the dev choose if the user can see this or not
	}
	
	public static void main(String[] args) {
		new GUI();//Creates a constructor but this needs to be implemented to actually show us the gui window

	}

	@Override
	public void actionPerformed(ActionEvent e) {//After implementing the unimplemented methods this autogenerated method is initiated to help us write our code in for the action we need to perform
		count++;//Increases the count by 1
		label.setText("Number of clicks: " + count);//This just changes the text of the label like lable.config("Hey") and then adds the number of clicks to the string
		
	}

}

3.9 (10 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
basic steps for GUI java build gui app with java simple GUI code in java best way to make java gui building guis in java what to use for make gui through java how to code gui at java Java creating a GUI best way to create a gui in java how to create a java gui gui using swing in java how to develop gui app in java java program with gui Java GUI code example how to make a professional gui with java making gui app java gui app in java gui with java creating gui in swing java making java gui from code gui program in java make gui in java Create a java with gui build gui with java using java to create guis java basic gui how to create gui class in java how to design a gui java how to call an gui in java java how to create gui how to setup gui in java learn gui in java java simple gui program how to develop gui with java how to make guis in java how to create gui based application in java java how make gui java how to create a simple gui make java gui gui application in java how to use a gui with java how to make gui in swing java gui application java make gui for application java We can use a class to create a GUI in Java Java GUI Tutorial using java make your own gui framework java making own gui library java how to write your own gui library java gui programming tutorial java make your own gui software make gui with java simple java gui basic java gui build gui in java making java guis the right way java create a gui tool how to program a gui in java how do you create a gui in java how to make a gui in java 8 java create a gui examples java create a gui example java create a gui how to build gui in java gui based java program make gui java how to build java gui how to create java guis creating java programs with gui how to build a gui java how to make gui application in java Create JAva Gui Using Designer how to make a gui app with java make java gui apps software gui in java create a gui application in java creating gui in java simple gui program in java java make object gui java design gui java tutorial of GUI java gui examples how to create gui for java application java swing make gui creating a gui app java gui examples java What kind of Java Application can I make with GUI? how to create a simple gui in java gui java example gui using java what is the best way of creating GUI's in java how to code gui in java building GUIs java create a software gui in java make a gui in java how to make a gui os in java how to make your own gui library in java basic gui java gui tutorial java how to make a java application with gui how to make a gui for java how to program gui java best way to make a java gui gui java tutorial GUI in Java examples java gui how To how ro make a gui in java how to setup gui class in java simple gui with java how to make a java gui creating a java gui java gui example gui in java gui app with java gui in java java Gui design java create simple gui building a gui in java best way to make GUI in java how to make gui in java tutorial how to create a gui in java swing how far java gui can do java create guid Java Gui Builder program how to amke a java gui How to creat java gui eclipse how to create java GUI java gui ideas how to create a good gui in java how to make gui things with java GUI design java how to use gui in java java gui example code make simple java GUI java create graphical interface how to create a gui java java create a simple ui java windos apliction gui how to build a gui in java gui project in java how to make guiin java how to make a clickable user interface in java simple gui java create a gui in java make gui app in java how to make gui with java Simple gui in java making a simple gui in java create java guis make a gui online for java simple gui example in java creating a gui in java how to make a gui java java gui tutorial in eciplese how to create gui in eclipse using java how to make gui in java java how to make gui create gui with java what are the different ways to create a java gui application how to create a gui for java create GUI in eclipse jframe create GUI interface in eclipse create gui java java create gui java making gui create a gui java making a gui in java java swing in eclipse tutorial create gui in eclipse java how to create a gui in java run gui java from eclipse how to make gui java java gui tutorial explained java create a user interface how to amke gui java java gui tutorial how to make a gui in java how to make a gui with java create gui application in java making a ui in java java make gui new way to do GUI on java how to create gui in java how to create gui for java program create gui in java making a java gui creating gui java java make a gui java how to make a gui
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source