How to make a JFrame?

Опубликовано: 05 Май 2026
на канале: Khadem tech
124
6

package gui;


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;




public class Gui{



public static void main(String[] args) {
JFrame f=new JFrame("Login");
JPanel p=new JPanel();
p.setSize(400,300);
p.setBackground(Color.yellow);

JButton btn=new JButton(new ImageIcon("C:\\Users\\khadim\\Pictures\\close.png"));
btn.setBounds(50,10,100,30);


JTextField txt=new JTextField();
txt.setBounds(50,50,100,30);

JTextField txt1=new JTextField();
txt1.setBounds(50,90,100,30);

JLabel lbl=new JLabel();
lbl.setBounds(160,50,100,30);
lbl.setForeground(Color.red);

btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int a=Integer.parseInt(txt.getText());
int b=Integer.parseInt(txt1.getText());
int result=a+b;
lbl.setText(result+"");
}
});
f.add(p);
p.add(lbl);
f.add(txt1);
f.add(txt);
f.add(btn);
f.setSize(400, 300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.setVisible(true);

}

}