Java 将try-catch包含在公共方法中,并在需要时调用
如何将try-catch封装在公共方法中,并在需要时调用 我已尝试捕获一个屏幕代码。我想从另一个屏幕调用它,方法是对它公开(对整个应用程序公开) 可能吗?如果是的话,怎么办 请导游 重新编辑: 从下面的代码中可以看出,第二个选项卡窗格的实现已经显示出来,请忽略您可能发现的与本机java的语法差异(这是为Blackberry JDE实现的)。实现结构保持不变,因此请忽略差异,并为所面临的问题提出合理的解决方案Java 将try-catch包含在公共方法中,并在需要时调用,java,try-catch,Java,Try Catch,如何将try-catch封装在公共方法中,并在需要时调用 我已尝试捕获一个屏幕代码。我想从另一个屏幕调用它,方法是对它公开(对整个应用程序公开) 可能吗?如果是的话,怎么办 请导游 重新编辑: 从下面的代码中可以看出,第二个选项卡窗格的实现已经显示出来,请忽略您可能发现的与本机java的语法差异(这是为Blackberry JDE实现的)。实现结构保持不变,因此请忽略差异,并为所面临的问题提出合理的解决方案 // setup the second tab vfm = new Ver
// setup the second tab
vfm = new VerticalFieldManager(
Field.USE_ALL_HEIGHT | Field.USE_ALL_WIDTH |
Manager.VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL );
//Initialize grid for publishing results
grid.add(new LabelField("Name")
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.CYAN);
super.paint(graphics);
}
});
grid.add(new LabelField("Total")
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.CYAN);
super.paint(graphics);
}
});
grid.setColumnPadding(100);
grid.setRowPadding(20);
//TRY CATCH STARTS HERE
try
{
//Open or create the database
Database db = DatabaseFactory.openOrCreate("database1.db");
Statement statementG55 = db.createStatement("CREATE TABLE IF NOT EXISTS GTemp4(gname TEXT,gbal INTEGER)");
statementG55.prepare();
statementG55.execute();
statementG55.close();
Statement statementG56 = db.createStatement("SELECT gname,gbal FROM GTemp4 ORDER BY ROWID DESC");
statementG56.prepare();
statementG56.execute();
Cursor c = statementG56.getCursor();
//Get to the row of grid
for (int i =1; i < grid.getRowCount(); i++)
{
System.out.println("Inside for first loops");
//Get to the column of grid
for (int j = 0; j < grid.getColumnCount() ; j++)
{
System.out.println("Inside for second loops");
//Get to the row of temp4 table
while(c.next())
{
System.out.println("Inside while");
Row r;
r = c.getRow();
for (int k = 1; k >=0; k--)
{
System.out.println("Inside for loops");
if(k==0)
{
System.out.println("Retrieving Names");
grid.insert(new LabelField(r.getString(k))
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.GOLD);
super.paint(graphics);
}
},i,j);
}
else
{
System.out.println("Retrieving other values");
String p = "" + r.getObject(k);
grid.insert(new LabelField(p)
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.GOLD);
super.paint(graphics);
}
},i,j);
}
grid.setBackground(BackgroundFactory.createLinearGradientBackground(Color.MIDNIGHTBLUE,Color.STEELBLUE,Color.MIDNIGHTBLUE,Color.STEELBLUE));
}
System.out.println("Exiting while");
}
System.out.println("Exiting sec for");
break;
}
System.out.println("Exiting first for");
break;
}
statementG56.close();
db.close();
}
catch(Exception e)
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
vfm.add(grid);
nullFld = new NullField( Field.FOCUSABLE );
hfm = new HorizontalFieldManager();
hfm.add( nullFld );
hfm.add( myLbl );
pane = new Pane( hfm, vfm );
model.addPane( pane );
//设置第二个选项卡
vfm=新的垂直现场管理器(
Field.USE_ALL_HEIGHT | Field.USE_ALL_WIDTH|
Manager.VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL);
//初始化网格以发布结果
grid.add(新LabelField(“名称”)
{
公共空间绘制(图形)
{
图形.设置颜色(颜色.青色);
超级油漆(图形);
}
});
网格添加(新LabelField(“总计”)
{
公共空间绘制(图形)
{
图形.设置颜色(颜色.青色);
超级油漆(图形);
}
});
网格设置列填充(100);
grid.setRowPadding(20);
//从这里开始尝试接球
尝试
{
//打开或创建数据库
Database db=DatabaseFactory.openOrCreate(“database1.db”);
语句statementG55=db.createStatement(“如果不存在创建表GTemp4(gname文本,gbal整数)”;
语句g55.prepare();
语句g55.execute();
语句g55.close();
语句statementG56=db.createStatement(“按ROWID DESC从GTemp4顺序选择gname、gbal”);
语句G56.prepare();
语句G56.execute();
游标c=statementG56.getCursor();
//到达网格的那一行
对于(int i=1;i=0;k--)
{
System.out.println(“循环内部”);
如果(k==0)
{
System.out.println(“检索名称”);
grid.insert(新LabelField(r.getString(k))
{
公共空间绘制(图形)
{
图形.设置颜色(颜色.金色);
超级油漆(图形);
}
},i,j);
}
其他的
{
System.out.println(“检索其他值”);
字符串p=”“+r.getObject(k);
网格插入(新LabelField(p)
{
公共空间绘制(图形)
{
图形.设置颜色(颜色.金色);
超级油漆(图形);
}
},i,j);
}
setBackground(BackgroundFactory.createLinearGradientBackground(Color.MidlingBlue,Color.STEELBLUE,Color.MidlingBlue,Color.STEELBLUE));
}
System.out.println(“退出时”);
}
System.out.println(“退出sec for”);
打破
}
System.out.println(“先退出为”);
打破
}
语句G56.close();
db.close();
}
捕获(例外e)
{
System.out.println(e.getMessage());
e、 printStackTrace();
}
vfm.add(网格);
nullFld=新的NullField(Field.FOCUSABLE);
hfm=新HorizontalFieldManager();
hfm.add(nullFld);
hfm.add(myLbl);
窗格=新窗格(hfm、vfm);
model.addPane(窗格);
非常感谢下面的每一个人
public void doSomeSearching(...) throws Exception //This will allow you to remove the try/catch block from within the method and be able to catch any exceptions in the layer above.
{
//Do the searching
//Update panel2
}
JButton btnSearch = new JButton("Search");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
try
{
doSomeSearching();
}
catch (Exception e)
{
//Do something when exception is raised.
}
}
});