0% found this document useful (0 votes)
12 views

Code

The document contains code for several Java programs that create graphical user interfaces (GUIs) using Swing. The code samples show how to create frames, panels, labels, text fields, and buttons to build GUI applications.

Uploaded by

ngquyhung2610
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Code

The document contains code for several Java programs that create graphical user interfaces (GUIs) using Swing. The code samples show how to create frames, panels, labels, text fields, and buttons to build GUI applications.

Uploaded by

ngquyhung2610
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Buổi 1:

public class BaiTap3 {


public static void main(String[] args) {
JFrame frm = new JFrame("Tinh Tong, Tich, Hieu");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLayout(new BoxLayout(frm.getContentPane(), BoxLayout.Y_AXIS));
frm.setSize(300, 200);
frm.setLocationRelativeTo(null);
JPanel pnTieuDe = new JPanel();
pnTieuDe.setLayout(new FlowLayout());
JLabel lbTieuDe = new JLabel("Tinh Luong Nhan Vien: ");
pnTieuDe.add(lbTieuDe);
JPanel pnNhapLieu = new JPanel();
pnNhapLieu.setLayout(new GridLayout(3,2));
JLabel lbLuongNgay = new JLabel("Luong Mot Ngay: ");
JTextField txtLuongNgay = new JTextField(15);
pnNhapLieu.add(lbLuongNgay); pnNhapLieu.add(txtLuongNgay);
JLabel lbNgayCong = new JLabel("Ngay Cong: ");
JTextField txtNgayCong = new JTextField(15);
pnNhapLieu.add(lbNgayCong); pnNhapLieu.add(txtNgayCong);
JLabel lbLuongThang = new JLabel("Luong Thang: ");
JTextField txtLuongThang = new JTextField(15);
pnNhapLieu.add(lbLuongThang); pnNhapLieu.add(txtLuongThang);
JPanel pnNutLenh = new JPanel();
pnNutLenh.setLayout(new FlowLayout());
JButton BtnTinh = new JButton("Tinh");
BtnTinh.setIcon(new ImageIcon ("Hinh/tinh.png"));
pnNutLenh.add(BtnTinh);
JButton BtnXoa = new JButton("Xoa");
BtnXoa.setIcon(new ImageIcon ("Hinh/xoa.png"));
pnNutLenh.add(BtnXoa);
JButton BtnThoat = new JButton("Thoat");
BtnThoat.setIcon(new ImageIcon ("Hinh/thoat.png"));
pnNutLenh.add(BtnThoat);
frm.add(pnTieuDe);
frm.add(pnNhapLieu);
frm.add(pnNutLenh);
frm.setVisible(true)}}

public class BaiTap4 {


public static void main(String[] args) {
JFrame frm = new JFrame("Giai Phuong Trinh");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLayout(new BoxLayout(frm.getContentPane(), BoxLayout.Y_AXIS));
frm.setSize(300, 200);
frm.setLocationRelativeTo(null);

JPanel pnTieuDe = new JPanel();


pnTieuDe.setLayout(new FlowLayout());

JLabel lbTieuDe = new JLabel("Giai Phuong Trinh Bac 1: ");


pnTieuDe.add(lbTieuDe);

JPanel pnNhapLieu = new JPanel();


pnNhapLieu.setLayout(new GridLayout(3,2));

JLabel lbHeSoA = new JLabel("He So a: ");


JTextField txtHeSoA = new JTextField(15);
pnNhapLieu.add(lbHeSoA); pnNhapLieu.add(txtHeSoA);

JLabel lbHeSoB = new JLabel("He So b: ");


JTextField txtHeSoB = new JTextField(15);
pnNhapLieu.add(lbHeSoB); pnNhapLieu.add(txtHeSoB);

// Panel Button
JPanel pnNutLenh = new JPanel();
pnNutLenh.setLayout(new FlowLayout());

JButton BtnTinh = new JButton("Giai");


pnNutLenh.add(BtnTinh);

JButton BtnXoa = new JButton("Thoat");


pnNutLenh.add(BtnXoa);
JButton BtnThoat = new JButton("Moi");
pnNutLenh.add(BtnThoat);

frm.add(pnTieuDe);
frm.add(pnNhapLieu);
frm.add(pnNutLenh);
frm.setVisible(true);
}}
Buổi 2:

public class bt1 {


private JFrame frmHinThngBo;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
bt1 window = new bt1();
window.frmHinThngBo.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public bt1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmHinThngBo = new JFrame();
frmHinThngBo.setTitle("HI\u1EC6N TH\u00D4NG B\u00C1O");
frmHinThngBo.setBounds(100, 100, 450, 300);
frmHinThngBo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmHinThngBo.getContentPane().setLayout(new
BoxLayout(frmHinThngBo.getContentPane(), BoxLayout.Y_AXIS));

JPanel panel = new JPanel();


panel.setForeground(Color.CYAN);
frmHinThngBo.getContentPane().add(panel);

JLabel lblNewLabel = new JLabel("CH\u01AF\u01A0NG TR\u00CCNH HI\u1EC6N


TH\u00D4NG B\u00C1O");
lblNewLabel.setForeground(Color.ORANGE);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
panel.add(lblNewLabel);

JPanel panel_1 = new JPanel();


frmHinThngBo.getContentPane().add(panel_1);

JButton btnNewButton = new JButton("HI\u1EC2N TH\u1ECA");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Chào các bạn!!!");
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 12));
panel_1.add(btnNewButton);

JButton btnNewButton_1 = new JButton("\u0110\u00D3NG C\u1EECA S\u1ED4");


btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 11));
panel_1.add(btnNewButton_1);
}
}
public class bt2 {
private JFrame frmTmSLn;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
bt2 window = new bt2();
window.frmTmSLn.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public bt2() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmTmSLn = new JFrame();
frmTmSLn.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 20));
frmTmSLn.setTitle("T\u00CCM S\u1ED0 L\u1EDAN NH\u1EA4T TRONG BA S\
u1ED0");
frmTmSLn.setBounds(100, 100, 450, 290);
frmTmSLn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmTmSLn.getContentPane().setLayout(new
BoxLayout(frmTmSLn.getContentPane(), BoxLayout.Y_AXIS));

JPanel panel = new JPanel();


frmTmSLn.getContentPane().add(panel);

JLabel lblNewLabel = new JLabel("T\u00CCM S\u1ED0 L\u1EDAN NH\u1EA4T


TRONG BA S\u1ED0");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
panel.add(lblNewLabel);

JPanel panel_1 = new JPanel();


panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 2));
frmTmSLn.getContentPane().add(panel_1);
panel_1.setLayout(new GridLayout(3, 2, 20, 10));
JLabel lblNewLabel_1 = new JLabel("S\u1ED1 th\u1EE9 t\u1EF1 1:");
panel_1.add(lblNewLabel_1);

textField = new JTextField();


panel_1.add(textField);
textField.setColumns(10);

JLabel lblNewLabel_2 = new JLabel("S\u1ED1 th\u1EE9 t\u1EF1 2:");


panel_1.add(lblNewLabel_2);

textField_1 = new JTextField();


panel_1.add(textField_1);
textField_1.setColumns(10);

JLabel lblNewLabel_3 = new JLabel("S\u1ED1 th\u1EE9 t\u1EF1 3:");


panel_1.add(lblNewLabel_3);

textField_2 = new JTextField();


panel_1.add(textField_2);
textField_2.setColumns(10);

JPanel panel_2 = new JPanel();


frmTmSLn.getContentPane().add(panel_2);

JButton btnNewButton = new JButton("Th\u1EF1c hi\u1EC7n");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double lblNewLabel_1 = Double.parseDouble(textField.getText());
double lblNewLabel_2 = Double.parseDouble(textField_1.getText());
double lblNewLabel_3 = Double.parseDouble(textField_2.getText());

double max = lblNewLabel_1;


if (max < lblNewLabel_2 ) max = lblNewLabel_2;
if (max < lblNewLabel_3 ) max = lblNewLabel_3;

textField_3.setText(String.valueOf(max));
}
});
btnNewButton.setIcon(new ImageIcon("G:\\VoHoaiBao\\hinh\\tk.jpg"));
panel_2.add(btnNewButton);

JButton btnNewButton_1 = new JButton("M\u1EDBi");


btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(null);
textField_1.setText(null);
textField_2.setText(null);
textField_3.setText(null);

textField.requestFocus();
}
});
btnNewButton_1.setIcon(new ImageIcon("G:\\VoHoaiBao\\hinh\\load.jpg"));
panel_2.add(btnNewButton_1);

JButton btnNewButton_2 = new JButton("\u0110\u00F3ng");


btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnNewButton_2.setIcon(new ImageIcon("G:\\VoHoaiBao\\hinh\\close.jpg"));
panel_2.add(btnNewButton_2);

JPanel panel_3 = new JPanel();


frmTmSLn.getContentPane().add(panel_3);
panel_3.setLayout(new GridLayout(1, 2, 5, 5));

JLabel lblNewLabel_4 = new JLabel("S\u1ED1 l\u1EDBn nh\u1EA5t:");


panel_3.add(lblNewLabel_4);

textField_3 = new JTextField();


textField_3.setBackground(new Color(255, 228, 181));
textField_3.setForeground(SystemColor.desktop);
panel_3.add(textField_3);
textField_3.setColumns(10);
}

}
public class bt3 {
private JFrame frmGiiPhngTrnh;
private JTextField texta;
private JTextField textb;
private JTextField textKQ;
/**
* Launch the application.*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
bt3 window = new bt3();
window.frmGiiPhngTrnh.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public bt3() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmGiiPhngTrnh = new JFrame();
frmGiiPhngTrnh.setTitle("Gi\u1EA3i Ph\u01B0\u01A1ng Tr\u00ECnh");
frmGiiPhngTrnh.setBounds(100, 100, 450, 358);
frmGiiPhngTrnh.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmGiiPhngTrnh.getContentPane().setLayout(new
BoxLayout(frmGiiPhngTrnh.getContentPane(), BoxLayout.Y_AXIS));

JPanel tieuDe = new JPanel();


frmGiiPhngTrnh.getContentPane().add(tieuDe);
tieuDe.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

JLabel lblNewLabel = new JLabel("GI\u1EA2I PH\u01AF\u01A0NG TR\u00CCNH


B\u1EACC 1");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
tieuDe.add(lblNewLabel);

JPanel nhapLieu = new JPanel();


frmGiiPhngTrnh.getContentPane().add(nhapLieu);
nhapLieu.setLayout(new GridLayout(2, 2, 0, 15));
JLabel hsa = new JLabel("H\u1EC7 s\u1ED1 a");
nhapLieu.add(hsa);

texta = new JTextField();


nhapLieu.add(texta);
texta.setColumns(10);

JLabel hsb = new JLabel("H\u1EC7 s\u1ED1 b");


nhapLieu.add(hsb);

textb = new JTextField();


nhapLieu.add(textb);
textb.setColumns(10);

JPanel tinhToan = new JPanel();


frmGiiPhngTrnh.getContentPane().add(tinhToan);
tinhToan.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

JButton btnNewButton = new JButton("Gi\u1EA3i");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double hsa = Double.parseDouble(texta.getText());
double hsb = Double.parseDouble(textb.getText());
if (hsa == 0)
{
textKQ.setText("Phương trình vô nghiệm");
}
else
{
textKQ.setText(String.valueOf(hsa/hsb));
}
}
});
btnNewButton.setBackground(new Color(51, 255, 255));
tinhToan.add(btnNewButton);

JButton btnNewButton_1 = new JButton("Tho\u00E1t");


btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnNewButton_1.setBackground(new Color(51, 255, 51));
tinhToan.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("M\u1EDBi");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
texta.setText(null);
textb.setText(null);
textKQ.setText(null);
texta.requestFocus();
}
});
btnNewButton_2.setBackground(new Color(102, 255, 51));
tinhToan.add(btnNewButton_2);

JPanel tongKQ = new JPanel();


frmGiiPhngTrnh.getContentPane().add(tongKQ);
tongKQ.setLayout(new GridLayout(0, 2, 0, 15));

JLabel kQ = new JLabel("K\u1EBFt Qu\u1EA3:");


tongKQ.add(kQ);

textKQ = new JTextField();


textKQ.setBackground(new Color(255, 160, 122));
tongKQ.add(textKQ);
textKQ.setColumns(10);
}
}

public class bt4 extends JFrame implements ActionListener {


private JTextField txtA, txtB, txtC;
private JButton btnGiai, btnXoaRong, btnThoat;
private JLabel lblKetQua;
public bt4() {
setTitle("Giải phương trình bậc hai");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Tạo các thành phần giao diện
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 5)); // Use FlowLayout
for vertical alignment
add(panel, BorderLayout.CENTER);

// Labels and TextFields stacked vertically


JLabel lblA = new JLabel("Nhập a:");
panel.add(lblA);
txtA = new JTextField(10); // Set preferred column width
panel.add(txtA);

JLabel lblB = new JLabel("Nhập b:");


panel.add(lblB);
txtB = new JTextField(10);
panel.add(txtB);

JLabel lblC = new JLabel("Nhập c:");


panel.add(lblC);
txtC = new JTextField(10);
panel.add(txtC);

btnGiai = new JButton("Giải");


btnGiai.addActionListener(this);
panel.add(btnGiai);

btnXoaRong = new JButton("Xóa rỗng");


btnXoaRong.addActionListener(this);
panel.add(btnXoaRong);

btnThoat = new JButton("Thoát");


btnThoat.addActionListener(this);
panel.add(btnThoat);

lblKetQua = new JLabel();


lblKetQua.setFont(new Font("Arial", Font.BOLD, 16));
panel.add(lblKetQua);

// Hiển thị form


setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnGiai) {
// Lấy giá trị từ các trường nhập dữ liệu
double a = Double.parseDouble(txtA.getText());
double b = Double.parseDouble(txtB.getText());
double c = Double.parseDouble(txtC.getText());

// Giải phương trình bậc hai


double delta = b * b - 4 * a * c;
String ketQua = "";

if (delta > 0) {
// Có hai nghiệm phân biệt
double x1 = (-b + Math.sqrt(delta)) / (2 * a);
double x2 = (-b - Math.sqrt(delta)) / (2 * a);
ketQua = "x1 = " + x1 + ", x2 = " + x2;
} else if (delta == 0) {
// Có một nghiệm kép
double x = -b / (2 * a);
ketQua = "x = " + x;
} else {
// Vô nghiệm
ketQua = "Vô nghiệm";
}
// Hiển thị kết quả
lblKetQua.setText(ketQua);
} else if (e.getSource() == btnXoaRong) {
// Xóa dữ liệu trong các trường nhập dữ liệu
txtA.setText("");
txtB.setText("");
txtC.setText("");
lblKetQua.setText("");
} else if (e.getSource() == btnThoat) {
// Thoát khỏi chương trình
System.exit(0);
}
}
public static void main(String[] args) {
new bt4();
}
}
Buổi 3:
public class bt1 {
private JFrame frmngNhp;
private JTextField txtUser;
private JPasswordField txtPass;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
bt1 window = new bt1();
window.frmngNhp.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public bt1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmngNhp = new JFrame();
frmngNhp.setTitle("\u0110\u0103ng Nh\u1EADp");
frmngNhp.setBounds(100, 100, 450, 300);
frmngNhp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmngNhp.getContentPane().setLayout(null);

JPanel pnMain = new JPanel();


pnMain.setBounds(0, 0, 10, 10);
frmngNhp.getContentPane().add(pnMain);
pnMain.setLayout(null);

JLabel lbUser = new JLabel("User Name");


lbUser.setFont(new Font("Tahoma", Font.BOLD, 14));
lbUser.setBounds(32, 26, 84, 14);
frmngNhp.getContentPane().add(lbUser);
txtUser = new JTextField();
txtUser.setBounds(126, 23, 279, 32);
frmngNhp.getContentPane().add(txtUser);
txtUser.setColumns(10);

JLabel lbPass = new JLabel("PassWord");


lbPass.setFont(new Font("Tahoma", Font.BOLD, 14));
lbPass.setBounds(32, 72, 84, 14);
frmngNhp.getContentPane().add(lbPass);

txtPass = new JPasswordField();


txtPass.setBounds(126, 71, 279, 32);
frmngNhp.getContentPane().add(txtPass);

JPanel pnBtn = new JPanel();


pnBtn.setBounds(0, 0, 10, 10);
frmngNhp.getContentPane().add(pnBtn);

JButton btnLogin = new JButton("Login");


btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (checkData(txtUser.getText() , txtPass.getPassword() ))
{
JOptionPane.showMessageDialog(null, "Login success !",
"Login", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Login failed, try
again", "Login", JOptionPane.ERROR_MESSAGE);
}
}
private boolean checkData(String inputUserName, char[] inputPassword)
{
String username = "VoHoaiBao";
char[] password = {'1','2','3','4'};
return (Arrays.equals (inputPassword,password) &&
username.equals(inputUserName));
}
});
btnLogin.setBackground(new Color(255, 255, 255));
btnLogin.setBounds(164, 132, 72, 23);
frmngNhp.getContentPane().add(btnLogin);

JButton btnHelp = new JButton("Help");


btnHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(null, "Demo Pass", "Help",


JOptionPane.INFORMATION_MESSAGE);
}
});
btnHelp.setBounds(264, 132, 77, 23);
frmngNhp.getContentPane().add(btnHelp);
}
}
public class bt2 {
private JFrame frmThngTinNgi;
private JTextField txtHt;
private final ButtonGroup buttonGroup = new ButtonGroup();
private final ButtonGroup buttonGroup_1 = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
bt2 window = new bt2();
window.frmThngTinNgi.pack();
window.frmThngTinNgi.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public bt2() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmThngTinNgi = new JFrame();
frmThngTinNgi.setTitle("Thông tin người dùng");
frmThngTinNgi.setBounds(100, 100, 450, 423);
frmThngTinNgi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel pnMain = new JPanel();


pnMain.setBorder(new EmptyBorder(20, 20, 20, 20));
frmThngTinNgi.getContentPane().add(pnMain, BorderLayout.CENTER);
pnMain.setLayout(null);

JPanel pnThongTin = new JPanel();


pnThongTin.setBounds(20, 20, 394, 168);
pnThongTin.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 255), 2),
"Th\u00F4ng Tin", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(255, 0, 0)));
pnMain.add(pnThongTin);
pnThongTin.setLayout(null);

JLabel lbHt = new JLabel("Họ Và Tên");


lbHt.setBounds(84, 23, 72, 17);
lbHt.setFont(new Font("Tahoma", Font.BOLD, 14));
pnThongTin.add(lbHt);

txtHt = new JTextField();


txtHt.setBounds(161, 22, 86, 20);
pnThongTin.add(txtHt);
txtHt.setColumns(10);

JLabel lnDc = new JLabel("Địa Chỉ");


lnDc.setBounds(84, 64, 48, 17);
lnDc.setFont(new Font("Tahoma", Font.BOLD, 14));
pnThongTin.add(lnDc);

JTextArea txtDc = new JTextArea();


txtDc.setBounds(161, 62, 86, 20);
pnThongTin.add(txtDc);

JPanel pnST = new JPanel();


pnST.setBorder(new TitledBorder(new LineBorder(new Color(255, 0, 0), 2, true), "S\
u1EDF Th\u00EDch", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0)));
pnST.setBounds(20, 216, 197, 92);
pnMain.add(pnST);
pnST.setLayout(null);

JCheckBox ck1 = new JCheckBox("Đi Bơi");


ck1.setBounds(19, 21, 65, 25);
ck1.setFont(new Font("Tahoma", Font.BOLD, 14));
pnST.add(ck1);

JCheckBox ck2 = new JCheckBox("Xem Phim");


ck2.setBounds(19, 49, 95, 25);
ck2.setFont(new Font("Tahoma", Font.BOLD, 14));
pnST.add(ck2);

JPanel pnGT = new JPanel();


pnGT.setBorder(new TitledBorder(new LineBorder(new Color(255, 0, 0), 2, true),
"Gi\u1EDBi T\u00EDnh", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0)));
pnGT.setBounds(227, 216, 187, 92);
pnMain.add(pnGT);
pnGT.setLayout(null);

JRadioButton ck3 = new JRadioButton("Nam");


buttonGroup_1.add(ck3);
ck3.setBounds(18, 21, 57, 25);
ck3.setFont(new Font("Tahoma", Font.BOLD, 14));
pnGT.add(ck3);

JRadioButton ck4 = new JRadioButton("Nữ");


buttonGroup_1.add(ck4);
ck4.setBounds(18, 49, 47, 25);
ck4.setFont(new Font("Tahoma", Font.BOLD, 14));
pnGT.add(ck4);

JPanel pnNut = new JPanel();


pnNut.setBounds(184, 319, 214, 27);
pnMain.add(pnNut);
pnNut.setLayout(null);

JButton btnOK = new JButton("OK");


btnOK.setIcon(new ImageIcon("G:\\VoHoaiBao\\hinh\\ok.png"));
btnOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String hten = txtHt.getText();
String dc = txtDc.getText();
String st = ck1.isSelected() ? ck1.getText() : " ";
String st2 = ck2.isSelected() ? ck2.getText() : " ";
String gt = ck3.isSelected() ? "Nam" : "Nữ" ;
JOptionPane.showMessageDialog(null, "Họ Và Tên: "+ hten + "\nĐịa
Chỉ: " +dc + "\nSở Thich: "+ st + st2 + "\nGiới Tính: "+ gt);
}
});
btnOK.setBounds(10, 0, 89, 23);
pnNut.add(btnOK);

JButton BtnHelp = new JButton("Exit");


BtnHelp.setIcon(new ImageIcon("G:\\VoHoaiBao\\hinh\\thoat.jpg"));
BtnHelp.setBounds(109, 0, 99, 23);
pnNut.add(BtnHelp);
BtnHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}}

public class bt3 {


private JFrame frmTinThng;
private JTextField txtLuong;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
bt3 window = new bt3();
window.frmTinThng.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public bt3() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmTinThng = new JFrame();
frmTinThng.setTitle("Tiền Thưởng");
frmTinThng.setBounds(100, 100, 450, 300);
frmTinThng.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmTinThng.getContentPane().setLayout(null);

JPanel main = new JPanel();


main.setBorder(new EmptyBorder(20, 20, 20, 20));
main.setBounds(0, 0, 434, 262);
frmTinThng.getContentPane().add(main);
main.setLayout(null);

JLabel lblTnhTinThng = new JLabel("Tính tiền thưởng cuối năm");


lblTnhTinThng.setForeground(new Color(0, 255, 0));
lblTnhTinThng.setFont(new Font("Tahoma", Font.BOLD, 26));
lblTnhTinThng.setHorizontalAlignment(SwingConstants.CENTER);
lblTnhTinThng.setBounds(10, 2, 414, 32);
main.add(lblTnhTinThng);

JPanel pnThongTin = new JPanel();


pnThongTin.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2, true),
"Th\u00F4ng tin thu nh\u1EADp", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255,
0, 0)));
pnThongTin.setBounds(10, 45, 414, 109);
main.add(pnThongTin);
pnThongTin.setLayout(null);

JLabel lbLuong = new JLabel("Tiền lương: ");


lbLuong.setFont(new Font("Tahoma", Font.BOLD, 14));
lbLuong.setBounds(10, 21, 90, 22);
pnThongTin.add(lbLuong);

txtLuong = new JTextField();


txtLuong.setBounds(106, 25, 285, 20);
pnThongTin.add(txtLuong);
txtLuong.setColumns(10);

JLabel lbXL = new JLabel("Xếp Loại");


lbXL.setFont(new Font("Tahoma", Font.BOLD, 14));
lbXL.setHorizontalAlignment(SwingConstants.TRAILING);
lbXL.setBounds(-88, 54, 163, 22);
pnThongTin.add(lbXL);
JComboBox comboBox = new JComboBox();
comboBox.setBounds(106, 57, 100, 20);
pnThongTin.add(comboBox);
}
}

public class bt3 extends JFrame implements ActionListener {


// Khai báo các thành phần giao diện
private JTextField txtSoThu1, txtSoThu2, txtKetQua;
private JRadioButton radCong, radTru, radNhan, radChia;
private JButton btnThucHien, btnLamMoi, btnThoat;
public bt3() {
// Khởi tạo form
setTitle("Máy Tính Đơn Giản");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Tạo panel chứa các thành phần


JPanel panel = new JPanel(new GridLayout(5, 2, 5, 5));

// Thêm các thành phần vào panel


panel.add(new JLabel("Số Thứ 01:"));
txtSoThu1 = new JTextField();
panel.add(txtSoThu1);

panel.add(new JLabel("Số Thứ 02:"));


txtSoThu2 = new JTextField();
panel.add(txtSoThu2);

panel.add(new JLabel("Phép Toán:"));


radCong = new JRadioButton("Cộng");
radTru = new JRadioButton("Trừ");
radNhan = new JRadioButton("Nhân");
radChia = new JRadioButton("Chia");
ButtonGroup group = new ButtonGroup();
group.add(radCong);
group.add(radTru);
group.add(radNhan);
group.add(radChia);
panel.add(radCong);
panel.add(radTru);
panel.add(radNhan);
panel.add(radChia);

panel.add(new JLabel("Kết Quả:"));


txtKetQua = new JTextField();
txtKetQua.setEditable(false);
panel.add(txtKetQua);

// Thêm các nút bấm


btnThucHien = new JButton("Thực Hiện");
btnThucHien.addActionListener(this);
panel.add(btnThucHien);

btnLamMoi = new JButton("Làm Mới");


btnLamMoi.addActionListener(this);
panel.add(btnLamMoi);

btnThoat = new JButton("Thoát");


btnThoat.addActionListener(this);
panel.add(btnThoat);

// Thêm panel vào form


add(panel);
}
// Xử lý sự kiện click nút bấm
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnThucHien) {
// Lấy giá trị từ các ô nhập liệu
String soThu1Str = txtSoThu1.getText();
String soThu2Str = txtSoThu2.getText();
String phepToan = "";
if (radCong.isSelected()) {
phepToan = "+";
} else if (radTru.isSelected()) {
phepToan = "-";
} else if (radNhan.isSelected()) {
phepToan = "*";
} else if (radChia.isSelected()) {
phepToan = "/";
}
// Tính toán kết quả
double soThu1 = Double.parseDouble(soThu1Str);
double soThu2 = Double.parseDouble(soThu2Str);
double ketQua = 0;
try {
switch (phepToan) {
case "+":
ketQua = soThu1 + soThu2;
break;
case "-":
ketQua = soThu1 - soThu2;
break;
case "*":
ketQua = soThu1 * soThu2;
break;
case "/":
if (soThu2 == 0) {
throw new ArithmeticException("Chia cho 0");
}
ketQua = soThu1 / soThu2;
break;
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Lỗi: Nhập số không hợp lệ!", "Thông báo",
JOptionPane.ERROR_MESSAGE);
return;
} catch (ArithmeticException ex) {
JOptionPane.showMessageDialog(this, "Lỗi: " + ex.getMessage(), "Thông báo",
JOptionPane.ERROR_MESSAGE);
return;
}
// Hiển thị kết quả
txtKetQua.setText(String.format("%.2f", ketQua));
} else if (e.getSource() == btnLamMoi) {
// Xóa dữ liệu trong các ô nhập liệu
txtSoThu1.setText("");
txtSoThu2.setText("");
txtKetQua.setText("");

// Chọn lại nút radio mặc định


radCong.setSelected(true);
} else if (e.getSource() == btnThoat) {
// Thoát chương trình
System.exit(0);
}
}
public static void main(String[] args) {
// Hiển thị form
new bt3().setVisible(true);
}}
public class bt3 extends JFrame implements ActionListener {
private JTextField txtA, txtB, txtC;
private JLabel lblKetQua;
private JButton btnThucHien, btnLamMoi, btnThoat;
public bt3() {
setTitle("Xét loại tam giác");
setSize(400, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Tạo các widget


JLabel lblA = new JLabel("Cạnh A:");
txtA = new JTextField(10);

JLabel lblB = new JLabel("Cạnh B:");


txtB = new JTextField(10);

JLabel lblC = new JLabel("Cạnh C:");


txtC = new JTextField(10);
btnThucHien = new JButton("Thực hiện");
btnThucHien.addActionListener(this);

btnLamMoi = new JButton("Làm Mới");


btnLamMoi.addActionListener(this);

btnThoat = new JButton("Thoát");


btnThoat.addActionListener(this);

lblKetQua = new JLabel();


// Bố trí các widget
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2, 5, 5));

panel.add(lblA);
panel.add(txtA);

panel.add(lblB);
panel.add(txtB);

panel.add(lblC);
panel.add(txtC);

panel.add(btnThucHien);
panel.add(btnLamMoi);

panel.add(btnThoat);
panel.add(lblKetQua);

add(panel);
}
public static void main(String[] args) {
new bt3().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnThucHien) {
// Lấy giá trị từ các ô nhập
double a = Double.parseDouble(txtA.getText());
double b = Double.parseDouble(txtB.getText());
double c = Double.parseDouble(txtC.getText());
// Kiểm tra ba cạnh có hợp thành tam giác hay không
if (!isValidTriangle(a, b, c)) {
lblKetQua.setText("Ba cạnh A, B và C không hợp thành tam giác !!!");
return;
}
// Xác định loại tam giác
String triangleType = triangleType(a, b, c);
// Hiển thị kết quả
lblKetQua.setText("Loại tam giác: " + triangleType);
} else if (e.getSource() == btnLamMoi) {
// Xóa các giá trị trong các ô nhập
txtA.setText("");
txtB.setText("");
txtC.setText("");
// Hiển thị kết quả rỗng
lblKetQua.setText("");
} else if (e.getSource() == btnThoat) {
// Thoát chương trình
System.exit(0);
}
}
// Hàm kiểm tra ba cạnh có hợp thành tam giác hay không
private boolean isValidTriangle(double a, double b, double c) {
return a + b > c && a + c > b && b + c > a;
}
// Hàm xét loại tam giác
private String triangleType(double a, double b, double c) {
if (a == b && b == c) {
return "Tam giác đều";
} else if (a == b || b == c || a == c) {
return "Tam giác cân";
} else if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) {
return "Tam giác vuông";
} else {
return "Tam giác thường";
}
}
}
Buổi 4: Có file riêng
Buổi 5:
public class Bai01 {
private JFrame frame;
private JTextField txtHoTen;
private JTree tree;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Bai01 window = new Bai01();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Bai01() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 609, 430);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabel lblNewLabel = new JLabel("THÔNG TIN CÁC NGÀNH HỌC");


lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
lblNewLabel.setBounds(32, 0, 363, 36);
frame.getContentPane().add(lblNewLabel);
JButton btnThem = new JButton("Thêm");
btnThem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreeSelectionModel smd = tree.getSelectionModel();
if (smd.getSelectionCount() > 0) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
tree.getSelectionPath()
.getLastPathComponent();
DefaultMutableTreeNode newNode = new
DefaultMutableTreeNode(txtHoTen.getText());
selectedNode.add(newNode);
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.reload();
for (int i = 0; i < tree.getRowCount(); i++) {
tree.expandRow(i);
}
} else {
JOptionPane.showMessageDialog(null, "Bạn chưa chọn Node !!!");
}
}
});
btnThem.setBounds(10, 47, 112, 23);
frame.getContentPane().add(btnThem);

JButton btnCapNhat = new JButton("Cập Nhật");


btnCapNhat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreeSelectionModel smd = tree.getSelectionModel();
if (smd.getSelectionCount() > 0) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
tree.getSelectionPath()
.getLastPathComponent();
selectedNode.setUserObject(txtHoTen.getText());
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.reload();
for (int i = 0; i < tree.getRowCount(); i++) {
tree.expandRow(i);
}
} else {
JOptionPane.showMessageDialog(null, "Bạn chưa chọn Node !!!");
}
}
});
btnCapNhat.setBounds(10, 80, 111, 23);
frame.getContentPane().add(btnCapNhat);

JButton btnXoa = new JButton("Xoá");


btnXoa.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreeSelectionModel smd = tree.getSelectionModel();
if (smd.getSelectionCount() > 0) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
tree.getSelectionPath()
.getLastPathComponent();
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
if (selectedNode != tree.getModel().getRoot()) {
model.removeNodeFromParent(selectedNode);
model.reload();
for (int i = 0; i < tree.getRowCount(); i++) {
tree.expandRow(i);
}
} else {
JOptionPane.showMessageDialog(null, "Không được Xoá Node root !!!");
}
} else {
JOptionPane.showMessageDialog(null, "Bạn chưa chọn Node !!!");
}
}
});
btnXoa.setBounds(10, 111, 109, 23);
frame.getContentPane().add(btnXoa);

JLabel lblNhapTT = new JLabel("Nhập thông tin:");


lblNhapTT.setBounds(10, 147, 100, 14);
frame.getContentPane().add(lblNhapTT);

txtHoTen = new JTextField();


txtHoTen.setBounds(110, 144, 122, 20);
frame.getContentPane().add(txtHoTen);
txtHoTen.setColumns(10);

JPanel pnTree = new JPanel();


pnTree.setBounds(242, 47, 182, 204);
frame.getContentPane().add(pnTree);
pnTree.setLayout(new BorderLayout(0, 0));

JScrollPane scrollPane = new JScrollPane();


pnTree.add(scrollPane, BorderLayout.NORTH);

tree = new JTree();


tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("KCNTT") {
{
DefaultMutableTreeNode node_1;
node_1 = new DefaultMutableTreeNode("Ngành KTPM");
node_1.add(new DefaultMutableTreeNode(""));
node_1.add(new DefaultMutableTreeNode(""));
node_1.add(new DefaultMutableTreeNode(""));
node_1.add(new DefaultMutableTreeNode(""));
add(node_1);
node_1 = new DefaultMutableTreeNode("Ngành TNNT");
node_1.add(new DefaultMutableTreeNode(""));
node_1.add(new DefaultMutableTreeNode(""));
node_1.add(new DefaultMutableTreeNode(""));
node_1.add(new DefaultMutableTreeNode(""));
add(node_1);
node_1 = new DefaultMutableTreeNode("Ngành KHDL");
node_1.add(new DefaultMutableTreeNode(""));
node_1.add(new DefaultMutableTreeNode(""));
node_1.add(new DefaultMutableTreeNode(""));
node_1.add(new DefaultMutableTreeNode(""));
add(node_1);
}
}));
pnTree.add(tree, BorderLayout.CENTER);
}
}
public class Bai02 {
private JFrame frame;
private JLabel lbHinh;
/**
* Launch the application. */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Bai02 window = new Bai02();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Bai02() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JButton btnNewButton = new JButton("\u0110\u00D3NG");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnNewButton.setBounds(147, 11, 89, 23);
frame.getContentPane().add(btnNewButton);

JPanel panel = new JPanel();


panel.setBounds(0, 50, 434, 212);
frame.getContentPane().add(panel);
panel.setLayout(null);

lbHinh = new JLabel("");


lbHinh.setBounds(0, 11, 434, 207);
panel.add(lbHinh);
lbHinh.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
if (SwingUtilities.isRightMouseButton(e)){
showImg(e.getX(),e.getY());
}
}
});
}

private void showImg(int x, int y){


JPopupMenu pop = new JPopupMenu();
pop.add("Nha trang").addActionListener(e ->
showImage("danhSachHinh/halongbay.jpg" ));
pop.add("Hoi an").addActionListener(e -> showImage("danhSachHinh/hoian.jpg" ));
pop.add("Phong Nha ke bang").addActionListener(e ->
showImage("danhSachHinh/phongnhakebang1.jpg" ));
pop.add("sapa").addActionListener(e -> showImage("danhSachHinh/sapa.jpg" ));
pop.show(lbHinh, x, y);
}
private void showImage (String path){
ImageIcon icon = new ImageIcon(path);
lbHinh.setIcon(icon);
}
}

You might also like