gpt4 book ai didi

java - 从外部访问 JFrame 内容

转载 作者:行者123 更新时间:2023-12-01 23:16:23 26 4
gpt4 key购买 nike

这是一个非常非常愚蠢的问题。我是 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 的方法。当我在 ShowJobs.getTodaysJobs 中输入 ProductionWindow. 后按 [CTRL]-[SPACE] 时,Eclipse 不允许我选择 getTreeTable() 方法code> Main 末尾附近的行。

我知道对此有一个明显的答案,一个很好的理由等等,以及使其在 Java 中更“正确”工作的解决方案。我只是不知道这些,因为我对 Java 和 OOP 还很陌生。

最佳答案

根据这个问题和其他一些问题,您似乎对这些窗口有一个具体的想法,这些窗口必须非常简单,其中没有代码,然后由其他类在外部进行操作。这不是 Swing(甚至 Java)的目的。

将 JFrame 和 JDialog 视为您家中的房间。您不会为厨房配备所有合适的用具,然后进入餐厅并尝试远程操作炉子!

如果窗口的工作是在屏幕中填充表格、更新表格并处理与表格相关的用户输入,那么所有这些代码都应该位于窗口类的内部(无论是JFrame 或 JDialog)。

考虑焦点。如果用户的焦点和输入位于窗口内,则处理该问题的代码也应该位于该窗口内。

这是最基本的 OOP 原则之一:Encapsulation

关于java - 从外部访问 JFrame 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21145911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com