วันจันทร์ที่ 5 กันยายน พ.ศ. 2554

ส่งค่าจาก Textfield ไป หน้าอื่น


import javax.swing.JFrame;
import javax.swing.JLabel;
public class P2 extends JFrame{

 P2(String Gebtx1,String GebTx2,int sum){
 
  JLabel lb1 = new JLabel(Gebtx1);
  JLabel lb2 = new JLabel(GebTx2);
  JLabel lb3 = new JLabel(""+sum);
 
 
 
  setLayout(null);
  setBounds(50,100,300,500);
  setVisible(true);
 
 
  add(lb1);
  add(lb2);
  add(lb3);
 
  lb1.setBounds(10, 10, 100, 25);
  lb2.setBounds(10, 40, 100, 25);
  lb3.setBounds(10, 70, 100, 25);
 }
}

/////////////Class  ที่สอง


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Send extends JFrame implements ActionListener{
 JTextField tx1 = new JTextField(10);
 JTextField tx2 = new JTextField(10);
 JButton click = new JButton("pk");
 String GebTx1 ="";
 String GebTx2 ="";
 Send(){
  setLayout(null);
  setBounds(50,100,300,500);
  setVisible(true);
  add(tx1);
  add(tx2);
  add(click);
 
  tx1.setBounds(10, 10, 100, 25);
  tx2.setBounds(10, 40, 100, 25);
  click.setBounds(10, 70, 100, 25);
 
  click.addActionListener(this);
 }
 public static void main(String[] args) {
  new Send();
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  if(e.getSource()==click){
   GebTx1 = tx1.getText();
   GebTx2 = tx2.getText();
   int num1 = Integer.parseInt(GebTx1);
   int num2 = Integer.parseInt(GebTx2);
  
   JOptionPane.showMessageDialog(null, num1+num2)
;   new P2(GebTx1,GebTx2,num1+num2);
  }
 
 }
}


ไม่มีความคิดเห็น:

แสดงความคิดเห็น