在java中从不同的类获取数据而不重新打开JFrame?
我的课程中有两门课给我带来了麻烦。第一个打开一个JFrame。第二个更新.properties文件上的数据。在JFrame中有一个带有JTextAreas的面板和一个按钮“保存更改”。当按下该按钮时,我从第二个类调用一个方法,但要做到这一点,我必须在java中从不同的类获取数据而不重新打开JFrame?,java,swing,properties,jframe,instance,Java,Swing,Properties,Jframe,Instance,我的课程中有两门课给我带来了麻烦。第一个打开一个JFrame。第二个更新.properties文件上的数据。在JFrame中有一个带有JTextAreas的面板和一个按钮“保存更改”。当按下该按钮时,我从第二个类调用一个方法,但要做到这一点,我必须 firstClass x=newfirstclass() 对话框应该从用户那里收集信息,当它关闭时,根据它是如何关闭的,使用调用者来处理和处理结果 SecondClass secondClass = new SecondClass(); int re
firstClass x=newfirstclass()因此,当按下按钮时,文件将更新,但会打开一个新的JFrame。我很确定创建实例x
是造成这种情况的原因,但我不知道不这样做还有什么其他方法可以做到这一点
第1类:
public class firstClass{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new firstClass();
}
});
}
JFrame go = new JFrame("firstClass");
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea();
JButton savechanges = new JButton("Save");
public firstClass() {
panel.add(textArea);
panel.add(savechanges);
savechanges.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0){
secondClass f = new secondClass();
try {
f.UpdateData();
} catch (IOException e) {
e.printStackTrace();
}
}
});
go.add(panel);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(750, 750);
go.setVisible(true);
}
第2类:
public class secondClass {
public static void main(String[] args) {
//creates properties file
}
public void UpdateData() throws IOException{
firstClass x = new firstClass(); // <-------------------------------
FileInputStream in = new FileInputStream("config.properties");
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream("config.properties");
props.setProperty("prop1", x.textArea.getText().toString());
props.store(out, null);
out.close();
}
公共类第二类{
公共静态void main(字符串[]args){
//创建属性文件
}
public void UpdateData()引发IOException{
firstClass x=new firstClass();//您正在跨越目标,谁在这里实际控制着,谁负责什么
将责任范围分开,例如,您的SecondClass
只(实际上)负责从用户收集数据,因为您的FirstClass
实际上是知道应该使用哪些数据的人,所以SecondClass
不应该关心
这样想吧,二等舱
是服务员,它接受订单并将信息提供给一等舱
,谁是厨师,谁知道如何完成订单和准备饭菜
<>而不是使用单独的帧(由于一些不必要的原因),请考虑使用某个变量的模态<代码> jCalue<代码>。< /P>
对话框应该从用户那里收集信息,当它关闭时,根据它是如何关闭的,使用调用者来处理和处理结果
SecondClass secondClass = new SecondClass();
int reason = secondClass.showDialog();
if (reason == SAVE_RESULTS) {
//... Get the data from the secondClass and save it...
}
有关更多详细信息,请参阅
已更新
好的,那么您似乎想要的是某种能够存储/检索值的“配置”管理器
配置管理器将知道如何读取和写入属性,而不关心其他任何事情,它有一个明确定义的边界。然后,您需要要求配置管理器根据需要读取或写入值
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TestConfig {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestConfig();
}
});
}
JFrame go = new JFrame("firstClass");
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea();
JButton savechanges = new JButton("Save");
public TestConfig() {
panel.add(textArea);
panel.add(savechanges);
savechanges.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
ConfigurationManager.INSTANCE.put("prop1", textArea.getText());
} catch (IOException ex) {
JOptionPane.showMessageDialog(panel, "Failed to write properties", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
go.add(panel);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(750, 750);
go.setVisible(true);
}
public enum ConfigurationManager {
INSTANCE;
protected Properties readProperties() throws IOException {
Properties p = new Properties();
try (FileInputStream fis = new FileInputStream("config.properties")) {
p.load(fis);
}
return p;
}
public String get(String name) throws IOException {
return readProperties().getProperty(name);
}
public void put(String key, String vaue) throws IOException {
Properties p = readProperties();
p.put(key, vaue);
writeProperties(p);
}
protected void writeProperties(Properties p) throws IOException {
try (FileOutputStream fos = new FileOutputStream("config.properties")) {
p.store(fos, "Config");
}
}
}
}
这个示例非常繁重…每次读取值时,它都会从磁盘加载内容,每次设置值时,它都会存储到磁盘
您可以将值缓存在内存中,在首次加载时读取它们一次,并在将来某个时间写入,但我希望您仍然能理解这一点我明白您的意思,我只是不确定我是如何交叉使用的。类1只有一个JFrame和几个面板(使用cardLayout,因此一次只显示一个面板)所以我不认为我使用的是单独的框架。正如你所说,类1是厨师,类2是服务员,只是收集信息。我希望文件中包含信息的原因是,当用户关闭应用程序并重新打开时,先前输入的数据仍然存在。我只是不希望在我保存数据。因此,将信息返回给厨师长…您甚至不需要在secondClass
中创建firstClass
的第二个实例,只需将需要保存的详细信息传递给它…厨师长将从在secondClass
中更新的config.properties文件中获取信息e用户在textArea中输入的使用该属性的详细信息:props.setProperty(“prop1”,x.textArea.getText().toString());
但是如果没有第二个first class
实例,这将不起作用需要的是某种配置管理器来管理设置的存储和检索…看起来我只是又重新考虑了一下…我所做的只是将UpdateData
方法移到firstClass
上,它工作得很好。我想这就是你试图通过交叉目的告诉我的。。