【问题标题】:How to call the run method from another class?如何从另一个类调用run方法?
【发布时间】:2012-09-15 13:46:34
【问题描述】:

对不起,如果这是一个愚蠢的问题。

我想了解如何调用位于

中的 run 方法
java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new FereastraPrincipala().setVisible(true);

来自 AdaugaComanda.java 类。

在 FereastraPrincipala.java 中声明了 run 方法,我想从 AdaugaComanda.java 调用它,以便在从 AdaugaChitanta.java 的文本字段中引入值后,可以看到 FereastraPrincipala 的变化。如果我不调用方法,那么我必须再次运行 FereastraPrincipala.java,才能在 JTabbedPane 中看到新信息。

这里是 FereastraPrincipala.java 的代码

 package sakila.ui;

 import java.util.List;
 import java.util.Vector;
 import javax.swing.table.DefaultTableModel;
 import org.hibernate.Session;
 import sakila.entity.*;
 import sakila.util.HibernateUtil;

public class FereastraPrincipala extends javax.swing.JFrame {

public FereastraPrincipala() {
    initComponents();
}

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    AdaugaComanda ac = new AdaugaComanda();
    ac.setVisible(true);
}                                          

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new FereastraPrincipala().setVisible(true);
            Session session = HibernateUtil.getSessionFactory().openSession();

    try{

    List<Comanda> comenzi = session.createQuery("from Comanda").list();

    Vector<String> tableHeaders = new Vector<String>();
    Vector tableData = new Vector();
    tableHeaders.add("IdComanda");
    tableHeaders.add("Depozit");
    tableHeaders.add("Furnizor");
    tableHeaders.add("Client");
    tableHeaders.add("Produs");
    tableHeaders.add("Cantitate");
    tableHeaders.add("Unit de mas");

    for (Comanda comanda : comenzi) {
        Vector <Object> oneRow = new Vector <Object>();
        oneRow.add(comanda.getIdcomanda());
        oneRow.add(comanda.getDepozit() == null ? "" : comanda.getDepozit().toString());
        oneRow.add(comanda.getFurnizor() == null ? "" : comanda.getFurnizor().toString());
        oneRow.add(comanda.getClient() == null ? "" : comanda.getClient().toString());
        oneRow.add(comanda.getProdus() == null ? "" : comanda.getProdus().toString());
        oneRow.add(comanda.getCantitate());
        oneRow.add(comanda.getUnitmas());

        tableData.add(oneRow);

        }
   ComandaTable.setModel(new DefaultTableModel(tableData, tableHeaders));    

       }catch (Exception he){
        he.printStackTrace();
    }
 }                              
    });
}

}

这是 AdaugaComanda.java 的代码

   package sakila.ui;

  import java.io.EOFException;
  import java.util.List;
  import sakila.entity.*;
  import sakila.service.Functie;
  import sakila.entity.Client;

 public class AdaugaComanda extends javax.swing.JDialog {

public AdaugaComanda() {
    initComponents();
    initComboBoxes();
}

   private void initComboBoxes() {

    DepozitComboBox.removeAllItems();
    FurnizorComboBox.removeAllItems();
    ClientComboBox.removeAllItems();
    ProdusComboBox.removeAllItems();

    System.out.println("sterge itemurile");

    List<Depozit> depozite = (List<Depozit>) sakila.client.Client.citeste(Functie.LISTEAZA_DEPOZITE);
    for (Depozit depozit : depozite)
        DepozitComboBox.addItem(depozit);

    List<Furnizor> furnizori = (List<Furnizor>) sakila.client.Client.citeste(Functie.LISTEAZA_FURNIZORI);
    for (Furnizor furnizor : furnizori)
        FurnizorComboBox.addItem(furnizor);

    List<Client> clienti = (List<Client>) sakila.client.Client.citeste(Functie.LISTEAZA_CLIENTI);
    for (Client client : clienti)
        ClientComboBox.addItem(client);

    List<Produs> produse = (List<Produs>) sakila.client.Client.citeste(Functie.LISTEAZA_PRODUSE);
    for (Produs produs : produse)
        ProdusComboBox.addItem(produs);
    System.out.println("adaugaitemuri"); 

}

private void ClientComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                               
    // TODO add your handling code here:
}                                              

private void InsereazaButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    runQueryBasedOnInsert();
}                                               

 private void runQueryBasedOnInsert(){

    Comanda comanda = new Comanda();

    Depozit depozit = (Depozit)DepozitComboBox.getSelectedItem();
    comanda.setDepozit(depozit);

    Furnizor furnizor = ((Furnizor)FurnizorComboBox.getSelectedItem());
    comanda.setFurnizor(furnizor);

    sakila.entity.Client client = ((sakila.entity.Client)ClientComboBox.getSelectedItem());
    comanda.setClient(client);

    Produs produs = ((Produs)ProdusComboBox.getSelectedItem());
    comanda.setProdus(produs);

    comanda.setCantitate(Integer.parseInt(CantitateTextField.getText()));
    comanda.setUnitmas(UnitMasTextField.getText());

    sakila.client.Client.scrie(Functie.CREAZA_COMANDA, comanda);

}

 public static void main(String args[]) {

 java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new AdaugaComanda().setVisible(true);
        }
    });
}

也许有人可以帮助我。十分感谢!

【问题讨论】:

  • 看到你改变了你的问题,FereastraPrincipala 是 JFrame 吗?
  • FereastraPrincipala 是一个 JFrame,它表示主窗口,而 AdaugaChitanta 是一个 JDialog,我在其中插入值,这些值保留在数据库中。从 FereastraPrincipala.java 我可以打开 AdaugaChitanta,以便对数据库进行更改。但是我看不到 FereastraPrincipala 的 JTabbedPane 中的变化,只有当我再次运行 FereastraPrincipala.java 时,这就是为什么我想在关闭 JDialog AdaugaChitanta 后直接看到这些变化。我希望我的解释没有含糊。
  • 为了看到它们,我必须从 AdaugaChitanta 调用位于 FereastraPrincipala 中的方法(必须调用的内容在 run() 方法中),以便可以进行更改在 AdaugaChitanta.java 中插入值后看到。
  • 你看到我的更新了吗?这应该早些澄清:) 我建议你看看数据传输对象

标签: java methods call


【解决方案1】:

您可以将 FereastraPrincipala 设为 AnduagaChitanta 的成员变量。

public class  AnduagaChitanta
{
  FereastraPrincipala fPrincipala  = new FereastraPrincipala (); //Or inject it into the constructor

  private void SomeMethod()
  {
   fPrincipala.run();
  }
}

在运行方法中()

public void run()
{
  setvisible(true);
}

如果您想知道如何注入它:

公共类 AnduagaChitanta { FereastraPrincipala fPrincipala

  public AnduagaChitanta(FereastraPrincipala fPrinicipala)
  {
      this.fPrinicipala = fPrinicipala;
  }
  private void SomeMethod()
  {
   fPrincipala.run();
  }
}

如果你愿意,你可以让 FereastraPrincipala 实现一个接口,所以构造函数的定义可以是:

public AnduagaChitanta(ISomethingPrinicipala fPrinicipala)

但是现在我们将进入设计模式,所以我将保留它。

更新 在你更新之后,我会做这样的事情:

FereastraPrincipala extends JFrame implements Runnable 
{
   public void run()
   {
        setvisible(true) ;
   } 

}

我不知道在哪里,但也许在你的 AnduagaChitanta 课上我会这样做

public void SomeMethod()
{
  java.awt.EventQueue.invokeLater(fPrinicpala) 
}

我希望这是有道理的

【讨论】:

  • 我已经有一段时间没有做 Java 了。但应该或多或少是对的
  • 非常感谢您的宝贵时间。非常感谢,虽然我不知道你的解释是否符合我的代码。我是 Java 的初学者,也许这不是一个很好的问题。我重新编辑了我的问题,它提供了所有细节。如果你能弄清楚,我将不胜感激。
【解决方案2】:

永远不要调用线程的run() 方法。它在它自己的当前线程中执行!始终调用start() 方法。针对您的情况,创建一个新类,以便您可以从其他地方对其调用 start()

【讨论】:

    猜你喜欢
    • 2020-11-29
    • 2018-08-21
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 2019-08-16
    相关资源
    最近更新 更多