Answers for "how to make a gui app with java"

3

java how to make a gui

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);

  }

}
Posted by: Guest on March-07-2020
2

how to build a gui in java

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
		
	}

}
Posted by: Guest on April-10-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language