cambeme
Giới tính : Cung : Con giáp : Bài gửi : 174 Điểm : 5243 Thanks : 0 Sinh nhật : 05/12/1991 Ngày gia nhập : 25/03/2011 Tuổi : 32 Đến từ : Thành phố Hồ Chí minh Nghề nghiệp/ước mơ : IT Tâm trạng : Hưng phắn Châm ngôn sống : Không có gì quý hơn hột vịt thịt kho!
Cao nhân tắt thở vô phương trị!
| Tiêu đề: Bài tập SwingDemo 16/9/2011, 12:22 | |
| | 16/9/2011, 12:22 Bài tập SwingDemo | | | Bài 1: - Code:
-
import javax.swing.*;
public class cSwingEx1 extends JFrame { JLabel lblUsername = new JLabel("Name"), lblPassword = new JLabel("Pwd"); JTextField txtUsername = new JTextField(); JPasswordField txtPassword = new JPasswordField(); JButton bttOK = new JButton("OK"); JButton bttCancel = new JButton("Cancel");
/* * constructor */ public cSwingEx1() { // set window title setTitle("Swing Demo"); // set size setSize(250, 150); // reset null layout setLayout(null); // add labels to window add(lblUsername); add(lblPassword); // add textfield to window add(txtPassword); add(txtUsername); txtUsername.setHorizontalAlignment(4); // add button to window add(bttOK); add(bttCancel); // set location lblUsername.setBounds(20, 20, 60, 25); lblPassword.setBounds(20, 50, 60, 25); txtUsername.setBounds(75, 20, 150, 25); txtPassword.setBounds(75, 50, 150, 25); bttOK.setBounds(20, 80, 100, 25); bttCancel.setBounds(130, 80, 100, 25); }
public static void main(String[] args) { // TODO Auto-generated method stub // new window cSwingEx1 wMain = new cSwingEx1(); // end program as closing window. wMain.setDefaultCloseOperation(EXIT_ON_CLOSE); // show the window wMain.setVisible(true); } } Bài 2: - Code:
-
import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.jws.Oneway; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField;
public class cSwingEx2 extends JFrame implements ActionListener { JTextField txtNumber = new JTextField(); JButton btnTranslate = new JButton("trans"); JTextField txtEnglish = new JTextField(); /* * constructor */ public cSwingEx2() { // set window title setTitle("Translate"); // set size setSize(162, 192); // reset null layout setLayout(null); // add textfield to window add(txtNumber); add(txtEnglish); // add button to window add(btnTranslate); // set location txtNumber.setBounds(23, 50, 100, 25); txtNumber.setHorizontalAlignment(JTextField.RIGHT); btnTranslate.setBounds(23, 100, 100, 25); btnTranslate.addActionListener(this); txtEnglish.setEnabled(false); txtEnglish.setBounds(23, 125, 100, 25); txtEnglish.setHorizontalAlignment(JTextField.RIGHT); txtEnglish.setForeground(Color.BLACK); }
public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub String strVar = txtNumber.getText(); if (strVar.equals("9")) { txtEnglish.setText("NINE"); } else { if (strVar.equals("8")) { txtEnglish.setText("EIGHT"); } else { if (strVar.equals("7")) { txtEnglish.setText("SEVEN"); } else { if (strVar.equals("6")) { txtEnglish.setText("SIX"); } else { if (strVar.equals("5")) { txtEnglish.setText("FIVE"); } else { if (strVar.equals("4")) { txtEnglish.setText("FOUR"); } else { if (strVar.equals("3")) { txtEnglish.setText("THREE"); } else { if (strVar.equals("2")) { txtEnglish.setText("TWO"); } else { if (strVar.equals("1")) { txtEnglish.setText("ONE"); } else { txtEnglish.setText(""); txtNumber.setText(""); } } } } } } } } } }
public static void main(String[] args) { // TODO Auto-generated method stub // new window cSwingEx2 wMain = new cSwingEx2(); // end program as closing window. wMain.setDefaultCloseOperation(EXIT_ON_CLOSE); // show the window wMain.setVisible(true); }
}
Bài 3: - Code:
-
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField;
public class cSwingEx3 extends JFrame implements ActionListener { JTextField txtText = new JTextField(); JTextField txtAnalysis = new JTextField(); JButton bttAnalyse = new JButton("analyse"); JButton bttClear = new JButton("clear"); JLabel lblNoWs = new JLabel("N.o.Ws:"); JTextArea txtArea = new JTextArea();
/* * constructor */ public cSwingEx3() { // set window title setTitle("Words Analysis"); // set size setSize(300, 400); // reset null layout setLayout(null); // add textfield to window add(txtText); add(txtAnalysis); // add button to window add(bttClear); add(bttAnalyse); // add label to window add(lblNoWs); // add textarea to window add(txtArea); // set location txtText.setBounds(15, 35, 270, 25); txtText.setHorizontalAlignment(JTextField.RIGHT); txtAnalysis.setBounds(160, 105, 125, 25); bttClear.setBounds(160, 70, 125, 25); bttClear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub txtText.setText(""); txtArea.setText(""); txtAnalysis.setText(""); } }); bttAnalyse.addActionListener(this); bttAnalyse.setBounds(15, 70, 125, 25); lblNoWs.setBounds(15, 105, 125, 25); txtArea.setBounds(15, 140, 265, 300); }
public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub String strVar = txtText.getText().trim(); // First String sM = ""; int iVar = 0; for (int i = 0; i < strVar.length(); i++) { if (strVar.charAt(i) != ' ') { sM = sM + strVar.charAt(i) + "\n"; iVar++; } } txtArea.setText(sM); txtAnalysis.setText("" + iVar); }
public static void main(String[] args) { // TODO Auto-generated method stub // new window cSwingEx3 wMain = new cSwingEx3(); // end program as closing window. wMain.setDefaultCloseOperation(EXIT_ON_CLOSE); // show the window wMain.setVisible(true); } }
Bài 4: (Riêng bài này mình làm theo dạng Frame không giống 3 bài kia lắm ) - Code:
-
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.lang.Math;
public class cSwingEx4 extends JFrame implements ActionListener { /* * constructor */ public cSwingEx4() { // add frame to window frame = new JFrame("Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 110); frame.setLayout(new FlowLayout()); // add label to frame operation = new JLabel("Chon phep tinh"); result = new JLabel("Ket qua"); is = new JLabel("="); // add panel to frame buttons = new JPanel(); center = new JPanel(); // add textfield to frame number1 = new JTextField(15); number2 = new JTextField(15); // add button to frame Cong = new JButton("+"); Tru = new JButton("-"); Nhan = new JButton("*"); Chia = new JButton("/"); sin = new JButton("sinx"); cos = new JButton("cosx"); buttons.add(Cong); buttons.add(Tru); buttons.add(Nhan); buttons.add(Chia); buttons.add(sin); buttons.add(cos); center.add(number1); center.add(operation); center.add(number2); center.add(is); center.add(result); frame.add(center); frame.add(buttons); frame.setVisible(true); }
/* * * Activating action for buttons */ public void activateEvents() { Cong.addActionListener(this); Tru.addActionListener(this); Nhan.addActionListener(this); Chia.addActionListener(this); sin.addActionListener(this); cos.addActionListener(this); }
public void actionPerformed(ActionEvent al) { // Verify that the correct values are given try { Double.parseDouble(number1.getText()); Double.parseDouble(number2.getText()); if (al.getSource() == Cong) { operation.setText("Cong"); double pole1 = Double.parseDouble(number1.getText()); double pole2 = Double.parseDouble(number2.getText()); double resultd = pole1 + pole2; result.setText(Double.toString(resultd)); } if (al.getSource() == Tru) { operation.setText("Tru"); double pole1 = Double.parseDouble(number1.getText()); double pole2 = Double.parseDouble(number2.getText()); double resultd = pole1 - pole2; result.setText(Double.toString(resultd)); } if (al.getSource() == Nhan) { operation.setText("Nhan"); double pole1 = Double.parseDouble(number1.getText()); double pole2 = Double.parseDouble(number2.getText()); double resultd = pole1 * pole2; result.setText(Double.toString(resultd)); } if (al.getSource() == Chia) { operation.setText("Cong"); double pole1 = Double.parseDouble(number1.getText()); double pole2 = Double.parseDouble(number2.getText()); if (pole2 == 0) { JOptionPane.showMessageDialog(null, "Khong the chia cho 0!", "Error!", JOptionPane.ERROR_MESSAGE); double resultd = 0; result.setText(Double.toString(resultd)); return; } double resultd = pole1 / pole2; result.setText(Double.toString(resultd)); } if (al.getSource() == sin) { operation.setText("sinx"); double pole1 = Double.parseDouble(number1.getText()); double resultd = Math.sin(pole1); } if (al.getSource() == cos) { operation.setText("cosx"); double pole1 = Double.parseDouble(number1.getText()); double resultd = Math.cos(pole1); result.setText(Double.toString(resultd)); } } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog(null, "Xin dien so dung " + "vao ca hai khung", "Error!", JOptionPane.ERROR_MESSAGE); }
}
private JFrame frame; private JPanel center, buttons; private JLabel operation, result, is; private JTextField number1, number2; private JButton Cong, Tru, Nhan, Chia, sin, cos;
public static void main(String[] args) { cSwingEx4 wMain = new cSwingEx4(); wMain.setDefaultCloseOperation(EXIT_ON_CLOSE); wMain.activateEvents(); wMain.setVisible(false); } }
| | | | | |
|