Tải bản đầy đủ (.docx) (15 trang)

Bài tập thiết kế giao diện môn Lập trình JAVA kèm lời giải chi tiết

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (245.32 KB, 15 trang )

Giải bài tập_Lập trình java _Tạo giao diện 01/2018

DA_NG_NI

TỔNG HỢP JAVA_GIAO DIỆN CỦA TÔI
Trước khi giải bài tập, lưu ý các vấn đề sau:
ĐỂ TẠO Project thiết kế giao diện TRONG JAVA TA CHỌN THEO HÌNH SAU:

 ĐỂ TIẾN HÀNH CHẠY CODE TA CHUYỂN VỀ GIAO DIỆN VIẾT CODE [click nút source]=> CLICK PHẢI
CHUỘT CHỌN RUN FILE [shift +F6] để chạy code hiện hành


Giải bài tập_Lập trình java _Tạo giao diện 01/2018
Cách đặt địa chỉ tên của đối tượng:
Chọn code=> gõ địa chỉ đối tượng vào ô Variable Name

1.

Cách đặt tên đối tượng: chon Properties=> gõ tên vào ô Text

3. Khi viết code cho đối tượng có liên quan đến click chuột ta ta cần:
- chọn đối tượng.
- chọn sự kiện mouseClick=> tự động phát sinh hàm.
- viết code xử lý trong hàm này.

Trong các bài tập phía dưới vì code xử lý sự
kiện và khởi tạo giao diện do ngôn ngữ quy
định viết lồng vào nhau nên tôi đã lược bỏ
một số code phát sinh tự động. Chỉ lưu lại
code xử lý chính.
4.Viết cho sự kiện load form ta viết sau :



BÀI SỐ 1

DA_NG_NI


Giải bài tập_Lập trình java _Tạo giao diện 01/2018

DA_NG_NI

1.Thiết kế giao diện máy tính đơn giản

//LƯU Ý CÁC CODE CỦA BUTTON TA VIẾT TRONG SỰ KIỆN CLICK(=> LỰA CHỌN MOUSE CLICK TRONG
DẤU SÉT ĐÁNH RỒI CHỌN MOUSE)
//CHỨ KO CĨ KÍCH ĐÚP VÀO ĐỐI TƯỢNG

Tôi đã lược bỏ một số đoạn code tự phát sinh trong bài…chỉ lưu lại code của đối tượng xử lý khi chọn sự kiện như sau:

package laptrinh_java;
public class MayTinh_JFrameForm extends javax.swing.JFrame {
String so1,so2,pt;
public MayTinh_JFrameForm() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
// …Đã lược bỏ 1 số code tạo giao diện tự phát sinh
///--------------------------NÚT SỐ 1

private void btn_1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:

txt_hienthi.setText(txt_hienthi.getText() + btn_1.getText());
}
///--------------------------NÚT SỐ 2
private void btn_2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi.setText(txt_hienthi.getText() + btn_2.getText());
}


Giải bài tập_Lập trình java _Tạo giao diện 01/2018
///--------------------------NÚT SỐ 3
private void btn_3MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi.setText(txt_hienthi.getText() + btn_3.getText());
}

///--------------------------NÚT SỐ 4
private void btn_4MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi.setText(txt_hienthi.getText() + btn_4.getText());
}
///--------------------------NÚT CỘNG
private void btn_congMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
so1=txt_hienthi.getText();
pt="+";
txt_hienthi.setText("");

}

///--------------------------NÚT TRỪ
private void btn_bangMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
long kq=0;
so2=txt_hienthi.getText();
switch(pt)
{
case "+":
kq=Integer.parseInt(so1) + Integer.parseInt(so2);
txt_hienthi.setText(String.valueOf(kq));
break;
case "-":
kq=Integer.parseInt(so1) - Integer.parseInt(so2);
txt_hienthi.setText(String.valueOf(kq));
break;
case "*":
kq=Integer.parseInt(so1) * Integer.parseInt(so2);
txt_hienthi.setText(String.valueOf(kq));
break;
case "/":
if(Integer.parseInt(so2)!=0)
{
kq=Integer.parseInt(so1) / Integer.parseInt(so2);
txt_hienthi.setText(String.valueOf(kq));
break;
}
else
{

txt_hienthi.setText("Div 0");
}
}
}

///--------------------------NÚT RESET
private void btn_resetMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi.setText("");

DA_NG_NI


Giải bài tập_Lập trình java _Tạo giao diện 01/2018
}
///--------------------------NÚT SỐ 5
private void btn_5MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi.setText(txt_hienthi.getText() + btn_5.getText());
}

///--------------------------NÚT SỐ 6
private void btn_6MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi.setText(txt_hienthi.getText() + btn_6.getText());
}
///--------------------------NÚT SỐ 7
private void btn_7MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi.setText(txt_hienthi.getText() + btn_7.getText());

}

///--------------------------NÚT SỐ 8
private void btn_8MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi.setText(txt_hienthi.getText() + btn_8.getText());
}

///--------------------------NÚT SỐ 9
private void btn_9MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi.setText(txt_hienthi.getText() + btn_9.getText());
}

///--------------------------NÚT SỐ 0
private void btn_0MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi.setText(txt_hienthi.getText() + btn_0.getText());
}
///--------------------------NÚT TRỪ
private void btn_truMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
so1=txt_hienthi.getText();
pt="-";
txt_hienthi.setText("");
}

///--------------------------NÚT CHIA
private void btn_chiaMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:

so1=txt_hienthi.getText();
pt="/";
txt_hienthi.setText("");
}
///--------------------------NÚT NHÂN
private void btn_nhanMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
so1=txt_hienthi.getText();

DA_NG_NI


Giải bài tập_Lập trình java _Tạo giao diện 01/2018
pt="*";
txt_hienthi.setText("");
}
}
BÀI SỐ 2
Tạo giao diện đọc và ghi nội dung file .txt

package laptrinh_java_thietkegiaodien_hoanchinh;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ShowOpenDialog_DocGhi_File extends javax.swing.JFrame {
public ShowOpenDialog_DocGhi_File() {
initComponents();
filechooser_docghi_file.setVisible(false);
txt_hienthi.setVisible(false);
lb_ht.setVisible(false);

}
@SuppressWarnings("unchecked")
public String file_name;
//-----------------------------------------------------NÚT OPEN
private void btn_openMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
filechooser_docghi_file.setVisible(true);
txt_hienthi.setVisible(true);
lb_ht.setVisible(true);
txt_hienthi_file.setEnabled(false);

// mo hop thoai FileChooser
int returnVal=filechooser_docghi_file.showOpenDialog(this);
// lay dia chi File duoc mo

DA_NG_NI


Giải bài tập_Lập trình java _Tạo giao diện 01/2018

DA_NG_NI

java.io.File file=filechooser_docghi_file.getSelectedFile();
// chuyen dia chi sang chuoi
file_name=file.toString();
// doc noi dung file
FileInputStream fis;
try
{
fis=new FileInputStream(file_name);

}
catch(FileNotFoundException fex)
{
System.out.println("Error \n\t" + fex);
return;
}
int i = 0;
do
{
try {
i=fis.read();
} catch (IOException ex) {
Logger.getLogger(ShowOpenDialog_DocGhi_File.class.getName()).log(Level.SEVERE, null, ex);
}
if(i!=-1)
{
txt_hienthi_file.setText(txt_hienthi_file.getText() + String.valueOf((char)i));
}
}while(i!=-1);
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(ShowOpenDialog_DocGhi_File.class.getName()).log(Level.SEVERE, null, ex);
}
}
//-----------------------------------------------------NÚT CHỈNH SỬA
private void btn_editMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_hienthi_file.setEnabled(true);
}

//-----------------------------------------------------NÚT SAVE
private void btn_saveMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
// mo hop thoai FileChooser
int returnVal=filechooser_docghi_file.showSaveDialog(this);
// lay dia chi File duoc mo
java.io.File file=filechooser_docghi_file.getSelectedFile();
// chuyen dia chi sang chuoi
file_name=file.toString();
// ghi noi dung vao file
FileOutputStream fos;
try
{
fos=new FileOutputStream(file_name);
}catch(FileNotFoundException fex)


Giải bài tập_Lập trình java _Tạo giao diện 01/2018

DA_NG_NI

{
System.out.println("Error \n\t" + fex);
return;
}
char[] ch=txt_hienthi_file.getText().toCharArray();
for(int i=0;i{
try {
fos.write(ch[i]);

}
catch (IOException ex) {
Logger.getLogger(ShowOpenDialog_DocGhi_File.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
fos.close();
}
catch (IOException ex) {
Logger.getLogger(ShowOpenDialog_DocGhi_File.class.getName()).log(Level.SEVERE, null, ex);
}
txt_hienthi_file.setEnabled(false);
txt_hienthi_file.setText("");
}
public static void main(String args[])
{
….
}

BÀI 3
TẠO GIAO DIỆN GIẢI PHƯƠNG TRÌNH BẬC 1


Giải bài tập_Lập trình java _Tạo giao diện 01/2018

DA_NG_NI

package laptrinh_java_thietkegiaodien_hoanchinh;
public class giaipt1_JFrameForm extends javax.swing.JFrame {
String a,b,x;

public giaipt1_JFrameForm() {
initComponents();
}
@SuppressWarnings("unchecked")
//---------------------------------------NÚT KẾT QUẢ TRONG SỰ KIỆN CLICK CHUỘT
private void btn_ketquaMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
float x=((-1) * Float.parseFloat(txt_b.getText())) / Float.parseFloat(txt_a.getText());
txt_ketqua.setText((String.valueOf(x)));
}
//---------------------------------------NÚT RESET TRONG SỰ KIỆN CLICK CHUỘT
private void btn_lamlaiMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_a.setText("");
txt_b.setText("");
txt_ketqua.setText("");
}
}

BÀI 4
TẠO GIAO DIỆN GIẢI PHƯƠNG TRÌNH BẬC 2


Giải bài tập_Lập trình java _Tạo giao diện 01/2018

DA_NG_NI

package laptrinh_java_thietkegiaodien_hoanchinh;
public class giaipt2_JFrameForm extends javax.swing.JFrame {
String a,b,c,dt,x,x1,x2;

public giaipt2_JFrameForm() {
initComponents();
}
@SuppressWarnings("unchecked")
//-------------------------NÚT RESET TRONG SỰ KIỆN CLICK CHUỘT
private void btn_resetMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txt_ax2.setText("");
txt_bx.setText("");
txt_c.setText("");
txt_kqua.setText("");
//-------------------------NÚT KẾT QUẢ TRONG SỰ KIỆN CLICK CHUỘT
private void btn_kqMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
a=txt_ax2.getText();
b=txt_bx.getText();
c=txt_c.getText();
double dt=Math.pow(Float.parseFloat(b),2) - 4*Float.parseFloat(a)*Float.parseFloat(c);
if(dt==0)
{
float x=((-1) * Float.parseFloat(b)) / ( 2 * Float.parseFloat(a));
txt_kqua.setText("Phương trình có nghiệm kép x = " + String.valueOf(x));
}
else
{
if(dt>0)
{
double x1=(((-1) * Float.parseFloat(b)) + Math.sqrt(dt)) / ( 2 * Float.parseFloat(a));
double x2=(((-1) * Float.parseFloat(b)) - Math.sqrt(dt)) / ( 2 * Float.parseFloat(a));
txt_kqua.setText("Phương trình có 2 nghiệm \n\t x1 = " + String.valueOf(x1) + "\n\t x2 = " + String.valueOf(x2));

}
else
{
txt_kqua.setText( "Phương trình vô nghiệm" );
}
public static void main(String args[]) {

new giaipt2_JFrameForm().setVisible(true);
});
}
BÀI 5
TẠO GIAO DIỆN CHO PHÉP LỰA CHỌN 2 CHỨC NĂNG GIẢI PHƯƠNG TRÌNH TRÊN


Giải bài tập_Lập trình java _Tạo giao diện 01/2018

DA_NG_NI

package laptrinh_java_thietkegiaodien_hoanchinh;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Giaipt_1_2_FrameForm extends javax.swing.JFrame {
String a,b,c,x,x1,x2;
private Object gpb_chonpt;
public Giaipt_1_2_FrameForm() {
initComponents();
rd_ptb1.setSelected(true);
txt_nhapa.setVisible(false);
lb_x2.setVisible(false);

ButtonGroup gpb_chonpt = new ButtonGroup();
gpb_chonpt.add(rd_ptb1);
gpb_chonpt.add(rd_ptb2);
}
@SuppressWarnings("unchecked")
private void initComponents()
{
..hàm xây dựng giao diện tự phát sinh
}
//--------------------------NÚT KẾT QUẢ TRONG SỰ KIỆN CLICK
private void btn_ketquaMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
a=txt_nhapa.getText();
b=txt_nhapb.getText();
c=txt_nhapc.getText();
double x,xx,x1,x2;
DecimalFormat f = new DecimalFormat("####.00");//định dạng làm tròn số
if(rd_ptb1.isSelected())
{
x=((-1) * Double.parseDouble(c)) / Double.parseDouble(b);
txt_ketqua.setText("Phương trình co nghiệm \n\t x = " + String.valueOf(f.format(x)));
}
if(rd_ptb2.isSelected())
{
rd_ptb1.setSelected(false);
txt_nhapa.setVisible(true);
lb_x2.setVisible(true);
double dt=Math.pow(Double.parseDouble(b),2) - 4*Double.parseDouble(a)*Double.parseDouble(c);
if(dt==0)
{

xx=((-1) * Double.parseDouble(b)) / ( 2 *Double.parseDouble(a));
txt_ketqua.setText("Phương trình có nghiệm kép x = " + String.valueOf(f.format(xx)));
}
else
{
if(dt>0)
{
x1=(((-1) * Double.parseDouble(b)) + Math.sqrt(dt)) / ( 2 * Double.parseDouble(a));


Giải bài tập_Lập trình java _Tạo giao diện 01/2018

DA_NG_NI

x2=(((-1) * Double.parseDouble(b)) - Math.sqrt(dt)) / ( 2 * Double.parseDouble(a));
txt_ketqua.setText("Phương trình có 2 nghiệm \n\t x1 = " + String.valueOf(f.format(x1)) + "\n\t x2 = " +
String.valueOf(f.format(x2)));
}
else
{
txt_ketqua.setText( "Phương trình vơ nghiệm" );
}
}
}
}
//--------------------------Radio pt bậc 1 trong sự kiện CLICK
private void rd_ptb1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if(rd_ptb1.isSelected())
{

txt_nhapa.setVisible(false);
lb_x2.setVisible(false);
}
}

//--------------------------Radio pt bậc 2 trong sự kiện CLICK
private void rd_ptb2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if(rd_ptb2.isSelected())
{
txt_nhapa.setVisible(true);
lb_x2.setVisible(true);
}
}

//--------------------------Nút làm lại trong sự kiện CLICK
private void btn_lamlaiMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
rd_ptb1.setSelected(true);
txt_nhapa.setVisible(false);
lb_x2.setVisible(false);
txt_nhapa.setText("");
txt_nhapb.setText("");
txt_nhapc.setText("");
txt_ketqua.setText("");
}
BÀI 6
TẠO GIAO DIỆN TÍNH PHÉP TỐN PHÂN SỐ



Giải bài tập_Lập trình java _Tạo giao diện 01/2018

DA_NG_NI

String tu1,tu2,mau1,mau2,cong,tru;
public CongTru_Phanso_JFrameForm() {
initComponents();
}
@SuppressWarnings("unchecked")
static int uscln(int m, int n)
{
m = Math.abs(m);
n = Math.abs(n);
int ucnn = (m < n) ? m : n;
while (m % ucnn != 0 || n % ucnn != 0)
{
ucnn--;
}
return ucnn;
}
public void toigian()
{
int tt, tm;
tt=Integer.parseInt(txt_tu1.getText());
tt=Integer.parseInt(txt_tu1.getText());
}
//-------------------------NÚT CỘNG PHÂN SỐ TRONG SỰ KIỆN CLICK
private void btn_congMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
tu1=txt_tu1.getText();

mau1=txt_mau1.getText();
tu2=txt_tu2.getText();
mau2=txt_mau2.getText();
if(Double.parseDouble(mau1)==0)
{
txt_mau1.setText("");
}
DecimalFormat f = new DecimalFormat("####.##");
double t=(Double.parseDouble(tu1) * Double.parseDouble(mau2)) + (Double.parseDouble(tu2) *
Double.parseDouble(mau1));
double m=Double.parseDouble(mau1)*Double.parseDouble(mau2);
double cong=t/m;
txt_hienthi.setText(String.valueOf(f.format(cong)));
}
//-------------------------NÚT TRỪ PHÂN SỐ TRONG SỰ KIỆN CLICK
private void btn_truMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
tu1=txt_tu1.getText();
mau1=txt_mau1.getText();
tu2=txt_tu2.getText();
mau2=txt_mau2.getText();
DecimalFormat f = new DecimalFormat("####.##");
double tu=(Double.parseDouble(tu1) * Double.parseDouble(mau2)) - (Double.parseDouble(tu2) *
Double.parseDouble(mau1));
double mau=Double.parseDouble(mau1)*Double.parseDouble(mau2);
double tru=tu/mau;
txt_hienthi.setText(String.valueOf(f.format(tru)));
}
public static void main(String args[]) {
/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CongTru_Phanso_JFrameForm().setVisible(true);


Giải bài tập_Lập trình java _Tạo giao diện 01/2018
}
});
}

DA_NG_NI


Giải bài tập_Lập trình java _Tạo giao diện 01/2018

DA_NG_NI



×