Я новичок в Java и работал над простым приложением Java, которое создает простое приложение с графическим интерфейсом пользователя с полем ввода текста, кнопкой и меткой вывода. При нажатии кнопки отображается приветственное сообщение на основе ввода пользователя.
Код:
package com.example.inputoutputgui;
import java.awt.FlowLayout; //Imports classes for creating graphical user interfaces (GUIs).
import java.awt.event.ActionEvent; //Provides classes for creating and managing graphical user interface components.
import java.awt.event.ActionListener; //Provides interfaces for handling events, such as button clicks.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
//Class Declaration
//----------------------------------------
//InputOutputGUI: The name of the class.
//extends JFrame: Inherits from the JFrame class, which is the base class for creating window-based applications
//implements ActionListener: Implements the ActionListener interface to handle button click events.
public class InputOutputGUI extends JFrame implements ActionListener {
private JTextField inputTextField; //Class Variable: A text field for user input.
private JButton generateButton; //Class Variable: A button to trigger the generation of output.
private JLabel outputLabel; //Class Variable: Provides interfaces for handling events, such as button clicks.
//Constructor
public InputOutputGUI() {
super("Generate hello message including your name"); //Sets the title of the window.
setLayout(new FlowLayout()); //Sets the layout manager for the window to FlowLayout, which arranges components horizontally.
//Component Creation and Initialization
//Creates instances of JTextField, JButton, and JLabel.
inputTextField = new JTextField(20);
generateButton = new JButton("Generate"); ////Sets the text of the button to "Generate".
outputLabel = new JLabel();
//Adds an action listener to the button, which will call the actionPerformed method when the button is clicked.
generateButton.addActionListener(this);
//Adding Components to the Frame
//Adds the text field, button, and label to the frame.
add(inputTextField);
add(generateButton);
add(outputLabel);
//Frame Configuration
setSize(300, 150); //Sets the size of the frame.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Specifies that the application should exit when the frame is closed.
setVisible(true); //Makes the frame visible.
}
//Action Listener Method
//This method is called when the button is clicked.
//Gets the text from the input text field.
//Sets the text of the output label to a greeting message including the input text.
public void actionPerformed(ActionEvent e) {
String input = inputTextField.getText();
outputLabel.setText("Hello, " + input + "!");
}
//Main Method
//Creates an instance of the InputOutputGUI class, which starts the application.
public static void main(String[] args) {
new InputOutputGUI();
}
}
Я новичок в Java и работал над простым приложением Java, которое создает простое приложение с графическим интерфейсом пользователя с полем ввода текста, кнопкой и меткой вывода. При нажатии кнопки отображается приветственное сообщение на основе ввода пользователя. Код: [code]package com.example.inputoutputgui;
import java.awt.FlowLayout; //Imports classes for creating graphical user interfaces (GUIs). import java.awt.event.ActionEvent; //Provides classes for creating and managing graphical user interface components. import java.awt.event.ActionListener; //Provides interfaces for handling events, such as button clicks.
//Class Declaration //---------------------------------------- //InputOutputGUI: The name of the class. //extends JFrame: Inherits from the JFrame class, which is the base class for creating window-based applications //implements ActionListener: Implements the ActionListener interface to handle button click events. public class InputOutputGUI extends JFrame implements ActionListener { private JTextField inputTextField; //Class Variable: A text field for user input. private JButton generateButton; //Class Variable: A button to trigger the generation of output. private JLabel outputLabel; //Class Variable: Provides interfaces for handling events, such as button clicks.
//Constructor public InputOutputGUI() { super("Generate hello message including your name"); //Sets the title of the window. setLayout(new FlowLayout()); //Sets the layout manager for the window to FlowLayout, which arranges components horizontally.
//Component Creation and Initialization //Creates instances of JTextField, JButton, and JLabel. inputTextField = new JTextField(20); generateButton = new JButton("Generate"); ////Sets the text of the button to "Generate". outputLabel = new JLabel(); //Adds an action listener to the button, which will call the actionPerformed method when the button is clicked. generateButton.addActionListener(this); //Adding Components to the Frame //Adds the text field, button, and label to the frame. add(inputTextField); add(generateButton); add(outputLabel);
//Frame Configuration setSize(300, 150); //Sets the size of the frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Specifies that the application should exit when the frame is closed. setVisible(true); //Makes the frame visible. }
//Action Listener Method //This method is called when the button is clicked. //Gets the text from the input text field. //Sets the text of the output label to a greeting message including the input text. public void actionPerformed(ActionEvent e) { String input = inputTextField.getText(); outputLabel.setText("Hello, " + input + "!"); }
//Main Method //Creates an instance of the InputOutputGUI class, which starts the application. public static void main(String[] args) { new InputOutputGUI(); } } [/code] Структура папок: [code]Hello-App/ src/ main/ java/ com/ example/ InputOutputGUI/ InputOutputGUI.java [/code] когда мне нужно запустить код запуска Java я не нашел его [img]https://i.sstatic.net/oEQwweA4.png[/img]
JDK установлен Пакет расширений Java установлен [img]https://i.sstatic.net/Ox7FqE18.png[/img]