我想在JTree中添加一个结点,结点名由用户输入,就是由菜单中选择添加结点,然后调用一个输入名称的程序,根据输入的名称,在树中添加一个这个名的结点,现在做了很长时间,就是做不好,谁来帮帮我,给分。我现在的问题是如何得到输入的名称的问题。
newTable.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
AddTable nt = new AddTable();
if(nt.getFlag())
{
System.out.println(nt.getFlag());
String tableName = nt.getTableName();
addNode(tableName);
}
}
});
上面是菜单的事件
package datatrim;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class AddTable
{
boolean valueB = false;
String tableName;
JFrame jf;
JTextField tableput;
public AddTable()
{
jf = new JFrame("建表");
Container contentPane = jf.getContentPane();
contentPane.setLayout(new XYLayout());
JLabel table = new JLabel("请输入表名");
tableput = new JTextField();
JButton ok = new JButton("确定");
JButton no = new JButton("取消");
contentPane.add(table,new XYConstraints(10,10,80,20));
contentPane.add(tableput,new XYConstraints(90,10,100,20));
contentPane.add(ok,new XYConstraints(30,50,60,20));
contentPane.add(no,new XYConstraints(100,50,60,20));
tableput.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode()==KeyEvent.VK_ENTER)
{
ok_actionPerformed();
}
}
});
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ok_actionPerformed();
}
});
no.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jf.dispose();
}
});
jf.setSize(200,120);
jf.setVisible(true);
jf.setResizable(false);
}
public void ok_actionPerformed()
{
String tableName1 = tableput.getText();
if(tableName1.length()!=6)
{
JOptionPane.showMessageDialog(jf,"表名的格式为<年+月>!","输入失败!",0);
}
else
{
int year = Integer.parseInt(tableName1.substring(0,4));
int month = Integer.parseInt(tableName1.substring(4,tableName1.length()));
System.out.println(year+"\n"+month);
if((year<1900)||(year>2500)||(month<1)||(month>12))
{
JOptionPane.showMessageDialog(jf,"表名的格式为<年+月>!","输入失败!",0);
}
else
{
valueB = true;
tableName = tableName1;
//建表程序String sql = ""
jf.dispose();
}
}
}
protected void processWindowEvent(WindowEvent e)
{
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
jf.dispose();
}
}
public String getTableName()
{
return tableName;
}
public boolean getFlag()
{
return valueB;
}
/*public static void main(String args[])
{
AddTable at = new AddTable();
}*/
}
上面是输入名称的页面
newTable.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
AddTable nt = new AddTable();
if(nt.getFlag())
{
System.out.println(nt.getFlag());
String tableName = nt.getTableName();
addNode(tableName);
}
}
});
上面是菜单的事件
package datatrim;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class AddTable
{
boolean valueB = false;
String tableName;
JFrame jf;
JTextField tableput;
public AddTable()
{
jf = new JFrame("建表");
Container contentPane = jf.getContentPane();
contentPane.setLayout(new XYLayout());
JLabel table = new JLabel("请输入表名");
tableput = new JTextField();
JButton ok = new JButton("确定");
JButton no = new JButton("取消");
contentPane.add(table,new XYConstraints(10,10,80,20));
contentPane.add(tableput,new XYConstraints(90,10,100,20));
contentPane.add(ok,new XYConstraints(30,50,60,20));
contentPane.add(no,new XYConstraints(100,50,60,20));
tableput.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode()==KeyEvent.VK_ENTER)
{
ok_actionPerformed();
}
}
});
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ok_actionPerformed();
}
});
no.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jf.dispose();
}
});
jf.setSize(200,120);
jf.setVisible(true);
jf.setResizable(false);
}
public void ok_actionPerformed()
{
String tableName1 = tableput.getText();
if(tableName1.length()!=6)
{
JOptionPane.showMessageDialog(jf,"表名的格式为<年+月>!","输入失败!",0);
}
else
{
int year = Integer.parseInt(tableName1.substring(0,4));
int month = Integer.parseInt(tableName1.substring(4,tableName1.length()));
System.out.println(year+"\n"+month);
if((year<1900)||(year>2500)||(month<1)||(month>12))
{
JOptionPane.showMessageDialog(jf,"表名的格式为<年+月>!","输入失败!",0);
}
else
{
valueB = true;
tableName = tableName1;
//建表程序String sql = ""
jf.dispose();
}
}
}
protected void processWindowEvent(WindowEvent e)
{
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
jf.dispose();
}
}
public String getTableName()
{
return tableName;
}
public boolean getFlag()
{
return valueB;
}
/*public static void main(String args[])
{
AddTable at = new AddTable();
}*/
}
上面是输入名称的页面
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货