如何将一个表单的值发送到另一个表单java与jframe(我检查“在JFrames之间传递值”,我不明白答案)

时间:2017-01-03 20:26:54

标签: java swing jtable

我正在尝试学习java。 我创建架构,但我不知道如何以两种形式发送数据这里是我的源代码......

这部分是桌子和按钮;

package main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Form1 extends JFrame implements ActionListener 
{
   JButton button = new JButton("Open form");
   public Form1(){
      String[] column = {"Name", "Surname"};
      Object[][] data ={};

      DefaultTableModel model = new DefaultTableModel(data, column);
      JTable table = new JTable(model);
      JScrollPane pane = new JScrollPane(table);
      JFrame frame = new JFrame();
      button.setBounds(150, 300, 80, 35);
      pane.setBounds(150, 200, 150, 80);
      frame.add(pane);
      frame.add(button);
      frame.setLayout(null);
      frame.setTitle("Form1");
      frame.setResizable(false);
      frame.setSize(450,380);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true); 
      button.addActionListener(this);

   }
   public void actionPerformed (ActionEvent action){
      if(action.getSource() == button )
      {
         Form2 f2 = new Form2();
         dispose();
      }
   } 
   public static void main(String[] args) {
      Form1 f1 = new Form1();
   }  
}

我想从这个表单中获取值;

package main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class Form2 extends JFrame implements ActionListener{
   JTextField txt = new JTextField(10);
   JTextField txt2 = new JTextField(10);
   JButton addButton = new JButton("add");
   public Form2(){
      JFrame frame = new JFrame();
      txt.setBounds(20, 50, 80, 22);
      txt2.setBounds(20, 100, 80, 22);
      addButton.setBounds(20, 150, 80, 30);

      frame.add(txt);
      frame.add(txt2);
      frame.add(addButton);
      frame.setLayout(null);
      frame.setTitle("Form2");
      frame.setResizable(false);
      frame.setSize(450,380);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      addButton.addActionListener(this);    
   }
   public void actionPerformed (ActionEvent action){
      if(action.getSource() == addButton){
         String Name= this.txt.getText();
         String Surname= this.txt2.getText();
         //DefaultTableModel model = (DefaultTableModel) Form1.table.getModel();
         Object[] row = {Name, Surname};
         Form1 f1 = new Form1();
         dispose();
      }
   }  
}

我检查了教程和其他youtube频道,没有人不喜欢这个你能不能给我一个灵感来学习教程或例子。

0 个答案:

没有答案