真的,这里真是个愚蠢的问题。我是Java(和OOP)的新手,来自Javascript(实际上是Extendscript)背景。我在这里有一个JFrame:
package info.chrismcgee.sky.production;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import net.miginfocom.swing.MigLayout;
import org.jdesktop.swingx.JXTreeTable;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
public class ProductionWindow extends JFrame {
/**
*
*/
private static final long serialVersionUID = -1899673458785493250L;
private JPanel contentPane;
private JTextField textField;
private JLabel lblTodaysDate;
private JXTreeTable treeTable;
/**
* Create the frame.
*/
public ProductionWindow() {
setMinimumSize(new Dimension(450, 300));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[][grow,center][]", "[][grow][]"));
JButton btnPrev = new JButton("<- PREV");
contentPane.add(btnPrev, "cell 0 0,alignx left");
lblTodaysDate = new JLabel("Today's Date");
lblTodaysDate.setHorizontalAlignment(SwingConstants.CENTER);
lblTodaysDate.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
contentPane.add(lblTodaysDate, "cell 1 0,growx");
JButton btnNext = new JButton("NEXT ->");
contentPane.add(btnNext, "cell 2 0,alignx right");
treeTable = new JXTreeTable();
treeTable.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
contentPane.add(treeTable, "cell 0 1 3 1,grow");
JLabel lblTotal = new JLabel("Total:");
lblTotal.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
contentPane.add(lblTotal, "cell 0 2");
textField = new JTextField();
textField.setEditable(false);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
textField.setText("1,000");
contentPane.add(textField, "cell 1 2 2 1,growx");
textField.setColumns(10);
}
public JPanel getContentPane() {
return contentPane;
}
public void setContentPane(JPanel contentPane) {
this.contentPane = contentPane;
}
public JTextField getTextField() {
return textField;
}
public void setTextField(JTextField textField) {
this.textField = textField;
}
public JLabel getLblTodaysDate() {
return lblTodaysDate;
}
public void setLblTodaysDate(String today) {
this.lblTodaysDate.setText(today);
}
public JXTreeTable getTreeTable() {
return treeTable;
}
}
然后我从Main
类调用此代码:
package info.chrismcgee.sky.production;
import info.chrismcgee.sky.production.tables.JobManager;
import info.chrismcgee.sky.production.tables.ShowJobs;
import info.chrismcgee.util.InputHelper;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.apache.commons.io.FileUtils;
import org.joda.time.LocalDate;
public class Main {
// This enum will call a stored procedure which returns all of a certain day's jobs.
public static final String SQL_JOBS_BY_DATE = "{CALL GetJobsWithCountByDate(?, ?)}";
private static Connection conn = ConnectionManager.getInstance().getConnection();
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ProductionWindow frame = new ProductionWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
ConnectionManager.getInstance().setDBType(DBType.MYSQL);
JobManager.displayAllRows();
LocalDate searchDate = new LocalDate(2014, 01, 02);
Date sqlDate = Date.valueOf(searchDate.toString());
ResultSet rs = null;
try (
// Create a statement object. (Defines how the result set is handled.)
CallableStatement stmt = conn.prepareCall(
SQL_JOBS_BY_DATE,
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
) {
// Create the result set for today.
stmt.setDate(1, sqlDate);
stmt.registerOutParameter("total", Types.INTEGER);
rs = stmt.executeQuery();
int nRows = stmt.getInt("total");
ShowJobs.displayData(rs, nRows);
ShowJobs.getTodaysJobs(rs, nRows, ProductionWindow, textField);
} catch (SQLException e) {
// In case there is some error with the database.
ConnectionManager.processException(e);
} finally {
rs.close();
}
ConnectionManager.getInstance().close();
}
}
这仍在开发中;还没有测试过。我的问题是从Main类访问ProductionWindow的方法。当我在Main的末尾附近的getTreeTable()
行中输入ProductionWindow.
后按[CTRL] - [SPACE]时,Eclipse不允许我选择ShowJobs.getTodaysJobs
方法。
我知道这有一个明显的答案,一个很好的理由等,以及让它在Java中更“合适”的解决方案。我只是不知道这些,因为我还是Java和OOP的新手。
答案 0 :(得分:2)
根据这个问题以及其他一些人的判断,你似乎对这些窗口有一个特定的想法,它们必须非常简单,没有代码,然后由其他类进行外部操作。这不是Swing(甚至是Java)的意思。
将JFrame和JDialog视为您家中的房间。你不会用所有合适的餐具安装你的厨房,然后进入餐厅,尝试远程操作炉灶!
如果一个窗口的工作是填充屏幕中的表,更新表并处理与表相关的用户输入,那么所有代码应该在窗口& #39; s类(无论是JFrame还是JDialog)。
考虑焦点。如果用户的焦点和输入位于窗口内,则处理该窗口的代码也应位于该窗口内。
这是最基本的OOP原则之一:Encapsulation
答案 1 :(得分:1)
问题是getTreeTable()不是静态方法,因此不能通过类名(ProductionWindow.getTreeTable())访问。你需要一个对象的句柄才能做到这一点。
在run()中,当您声明一个新的“frame”时,将其存储在Main类的class level属性中。然后使用“frame.getTreeTable()”。为了演示,写下“框架”。在声明“frame”之后,然后按ctrl + space。你会在那里得到很多方法。