java组件运用案例Word下载.docx
《java组件运用案例Word下载.docx》由会员分享,可在线阅读,更多相关《java组件运用案例Word下载.docx(15页珍藏版)》请在冰点文库上搜索。
i<
number;
i++)
JPanelpanel=newJPanel(newFlowLayout(FlowLayout.RIGHT));
this.getContentPane().add(panel);
if(i==1)
{combox=newJComboBox(operators);
//组合框,默认不可编辑
panel.add(combox);
}
if(i==number-1)
JButtonbutton=newJButton("
="
panel.add(button);
button.addActionListener(this);
//注册单击事件监听器
}
texts[i]=newJTextField("
0"
5);
texts[i].setHorizontalAlignment(SwingConstants.RIGHT);
//文本行右对齐
panel.add(texts[i]);
texts[i+number]=newJTextField("
8);
texts[i+number].setHorizontalAlignment(SwingConstants.RIGHT);
texts[i+number].setEditable(false);
panel.add(texts[i+number]);
texts[number-1].setEditable(false);
//只能显示,不允许编辑
this.setVisible(true);
publicvoidactionPerformed(ActionEvente)//按钮单击事件处理方法
try
{intx=Integer.parseInt(texts[0].getText(),10);
Stringa=Integer.toBinaryString(x);
inty=Integer.parseInt(texts[1].getText(),10);
Stringb=Integer.toBinaryString(y);
texts[3].setText(a);
texts[4].setText(b);
switch(combox.getSelectedIndex())
{case0:
x+=y;
break;
case1:
x-=y;
texts[2].setText(x+"
"
texts[5].setText(Integer.toString(x,2));
catch(NumberFormatExceptionnfe)
{JOptionPane.showMessageDialog(this,"
字符串不能转换成整数,请重新输入!
finally{}
publicstaticvoidmain(Stringarg[])
newIntBinaryJFrame();
}
计算器运用实例
publicclassjisuanqiextendsJFrameimplementsActionListener{
privateJTextFieldtext_math;
privateJPaneltop;
privateJPanelcalckeysPanel;
privatefinalString[]keys={"
sqrt"
+/-"
CE"
C"
7"
8"
9"
/"
4"
5"
6"
"
*"
1"
2"
3"
."
};
privateJButtonk[]=newJButton[keys.length];
privatebooleanfirst=true;
privatedoublere_num=0;
privateStringoperator="
;
privatebooleanoperatev=true;
publicjisuanqi(){
super("
计算器"
this.init();
this.setBounds(300,200,300,300);
this.setBackground(java.awt.Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.pack();
privatevoidinit(){
text_math=newJTextField("
text_math.setHorizontalAlignment(JTextField.RIGHT);
text_math.setBackground(Color.WHITE);
top=newJPanel();
top.setLayout(newBorderLayout());
top.add("
Center"
text_math);
calckeysPanel=newJPanel();
calckeysPanel.setLayout(newGridLayout(5,4,3,3));
for(inti=0;
i<
keys.length;
i++){
k[i]=newJButton(keys[i]);
calckeysPanel.add(k[i]);
getContentPane().setLayout(newBorderLayout(3,5));
getContentPane().add("
North"
top);
calckeysPanel);
k[i].addActionListener(this);
publicstaticvoidmain(String[]args){
newjisuanqi();
publicvoidactionPerformed(ActionEvente){
Stringlabel=e.getActionCommand();
if(label.equals("
)){
text_math.setText("
}elseif(label.equals("
handleC();
}elseif("
0123456789."
.indexOf(label)>
=0){
handlenum(label);
}else{
handleOperator(label);
privatevoidhandlenum(Stringk){
if(first){
text_math.setText(k);
}elseif((k.equals("
))&
&
(text_math.getText().indexOf("
)<
0)){
text_math.setText(text_math.getText()+"
}elseif(!
k.equals("
text_math.setText(text_math.getText()+k);
first=false;
privatevoidhandleC(){
first=true;
operator="
privatevoidhandleOperator(Stringk){
if(operator.equals("
//除法运算
//如果当前结果文本框中的值等于0
if(getNumberFromText()==0){
//操作不合法
operatev=false;
text_math.setText("
除数不能为零"
re_num/=getNumberFromText();
}elseif(operator.equals("
//加法运算
re_num+=getNumberFromText();
//减法运算
re_num-=getNumberFromText();
//乘法运算
re_num*=getNumberFromText();
//平方根运算
re_num=Math.sqrt(re_num);
//正数负数运算
re_num=re_num*(-1);
//赋值运算
re_num=getNumberFromText();
if(operatev){
//双精度浮点数的运算
longt1;
doublet2;
t1=(long)re_num;
t2=re_num-t1;
if(t2==0){
text_math.setText(String.valueOf(t1));
text_math.setText(String.valueOf(re_num));
//运算符等于用户按的按钮
operator=k;
operatev=true;
privatedoublegetNumberFromText(){
doubleresult=0;
try{
result=Double.valueOf(text_math.getText()).doubleValue();
}catch(NumberFormatExceptione){
returnresult;
表格运用实例
importjavax.swing.table.DefaultTableModel;
publicclassFangZhengextendsJFrameimplementsActionListener{
privateJTextFieldtextn;
privateJButtonjb1,jb2;
privateDefaultTableModeltablemodel;
publicFangZheng(){
循环移位方阵"
Dimensiondim=getToolkit().getScreenSize();
this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2);
this.setBackground(java.awt.Color.lightGray);
JPanelpanel=newJPanel();
this.getContentPane().add(panel,"
panel.add(newJLabel("
随机个数(范围1-10):
));
textn=newJTextField("
panel.add(textn);
jb1=newJButton("
确定"
panel.add(jb1);
jb1.addActionListener(this);
Stringlk[]={"
"
for(inti=0;
i<
lk.length;
i++){
panel.add(newJLabel(lk[i]));
循环移位"
jb2=newJButton("
开始"
jb2.addActionListener(this);
panel.add(jb2);
Stringtitles[]=newString[10];
titles.length;
i++)
titles[i]=(i+1)+"
this.tablemodel=newDefaultTableModel(titles,10);
JTabletable=newJTable(this.tablemodel);
this.getContentPane().add(newJScrollPane(table),"
if(e.getSource()==jb1){
try{
intn=Integer.parseInt(this.textn.getText());
for(inti=0;
10;
for(intj=0;
j<
j++){
this.tablemodel.setValueAt(null,i,j);
}
}
random(n);
}
catch(NumberFormatExceptionnfex){
JOptionPane.showMessageDialog(this,"
无法转换成整数"
提醒"
JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource()==jb2){
intn=Integer.parseInt(this.textn.getText());
for(inti=1;
n;
for(intj=0;
n-1;
this.tablemodel.setValueAt(this.tablemodel.getValueAt(i-1,j),i,j+1);
this.tablemodel.setValueAt(this.tablemodel.getValueAt(i-1,n-1),i,0);
privatevoidrandom(intn){
for(intj=0;
j++)
if(j<
n)
this.tablemodel.setValueAt((int)(Math.random()*100)+"
0,j);
else
this.tablemodel.setValueAt(null,0,j);
newFangZheng();
画布运用实例
publicclassArchimedesJFrameextendsJFrameimplementsActionListener{
privateArchimedesCanvasarchimedes;
privateJButtonjb3;
privateJPaneljp1;
privateJTextFieldtext_q;
privateJRadioButtonbcolor[];
privatedoublen;
publicArchimedesJFrame(){
阿基米德螺线"
text_q=newJTextField(10);
jp1=newJPanel();
jp1.add(newJLabel("
圈数:
jp1.add(text_q);
jb3=newJButton("
jp1.add(jb3);
jb3.addActionListener(this);
jp1.add(newJLabel(lk[i]));
颜色:
Stringcolorstr[]={"
红"
绿"
蓝"
ButtonGroupcolorgroup=newButtonGroup();
bcolor=newJRadioButton[colorstr.length];
bcolor.length;
i++){
bcolor[i]=newJRadioButton(colorstr[i]);
colorgroup.add(bcolor[i]);
jp1.add(bcolor[i]);
bcolor[i].addActionListener(this);
this.getContentPane().add(jp1,"
archimedes=newArchimedesCanvas(Color.black,this.n);
this.getContentPane().add(newJScrollPane(archimedes),"
publicvoidactionPerformed(ActionEvente){
Colorc=null;
if(e.getSource()==bcolor[0])
c=newColor(255,0,0);
if(e.getSource()==bcolor[1])
c=newColor(0,255,0);
if(e.getSource()==bcolor[2])
c=newColor(0,0,255);
if(e.getSource()==jb3){
try{
this.n=Double.parseDouble(text_q.getText());
无法转换成浮点数"
try{
if(n<
0||n>
20)
thrownewException("
archimedes.setn(n);
archimedes.setColor(c);
archimedes.repaint();
}catch(Exceptionqwe){
JOptionPane.showMessageDialog(this,"
圈数必须在0~20之间"
publicstaticvoidmain(String[]args){
newArchimedesJFrame();
}
classArchimedesCanvasextendsCanva