import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
class TestFrame implements ActionListener
{
JButton savebutton = null;
JRadioButton dnYesButton = null;
JRadioButton dnNoBUtton = null;
private PlmnSwitchFileManager plmnSwitchFileManager = PlmnSwitchFileManager.getInstance();
private void init()
{
JFrame jf = new JFrame("dn parameter config");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dnYesButton = new JRadioButton("yes");
dnNoBUtton = new JRadioButton("no");
dnYesButton.setActionCommand("JRadioButtonStatus");
dnYesButton.setMnemonic(KeyEvent.VK_A);
dnNoBUtton.setActionCommand("JRadioButtonStatus");
dnNoBUtton.setMnemonic(KeyEvent.VK_B);
dnYesButton.addActionListener(this);
dnNoBUtton.addActionListener(this);
ButtonGroup bg = new ButtonGroup();
bg.add(dnYesButton);
bg.add(dnNoBUtton);
initDnButtonSelectStatus();
savebutton = new JButton("save");
savebutton.addActionListener(this);
savebutton.setEnabled(false);
JLabel jLabel = new JLabel("hahahha");
jLabel.setFont(new Font("", Font.PLAIN, 25));
JPanel pan1 = new JPanel();
pan1.add(savebutton);
pan1.add(dnYesButton);
pan1.add(dnNoBUtton);
pan1.add(jLabel);
pan1.setVisible(true);
pan1.setSize(300, 300);
jf.getContentPane().add(pan1);
jf.setVisible(true);
jf.setSize(500, 500);
jf.setResizable(true);
}
private void initDnButtonSelectStatus()
{
dnNoBUtton.setSelected(!plmnSwitchFileManager.getStatus());
dnYesButton.setSelected(plmnSwitchFileManager.getStatus());
}
public static void main(String args[])
{
TestFrame testFrame = new TestFrame();
testFrame.init();
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == savebutton)
{
processFiles();
JOptionPane.showMessageDialog(null, "保存成功");
savebutton.setEnabled(false);
}
if (e.getActionCommand().equals("JRadioButtonStatus"))
{
processSaveButtonEnabledStatus();
}
}
private void processSaveButtonEnabledStatus()
{
if (isButtonAndFileStatusConsistent())
{
savebutton.setEnabled(false);
}
else
savebutton.setEnabled(true);
}
private void processFiles()
{
if (dnYesButton.isSelected() != plmnSwitchFileManager.getStatus())
{
processDnContainPlmn();
}
}
private boolean isButtonAndFileStatusConsistent()
{
// TODO 返回条件或上另外一个不一致的状态
return dnYesButton.isSelected() == plmnSwitchFileManager.getStatus();
}
private void processDnContainPlmn()
{
plmnSwitchFileManager.processDnContainPlmnFile(dnYesButton.isSelected());
}
}
文件解析类
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PlmnSwitchFileManager
{
private static PlmnSwitchFileManager plmnSwitchFileManager = new PlmnSwitchFileManager();
// 开关状态,和配置文件保持一致
private boolean status = false;
private String filePath = System.getProperty("user.dir") + File.separator + "conf" + File.separator
+ "a.properties";
public static PlmnSwitchFileManager getInstance()
{
return plmnSwitchFileManager;
}
private PlmnSwitchFileManager()
{
try
{
status = getStatusFromFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean getStatus()
{
return status;
}
private void setStatus(Boolean content)
{
status = content;
}
public void writeStatusToFile(Boolean content)
{
Properties prop = new Properties();
try
{
FileOutputStream oFile = new FileOutputStream(filePath);
prop.setProperty("switch", String.valueOf(content));
prop.store(oFile, "");
oFile.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private boolean getStatusFromFile() throws IOException
{
String value = "";
Properties prop = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
prop.load(in);
value = prop.getProperty("switch");
in.close();
return Boolean.valueOf(value);
}
public void processDnContainPlmnFile(boolean newStatus)
{
setStatus(newStatus);
writeStatusToFile(newStatus);
synFileToSlave();
}
private void synFileToSlave()
{
File fine = new File("");
}
}
浙公网安备 33010602011771号