我正在编写一个Java程序,该程序显示一个组合框,该组合框从属性文件中获取信息.有一个设置类,该类将允许用户更新组合框中的字段名称.我的问题是调用和修改设置类时,我无法弄清楚如何使用新设置更新组合框.我有一种方法可以重新粉刷整个面板并重新加载组合框.但是我不知道在设置类中按下“应用”按钮时如何激活该方法.
这是我要完成的工作的粗略示例.
主类:
package testing;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Properties;
import javax.swing.*;
public class testConfigLoad extends JFrame
{
JButton apply = new JButton("Apply");
JButton set = new JButton("Settings");
Properties config = new Properties();
FileInputStream fis = null;
FileOutputStream fos = null;
final String configFile = "config.properties";
OptPanel opt;
public testConfigLoad() throws IOException
{
super("Test Program");
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
config();
opt = new OptPanel(config);
buildFrame();
}
public void buildFrame()
{
set.addActionListener(new setListener());
add(opt);
add(apply);
add(set);
setVisible(true);
}
public void config() throws IOException
{
try
{
fis = new FileInputStream(configFile);
config.load(fis);
}
catch (FileNotFoundException e)
{
System.out.println("File not found");
}
finally
{
if (fis != null)
{
fis.close();
}
}
}
private class setListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
settings set = new settings(config);
}
}
public static void main(String[] args) throws IOException
{
new testConfigLoad();
}
}
需要刷新的面板:
package testing;
import java.util.Properties;
import javax.swing.*;
public class OptPanel extends JPanel
{
String[] opts;
JLabel optLabel = new JLabel("Available Options");
Properties config;
public OptPanel(Properties p)
{
config = p;
opts = new String[3];
buildPanel();
}
public void buildPanel()
{
for (int i = 0; i < opts.length; i++)
{
opts[i] = config.getProperty("option." + i + ".name");
}
JComboBox optBox = new JComboBox(opts);
add(optLabel);
add(optBox);
}
public void refPanel()
{
removeAll();
this.buildPanel();
((JPanel) this).revalidate();
repaint();
}
}
和设置类:
package testing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.*;
public class settings
{
Properties config;
final String configFile = "config.properties";
JFrame setFrame = new JFrame("Settings");
JLabel opt1 = new JLabel("Option 1");
JLabel opt2 = new JLabel("Option 2");
JLabel opt3 = new JLabel("Option 3");
JTextField text1 = new JTextField(15);
JTextField text2 = new JTextField(15);
JTextField text3 = new JTextField(15);
JButton apply = new JButton("Apply");
public settings(Properties p)
{
config = p;
setFrame.setSize(275, 200);
setFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setFrame.setLayout(new FlowLayout());
buildSetFrame();
}
public void buildSetFrame()
{
text1.setText(config.getProperty("option.0.name"));
text2.setText(config.getProperty("option.1.name"));
text3.setText(config.getProperty("option.2.name"));
apply.addActionListener(new applyListener());
setFrame.add(opt1);
setFrame.add(text1);
setFrame.add(opt2);
setFrame.add(text2);
setFrame.add(opt3);
setFrame.add(text3);
setFrame.add(apply);
setFrame.setVisible(true);
}
private class applyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
config.setProperty("option.0.name", text1.getText());
config.setProperty("option.1.name", text2.getText());
config.setProperty("option.2.name", text3.getText());
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(configFile);
config.store(fos, null);
}
catch (IOException f)
{
System.out.println("Error");
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (IOException g)
{
System.out.println("Problem");
}
}
}
setFrame.setVisible(false);
// This is where I need to pass something back to the
// testConfigLoad class to tell it to
// run the refPanel method in the OptPanel class.
}
}
}
该配置文件名为config.properties,如下所示:
option.2.name=two
option.1.name=one
option.0.name=zero
解决方法:
一种方法是在OptPanel和settings类之间使用回调.您可以提取功能以将元素添加到属性JComboBox到其自己的方法中:
public void updateProperties(Properties p) {
model.removeAllElements();
for (String s: p.stringPropertyNames()) {
model.addElement(p.getProperty(s));
}
}
其中model是DefaultComboBoxModel.那你可以简单地打电话
optPanel.updateProperties(config);
成功存储属性后.
一些要点:
>首选方法是不使用多个JFrame.一种选择是将单个JFrame与模式对话框一起使用.看到这个discussion.
>不要扩展JFrame,而是直接使用实例.
> Java Naming conventions指示类名以大写字母开头,因此设置将变为“设置”
标签:actionlistener,swing,java 来源: https://codeday.me/bug/20191031/1974961.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。