简体   繁体   English

运行时未显示Java Applet表

[英]Java Applet Table Not Showing When Run

I want to display a table after the user has provided input. 我想在用户提供输入后显示表格。 I can't figure out why the table won't show. 我不知道为什么表格不会显示。

I created my table as described here (Stack Overflow) and here (Rose India) in order to create a table I can add to, 我按照此处(堆栈溢出)此处(印度玫瑰)的说明创建了表格,以便创建可以添加到的表格,

but my code doesn't add the table to the applet, particularly here: 但是我的代码没有将表添加到小程序中,尤其是在这里:

aTable = new MDTable(mdList);
add( aTable );

Rest of the code, full paste here (Pastebin) : 其余代码,完整粘贴在此处(Pastebin)

public class FMCSAApplet extends Applet implements ActionListener {

String zipcode;

Label lab1;
TextField t1;
Button submitButton;
MDTable aTable;

public void init()
{
    setLayout(new FlowLayout()); 
    submitButton = new Button("Submit"); 
    lab1 = new Label("Enter a zipcode:");
    t1 = new TextField(15);

    add(lab1);
    add(t1);
    add(submitButton);
    submitButton.addActionListener(this); 
}

public void paint(Graphics g)
{
    g.drawString("Zip code: "+zipcode,30,100);
    if(zipcode!=null) {
        g.drawString("dotNum\t inputZip\t name\t\t city\t state\t mileage\t unsafe\t fatigued\t fitness\t substance\t maintenance\t inspections",30,120);
        try {
            ..Code Stripped..
                            // get info from database, add to ArayList<MData> mdList
                                    ..Code Stripped..
                                    mdList.add(motorcoach);
                }
            } // end while
            rs.close();

            aTable = new MDTable(mdList);
            add( aTable );

        }catch (Exception f) {
            System.out.println(f);
        }
    }
}

public void actionPerformed(ActionEvent evt) 
{
    // Here we will ask what component called this method
     if (evt.getSource() == submitButton) {
         zipcode = t1.getText();
         repaint();
     }
}

public boolean action(Event e,Object o)
{
    zipcode = t1.getText();
    repaint();
    return true;
}

} //end FMCSAApplet Class

class MDTable extends JPanel {
    public MDTable(ArrayList<MData> md) {
        Object[][] cellData = {
            {1,2,"3","4","5",6,7,8,9,10,11,12}     
        };

        String[] columnNames = {"dotNum","inputZip", "name","city","state","mileage","unsafe","fatigued","fitness","substance","maintenance","inspections"};
        add(  new JTable(cellData, columnNames) ) ;
        DefaultTableModel model = new DefaultTableModel(cellData,columnNames);
        JTable table = new JTable(model);
        for (MData md1 : md) {
            model.insertRow(table.getRowCount(),new Object[]{md1.getDotNum(), md1.getInputZip(), md1.getName(), md1.getCity(), md1.getState(), md1.getMileage(), md1.getUnsafe(), md1.getFatigued(), md1.getFitness(), md1.getSubstance(), md1.getMaintenance(), md1.getInspections()});
        }
    } //end MDTable Constructor
} //end MDTable Class

class MData {
        ..Code Stripped..
        public MData(int d, int i, String n, String c, String s, int m, int u, int f, int fi, int su, int ma, int in)
        ..Code Stripped..
}

You will need to validate the container after you add the MDTable component: 添加MDTable组件后,您将需要验证容器:

add( aTable );
validate();
...

Also its not a good idea to have database calls inside your paint method. paint方法内部进行数据库调用也不是一个好主意。 paint will get called multiple times when the applet regains focus or resized for instance. 例如,当小程序重新获得焦点或调整大小时, paint将被多次调用。 One suggestion would be to extract out the database call into a separate method and add a "Reload" button. 一种建议是将数据库调用提取到一个单独的方法中,并添加一个“重新加载”按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM