初学者遇到出血问题!

fpwcs 2008-04-03 11:33:23
import java.awt.*;
import java.awt.event.*;
import java.text.*;

import javax.swing.*;

public class BinarySearch extends JApplet implements ActionListener {
JLabel enterLabel, resultLabel;
JTextField enterField, resultField;
JTextArea output;

int array[];
String display = "";


public void init()
{

Container zs=getContentPane();
Container container =getcontentpane();

container.setLayout(new FlowLayout());//这种语法我不懂啊,为什么又不能把这句去掉呢?为何传递一个没有句丙的对象??


enterLabel = new JLabel( "Enter integer search key" );
container.add( enterLabel );

enterField = new JTextField( 10 );
container.add( enterField );


enterField.addActionListener( this );


resultLabel = new JLabel( "Result" );
container.add( resultLabel );

resultField = new JTextField( 20 );
resultField.setEditable( false );
container.add( resultField );


output = new JTextArea( 6, 60 );
output.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) );
container.add( output );


array = new int[ 15 ];

for ( int counter = 0; counter < array.length; counter++ )
array[ counter ] = 2 * counter;

}


public void actionPerformed( ActionEvent actionEvent )
{

String searchKey = actionEvent.getActionCommand();


display = "Portions of array searched\n";


int element = binarySearch( array, Integer.parseInt( searchKey ) );

output.setText( display );


if ( element != -1 )
resultField.setText( "Found value in element " + element );
else
resultField.setText( "Value not found" );

}


public int binarySearch( int array2[], int key )
{
int low = 0;
int high = array2.length - 1;
int middle;


while ( low <= high ) {
middle = ( low + high ) / 2;


buildOutput( array2, low, middle, high );


if ( key == array[ middle ] )
return middle;


else if ( key < array[ middle ] )
high = middle - 1;


else
low = middle + 1;

}

return -1;

}

void buildOutput( int array3[], int low, int middle, int high )
{

DecimalFormat twoDigits = new DecimalFormat( "00" );

// loop through array elements
for ( int counter = 0; counter < array3.length; counter++ ) {

// if counter outside current array subset, append
// padding spaces to String display
if ( counter < low || counter > high )
display += " ";

// if middle element, append element to String display
// followed by asterisk (*) to indicate middle element
else if ( counter == middle )
display += twoDigits.format( array3[ counter ] ) + "* ";

else // append element to String display
display += twoDigits.format( array3[ counter ] ) + " ";

} // end for

display += "\n";

} // end method buildOutput

} // end class BinarySearch


...全文
85 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
longshenls 2008-04-03
  • 打赏
  • 举报
回复
友情提示:

你提过 35 个问题,1 个是结帖的,1 个是无满意答案结帖(其实已经解决了),33 个未结。
结论:结帖率太低
这情况。。。
hjxaut 2008-04-03
  • 打赏
  • 举报
回复
确实,它是传入一个布局器对象,FlowLayout对象为布局器对象。由于你后面不需要对该对象进行操作,所以传入一个匿名的就可以了
fuyou001 2008-04-03
  • 打赏
  • 举报
回复
真的假的?
  • 打赏
  • 举报
回复
这种形式是匿名对象引用,new FlowLayout() 对象只用到一次,就没有必要再声明一个变量了。

如果采用 FlowLayout layout = new FlowLayout(); 再把 layout 传进去的话,也是将它
的引用作为方法参数传入的。


友情提示:

你提过 35 个问题,1 个是结帖的,1 个是无满意答案结帖(其实已经解决了),33 个未结。
结论:结帖率太低
chen09 2008-04-03
  • 打赏
  • 举报
回复
设置布局为FlowLayout(流式布局?),设好就ok,不需要吧句柄留下。
new FlowLayout(),缺省为中央靠拢。
new FlowLayout(FlowLayout.LEFT)或者new FlowLayout(FlowLayout.RIGHT)等

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧