我这里有3个java文件,一个写有输入框面的panel,一个是嵌套此panel的jframe,一个是需要根据是否有异常来循环调用并jframe的主函数,现在问题是,我在jframe当中设置的响应在调用中不起反映,主函数只将jframe中的构造函数走了一遍,我需要的效果是只有在jframe中响应之后才主函数才进行下一步操作,请高人帮我看看应该怎么修改,这个逻辑上应该是一个怎样的结构呢?
jframe
public class jframe extends JFrame implements ActionListener
{
JButton bu;
Main fu;
RowInputPanel pu;
jframe(Main f,RowInputPanel p)
{
fu = f;
pu=p;
bu = new JButton("ok");
bu.addActionListener(this);

this.setLayout(new BorderLayout());
this.add(pu,"Center");
this.add(bu,"South");
this.setVisible(true);
this.setSize(300,300);
}


public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bu)
{
try{
fu.addpa(pu.getData());
}catch(NumberFormatException error)
{
JOptionPane.showMessageDialog(this, "输入的应当是数字", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
}RowInputPanel
public class RowInputPanel extends JPanel 
{
JPanel jp[],panel;
JLabel jl[];
protected JNumberField jnf[];
double elem[];
int h,lie,p,cj,type;
boolean isaction;
String precon[];

RowInputPanel(int a,int b,int c)//a是第几行b有多少列c精确位
{
h=a+1;
lie=b;
p=c;
jp = new JPanel[lie];
jl = new JLabel[lie];
jnf = new JNumberField[lie];
panel = new JPanel();
elem = new double[lie];
precon = new String[lie];

panel.setLayout(new GridLayout(1,lie));

for(int i=0;i<lie;i++)
{
jp[i]=new JPanel();
jl[i]=new JLabel("A["+h+"]["+(i+1)+"]=");
jnf[i]=new JNumberField();
jnf[i].setColumns(p+1);
jp[i].add(jl[i]);
jp[i].add(jnf[i]);
panel.add(jp[i]);
}

this.setLayout(new BorderLayout());
this.add(new JLabel("您正在进行第  "+h+"行  的输入"),"North");
this.add(panel,"Center");
//this.setSize(lie*p*28,120);
        this.setVisible(true);
}
public Object[] getData() throws NumberFormatException
{
Object data[] = new Object[lie];
for(int i=0;i<lie;i++)
{
data[i]=new Double(jnf[i].getDouble());
}
return data;
}

public void setData(String[] data)
{
for(int i=0;i<lie;i++)
{
jnf[i].setText(data[i]);
}
}
}JNumberField
public class JNumberField extends JTextField
{    public JNumberField()
    {
     addKeyListener( new KeyAdapter() {
            //当回车时候实现tab功能
         public void keyPressed(KeyEvent evt){
             int key = evt.getKeyCode();
             if (key == KeyEvent.VK_ENTER)
             transferFocus();  
          }
      });
    }    public int getInteger()
        throws NumberFormatException
    {
        return Integer.valueOf(getText()).intValue();
    }    public double getDouble()
        throws NumberFormatException
    {
        return Double.valueOf(getText()).doubleValue();
    }
    
}Main
public class Main extends JFrame 
{ JNumberField pa[];
boolean c; Main()
{
super("!!");
    pa = new JNumberField[4];
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(1,4));     
    for(int i=0;i<4;i++)
    {
     pa[i]=new JNumberField();
     p.add(pa[i]);
    }
    this.add(p);
    this.setVisible(true);
    this.setSize(300,300);
    
    for(int i=0;i<4;i++)
    {
     //do{
     RowInputPanel my = new RowInputPanel(i,4,5);
     jframe k=new jframe(this,my);
     //}while(c==false);
    }
}


public void addpa(Object a[])
{
for(int i=0;i<4;i++)
{
pa[i].setText(a[i].toString());
}
}


public static void main(String[] args) 
{
// TODO Auto-generated method stub
new Main();
}}

解决方案 »

  1.   

    你把按钮放在panel中试一试。
      

  2.   

    很乱很乱,不知道你要实现什么功能,我自己猜测一下,看看行不。
    你要怎么产生输入窗口由main控制比较好,什么功能由什么类拥有,应该先想好。
    简单改了下你的Main.java,数据保存我没考虑,不知道你要干什么,
    你起的标识符起得够稀奇古怪的,呵。//接口
    public class Main extends JFrame  implements ActionListener
    {  JNumberField pa[]; 
    boolean c; 
    JButton input;
    JButton reset;
    //控制产生哪个输入窗口,最多产生4个。
    static int j = 0;
    Main() 

    super("显示数据"); 
        pa = new JNumberField[4]; 
        JPanel p = new JPanel(); 
    //添加放按钮的面板
        JPanel p2 = new JPanel();
        input = new JButton("输入数据");
        reset = new JButton("重新输入");
        input.addActionListener(this);
        reset.addActionListener(this);
        p2.add(input);
        p2.add(reset);
        p.setLayout(new GridLayout(1,4)); 
        for(int i=0;i <4;i++) 
        { 
         pa[i]=new JNumberField(); 
         p.add(pa[i]); 
        } 
        this.add(p,"Center"); 
        this.add(p2,"South");
        this.setVisible(true); 
        this.setSize(200,100); 
        this.setLocation(300,300);


    public void addpa(Object a[]) 

    for(int i=0;i <4;i++) 

     pa[i].setText(a[i].toString()); 


    //产生输入窗口由input按钮控制。
    public void actionPerformed(ActionEvent a){
    if (a.getSource() == input){
    //System.out.println(j);
    if(j<4){
          RowInputPanel my = new RowInputPanel(j,4,5); 
          jframe k=new jframe(this,my); 
            j++; 
          }
    }
    if(a.getSource() == reset){
    //System.out.println(j);
           j = 0;
          }
    }
    public static void main(String[] args)  

    new Main(); 
    } } 
      

  3.   

    这么说吧,有没有人能把JOptionPane.showOptionDialog中默认按回车就是点确定更改为只有鼠标才能响应,回车不响应此事件
      

  4.   

    在键盘事件中判断输入的是不是回车。如果是就不响应。
    再用MouseListener进行监听。
      

  5.   

     if(j<4){
                 RowInputPanel my = new RowInputPanel(j,4,5); 
                 jframe k=new jframe(this,my); 
                j++; 
              }
    在键盘事件中判断输入的是不是回车。如果是就不响应。 
    再用MouseListener进行监听。
      

  6.   

    谢谢楼上的答复,我这个程序从逻辑上就不对,所以我放弃了,我想知道的是把JOptionPane.showOptionDialog中默认按回车就是点确定更改为只有鼠标才能响应,回车不响应此事件
    我也明白在键盘事件中判断输入的是不是回车。如果是就不响应。  
    再用MouseListener进行监听。
    但是具体代码应该怎么写,我不是很会操作这个监听,而且这个控制应该写在什么位置呢?我的主函数中不能写事件响应,最多也就是  写JOptionPane中的判断,只能使用这种,不能写其他任何响应
      

  7.   

    其实再说抽象一些就是
    如果我有a文件和b文件,a文件中有响应事件的监听器,我如何在b文件中知道a文件中的事件是否触发