Chắc là code này
Mã:
if(e.getSource() == btnstar)
{
String c1 = String.valueOf(txtc1.getText());
String c2 = String.valueOf(txtc2.getText());
txtc1.setText(c2);
txtc2.setText(c1);
}
Thêm một cách giải của bài 2 (dùng GridBagLayout)
Mã:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class test implements ActionListener {
JFrame frame;
JLabel lblc1 = new JLabel("Chuoi 1: ");
JLabel lblc2 = new JLabel("Chuoi 2: ");
JTextField txtc1 = new JTextField(20);
JTextField txtc2 = new JTextField(20);
JButton btnstar = new JButton("Bat dau");
JButton btnend = new JButton("Thoat");
public test(String title) {
frame = new JFrame();
frame.setTitle(title);
}
public void show() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void addControls() {
GridBagLayout gbl = new GridBagLayout();
Container con = frame.getContentPane();
con.setLayout(gbl);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.gridx = 0; //cot
c.gridy = 0; //dong
con.add(lblc1, c);
c.gridx = 1;
con.add(txtc1, c);
c.gridx = 0;
c.gridy = 1;
con.add(lblc2, c);
c.gridx = 1;
c.gridy = 1;
con.add(txtc2, c);
c.gridx = 1;
c.gridy = 2;
con.add(btnstar, c);
c.gridx = 1;
c.gridy = 3;
con.add(btnend, c);
btnstar.addActionListener(this);
btnend.addActionListener(this);
txtc1.setText("Chao mung den voi java");
txtc2.setText("Chao mung den voi 2mit");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try
{
if(e.getSource() == btnstar)
{
String c1 = String.valueOf(txtc1.getText());
String c2 = String.valueOf(txtc2.getText());
txtc1.setText(c2);
txtc2.setText(c1);
}
if(e.getSource() == btnend)
{
int hoi = JOptionPane.showConfirmDialog(null, "Ban co muon thoat khong?");
if (hoi == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
catch(Exception Exception)
{
JOptionPane.showMessageDialog(null, "Co loi xay ra!");
}
}
public static void main(String[] args) {
test h = new test("Test");
h.addControls();
h.show();
}
}
--- Nhập chung bài viết,
20 Tháng mười hai 2012 ---
Mọi người ai làm bài 1 chưa gửi mình tham khảo với. Cũng làm rồi nhưng mà có khác tý so với đề
. Làm đúng đề nó không chịu chạy 
Mã:
import java.util.*;
public class xmnk extends xe {
int t; //bien t = thue
Scanner s = new Scanner(System.in);
//phuong thuc tao xmnk
public xmnk()
{
try
{
System.out.println("Hieu xe: ");
hieuxe = s.next();
System.out.println("Gia xe xuat xuong: ");
gia = s.nextFloat();
System.out.println("Nuoc SX: ");
ng = s.next();
if(!ng.equals("vn"))
{
System.out.println("Thue nhap khau: ");
t = s.nextInt();
}
}
catch(Exception e)
{
System.out.println("Co mot loi khac xay ra!");
}
}
@Override
public float tinhGiaThuc() {
// TODO Auto-generated method stub
float giathuc = gia;
if(!ng.equals("vn"))
{
giathuc = gia + ((gia * t) / 100);
}
return giathuc;
}
public String toString()
{
return "Hieu xe: " + hieuxe + "\nNuoc SX: " + ng + "\nGia xe: " + tinhGiaThuc();
}
}