现在小弟要实现一个功能:点击一个菜单项,读取config.prop文件,弹出一个设置参数的对话框,用户可以修改里面的内容,点击保存按钮可以写到文件中。大约有7、8个参数项,有一个要密码框,有一个下拉选择框,有一个是多行的文本编辑框,其余是单行的文本框,布局只要看的顺眼就行。
    哪位高手能够给出一个例子代码,小弟不胜感激,虽然分有点少,但是你可以到我另一篇提问 http://topic.csdn.net/u/20071011/16/9ec8ab16-a56b-4be5-84aa-0609ce179075.html 中回复一下,我那里有100分,没有什么满意的答案,分还没散,我会在那里再多加点分

解决方案 »

  1.   

    java里读写properties的方法都是现成的。现在需要做的就是画几个文本框,对其Value的表示和保存。
    直接google吧,很容易找的。
      

  2.   

    随手写了写,没用你说的那些密码框之类
    用了个table实现的,你可以自己改一下
    package proper;import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Vector;
    import java.util.Map.Entry;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;/**
     * 
     * Test Class
     * 
     * author:zht
     * mail:[email protected]
     * 2007-10-31
     */
    public class Test extends JPanel implements ActionListener { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Test test = new Test();
    JFrame jf = new JFrame();
    jf.setContentPane(test);
    jf.pack();
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);
    } JTable jt; JButton modify_JBn; Properties props = null; public Test() {
    try {
    props = loadPro();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    this.setLayout(new BorderLayout());
    String[] tn = { "名称", " 值", };
    String[][] data = {};
    DefaultTableModel defaultModel = new DefaultTableModel(data, tn) {
    @Override
    public boolean isCellEditable(int row, int column) {
    if (column == 0) {
    return false;
    } else
    return true;
    }
    };
    jt = new JTable(defaultModel);
    this.add(new JScrollPane(jt));
    {
    Iterator itor = props.entrySet().iterator();
    while (itor.hasNext()) {
    Map.Entry vEntry = (Entry) itor.next();
    Vector<Object> vv = new Vector<Object>();
    vv.add(vEntry.getKey());
    vv.add(vEntry.getValue());
    System.out.println(vEntry.getKey());
    defaultModel.addRow(vv);
    }
    }
    modify_JBn = new JButton("modify");
    modify_JBn.addActionListener(this);
    JPanel jp = new JPanel();
    jp.setLayout(new FlowLayout());
    jp.add(modify_JBn);
    this.add("South", jp);
    } public Properties loadPro() throws FileNotFoundException, IOException {
    Properties props = new Properties();
    props.load(new FileInputStream(new File("config/config.properties")));
    return props;
    } @Override
    public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < jt.getRowCount(); i++) {
    String name = jt.getValueAt(i, 0).toString();
    String value = jt.getValueAt(i, 1).toString();
    props.setProperty(name, value);
    System.out.println("==");
    }
    FileOutputStream fos;
    try {
    fos = new FileOutputStream(new File("config/config.properties"));
    props.store(fos, "");
    } catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }
    }
      

  3.   

    不是我想要的,用JTable干嘛,JFrame也不需要吧,只要写一个菜单项的监听器就行了,弹出对话框,修改配置,保存
      

  4.   

    不是拉,是你写的复杂了点,其实很简单,下面是我的部分程序,我总觉得程序太繁琐了,不知道有什么更好的办法miSetPara.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            Properties prop = new Properties();
            try
            {
                prop.load(new FileInputStream(new File(SalaryMail.PROPFILENAME))); 
                
                JLabel lSmtpServerIP = new JLabel("smtp服务器的IP或者域名");
                JLabel lSmtpServerPort = new JLabel("smtp服务器的邮件发送端口号");
                JLabel lUserID = new JLabel("发送邮件的用户id(同发信人邮件地址)");
                JLabel lPassword = new JLabel("发送邮件的用户口令");
                JLabel lNeedAuth = new JLabel("邮件服务器是否需要用户名口令验证");
                JLabel lFromAddress = new JLabel("发信人邮件地址");
                JLabel lMailSubject = new JLabel("邮件标题");
                JLabel lMailHead = new JLabel("通知内容头");
                JLabel lMailSignature = new JLabel("邮件署名");
                
                JTextField tfSmtpServerIP = new JTextField(sm.encString(prop.get("smtpserverip").toString()),10);
                JTextField tfSmtpServerPort = new JTextField(sm.encString(prop.get("smtpserverport").toString()),10);
                JTextField tfUserID = new JTextField(sm.encString(prop.get("userid").toString()),15);
                JPasswordField tfPassword = new JPasswordField(sm.encString(prop.get("password").toString()),10);
                Choice cNeedAuth = new Choice();
                cNeedAuth.add("是");
                cNeedAuth.add("否");
                JTextField tfFromAddress = new JTextField(sm.encString(prop.get("fromaddress").toString()),15);
                JTextField tfMailSubject = new JTextField(sm.encString(prop.get("mailsubject").toString()),10);
                JTextField tfMailHead = new JTextField(sm.encString(prop.get("mailhead").toString()),25);
                JTextArea taMailSignature = new JTextArea(sm.encString(prop.get("mailsignature").toString()));
                JScrollPane sp = new JScrollPane(taMailSignature);
                sp.setPreferredSize(new Dimension(260,200));
                
                JButton bOK = new JButton("确定");
                
                JDialog dlg = new JDialog(f,"设置参数",true);
                
                dlg.setLayout(new FlowLayout());
                dlg.add(lSmtpServerIP);
                dlg.add(tfSmtpServerIP);
                dlg.add(lSmtpServerPort);
                dlg.add(tfSmtpServerPort);
                dlg.add(lUserID);
                dlg.add(tfUserID);
                dlg.add(lPassword);
                dlg.add(tfPassword);
                dlg.add(lNeedAuth);
                dlg.add(cNeedAuth);
                dlg.add(lFromAddress);
                dlg.add(tfFromAddress);
                dlg.add(lMailSubject);
                dlg.add(tfMailSubject);
                dlg.add(lMailHead);
                dlg.add(tfMailHead);
                dlg.add(lMailSignature);
                dlg.add(sp);
                dlg.add(bOK);
                
                dlg.setBounds(x,y,w,h);
                dlg.setVisible(true);
                dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });另外就是我的布局有点问题,应该用那种布局呢
      

  5.   

     GridLayout(int rows,int cols)
      

  6.   

    GridLayout(int   rows,int   cols)效果不好,
    还有其他适合的布局方法吗
    我记得有个SpringLayout的布局,大家用过吗,适合吗?
      

  7.   

    GridLayout搞出来的效果每个格子都一样大,有没有其他的布局方式呢?