如何将 main Java class 变成构造函数?

How to turn a main Java class into a constructor?

我很难理解如何将应用程序转换为可调用 class,反之亦然 Java。比方说我写了一个 tabe 可以作为 main class 正常工作。它生成带有面板的框架 (window),这是一个 table。如果我错了请纠正我。

    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;

    public class DisplayPanel {
      public static void main(String args[]) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
            { "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
        Object columnNames[] = { "Column One", "Column Two", "Column Three" };
        JTable table = new JTable(rowData, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setSize(300, 150);
        frame.setVisible(true);
      }
    }

我有一个电话 JPanel p = new DisplayPanel();

那应该只是 return 一个面板,由上面的代码生成。那么,我如何更改代码以具有可调用的 DisplayPanel() class return 是一个 Jpanel?这个转换的名称是什么(这样我就可以google了)?


让我改一下问题。我有两个独立的项目:一个沙盒(上面显示了 main 并按预期工作)和一个应该调用 class(returning 函数)并将其显示在 largeProject 自己的框架中的 largeProject。我的大型项目有:

 JPanel p1 = new DisplayPanel1();
 add(p1, BorderLayout.NORTH);

我需要把它变成

 JPanel p1 = new DisplayPanel1(); //this one is implemented, but the panel is very diffeernt, hence not reusable
 JPanel p2 = new DisplayPanel2();
 add(p1, BorderLayout.NORTH);
 add(p2, BorderLayout.SOUTH); 

所以,打开DisplayPanel2();进入一个框架并没有真正的帮助。此外,仅帮助解决这个特定问题不如解释一般原则有用。这种转换背后的逻辑是什么。

感谢您指出 DisplayPanel 和 JPanel 之间的区别。

与其创建 JFrame 并使其可见,您的 DisplayPanel 应该 成为 JFrame.

进行此转换的一种方法是使您的 class 扩展 JFrame,并将您的代码放入构造函数中,使用 this 而不是新的 JFrame :

public class DisplayPanel extends JFrame {

  public DisplayPanel() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
        { "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
    Object columnNames[] = { "Column One", "Column Two", "Column Three" };

    JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);
    this.add(scrollPane, BorderLayout.CENTER);
    this.setSize(300, 150);
    // this.setVisible(true); this line should probably be called by the caller class now
  }
}

请注意,您还可以将列和行作为构造函数参数传递:

public class DisplayPanel extends JFrame {

  public DisplayPanel(Object[][] rowData, Object[] columnNames) {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);
    this.add(scrollPane, BorderLayout.CENTER);
    this.setSize(300, 150);
    // this.setVisible(true); this line should probably be called by the caller class now
  }
}

I have a call JPanel p = new DisplayPanel(); That should simply return a panel, generated from the code above

实际上,不,它不是 return 面板,它试图 创建 DisplayPanel 的实例 作为 JPanel ,但它不能,因为 DisplayPanel 不是 JPanel

how can I change the code to have a callable DisplayPanel() class that returns a Jpanel?

你可以把你的 DisplayPanel 变成 JPanel 这样的东西。

public class DisplayPanel extends JPanel { // <-- Note the 'extends'
    public DisplayPanel() {
        Object rowData[][] = { 
              { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
              { "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
        Object columnNames[] = { "Column One", "Column Two", "Column Three" };
        JTable table = new JTable(rowData, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane, BorderLayout.CENTER);
        // might have to set size on this...
    }
}

那么您创建框架的 Main class 可能看起来像这样。

public class Main {

  public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
          public void run() {
              createAndShowGui();
          }
       });
  }

  public static void createAndShowGui() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DisplayPanel p = new DisplayPanel(); <-- Use the "non-main" class here
    frame.add(p);
    // frame.setSize(300, 150); 
    frame.pack();
    frame.setVisible(true);
  }
}