藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 應用編程


    

藍森林 http://www.lslnet.com 2006年6月6日 10:18


請高手指點程序錯在何處,該如何改?

import java.awt.*;
import java.io.*;

public class VFlowLayout implements LayoutManager,Serializable{

int hgap;
int vgap;

public VFlowLayout(){
this(5,5);
}

public VFlowLayout(int i,int j){
this.hgap=i;
this.vgap=j;
}

public void addLayoutComponent(String name,Component comp){
}


public void removeLayoutComponent(Component comp){
}

public Dimension  preferredLayoutSize(Container  container){
synchronized(container.getTreeLock()){
Dimension dimension1 =new Dimension(0,0);
int i=container.getComponentCount();
for(int j=0;JComponent component=container.getComponent(j);j++){
          if(component.isVisible()){
Dimension dimension2 = component.getPreferedSize();
dimension1.width=Math.max(dimension1.width,dimension2.width);
if(j>;0)
dimension1.height += vgap;
dimension1.height += dimension2.height;
}
}

Insets insets=container.getInsets();
dimension1.height +=insets.top +insets.bottom +vgap*2;
dimension1.width +=insets.left +insets.right +hgap*2;

Dimension dimension= dimension1;
return dimension;
}
}

public Dimension minimumLayoutSize(Container container){
synchronized(container.getTreeLock()){
Dimension dimension1 =new Dimension(0,0);

int i=container.getComponentCount();
for(int j=0;j component component = container.getComponent(j);
if(component.isVisible()){
Dimension dimension2 = component.getMinimumSize();
dimension1.width=Math.max(dimension1.width,dimension2.width);
if(j>;0)
dimension1.height += vgap;
dimension1.height += dimension2.height;
}
}
Insets insets=container.getInsets();
dimension1.height +=insets.top +insets.bottom +vgap*2;
dimension1.width +=insets.left +insets.right +hgap*2;

Dimension dimension= dimension1;
return dimension;
}
}

public void layoutContainer(Container container){
synchronized(container.getTreeLock()){
Insets insets=container.getInsets();
int vSpace=container.getSize().height-(insets.top+insets.bottom+vgap*2);
int componentCount =container.getComponentCount();
int left =insets.left+hgap;
int totalHeight=0;
int width=0;
int componentStart=0;
for(int i=0;Jcomponent component=container.getComponent(i));
if(component.isVisible()){
Dimension dimension = component.getPreferredSize();
component.setSize(dimension.width,dimension.width);
if(totalHeight==0||totalHeight+dimension.height<=vSpace){
if(totalHeight>;0)
totalHeight +=vgap;
totalHeight +=dimension.height;
width =Math.max(width,dimension.width);
}else{
moveComponents(Container,insets.top+vgap,left,width,componentStart,i);
totalHeight=0;
left +=hgap +width;
width =dimension.width;
componentStart=i;
}
}
}
moveComponents(container,insets.top+vgap,left,width,componentStart,componentCount);
}
}

private void moveComponents(Container container,int top,int left,int width,int componentStart,int componentEnd){
synchronized(container.getTreeLock()){
for(int i=componentStart;JComponent component=container.getComponent(i))
if(component.isVisible()){
component.setLocation(left,top);
top+=component.getPreferedSize().height+vgap;
}
}
}
}

public void setHgap(int i){
this.hgap=i;
}

public void setVgap(int i){
this.vgap=i;
}

public int getHgap(){
return(this.hgap);
}

public int getVgap(){
return(this.vgap);
}
}

請高手指點程序錯在何處,該如何改?

編譯錯,還是運行錯?

請高手指點程序錯在何處,該如何改?

是編譯錯,是一本JAVA入門教科書的例子,但就是編譯出錯。本人初學JAVA,菜鳥一個,還請高手指點,多謝了。錯誤提示如下:
VFlowLayout.java:29: ';' expected
for(int j=0;JComponent component=container.getComponent(j);j++){
                       ^
VFlowLayout.java:53: ';' expected
for(int j=0;j component component = container.getComponent(j);
              ^
VFlowLayout.java:54: illegal start of expression
if(component.isVisible()){
^
VFlowLayout.java:61: ')' expected
}
^
VFlowLayout.java:71: 'class' or 'interface' expected
public void layoutContainer(Container container){
       ^
VFlowLayout.java:100: 'class' or 'interface' expected
}
^
VFlowLayout.java:134: 'class' or 'interface' expected
^
7 errors

請高手指點程序錯在何處,該如何改?

你自己手工輸入的?感覺很多地方都完全違反Java的語法:
我改成這樣了:
[code]

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.io.Serializable;

public class VFlowLayout implements LayoutManager, Serializable {

        int hgap;

        int vgap;

        public VFlowLayout() {
                this(5, 5);
        }

        public VFlowLayout(int i, int j) {
                this.hgap = i;
                this.vgap = j;
        }

        public void addLayoutComponent(String name, Component comp) {
        }

        public void removeLayoutComponent(Component comp) {
        }

        public Dimension preferredLayoutSize(Container container) {
                synchronized (container.getTreeLock()) {
                        Dimension dimension1 = new Dimension(0, 0);
                        int i = container.getComponentCount();
                        for (int j = 0; j < i; j++) {
                                Component component = container.getComponent(j);
                                if (component.isVisible()) {
                                        Dimension dimension2 = component.getPreferredSize();
                                        dimension1.width = Math.max(dimension1.width,
                                                        dimension2.width);
                                        if (j >; 0)
                                                dimension1.height += vgap;
                                        dimension1.height += dimension2.height;
                                }
                        }

                        Insets insets = container.getInsets();
                        dimension1.height += insets.top + insets.bottom + vgap * 2;
                        dimension1.width += insets.left + insets.right + hgap * 2;

                        Dimension dimension = dimension1;
                        return dimension;
                }
        }

        public Dimension minimumLayoutSize(Container container) {
                synchronized (container.getTreeLock()) {
                        Dimension dimension1 = new Dimension(0, 0);

                        int i = container.getComponentCount();
                        for (int j = 0; j < i; j++) {
                                Component component = container.getComponent(j);
                                if (component.isVisible()) {
                                        Dimension dimension2 = component.getMinimumSize();
                                        dimension1.width = Math.max(dimension1.width,
                                                        dimension2.width);
                                        if (j >; 0)
                                                dimension1.height += vgap;
                                        dimension1.height += dimension2.height;
                                }
                        }
                        Insets insets = container.getInsets();
                        dimension1.height += insets.top + insets.bottom + vgap * 2;
                        dimension1.width += insets.left + insets.right + hgap * 2;

                        Dimension dimension = dimension1;
                        return dimension;
                }
        }

        public void layoutContainer(Container container) {
                synchronized (container.getTreeLock()) {
                        Insets insets = container.getInsets();
                        int vSpace = container.getSize().height
                                        - (insets.top + insets.bottom + vgap * 2);
                        int componentCount = container.getComponentCount();
                        int left = insets.left + hgap;
                        int totalHeight = 0;
                        int width = 0;
                        int componentStart = 0;
                        for (int i = 0; i < componentCount; i++) {
                                Component component = container.getComponent(i);
                                if (component.isVisible()) {
                                        Dimension dimension = component.getPreferredSize();
                                        component.setSize(dimension.width, dimension.width);
                                        if (totalHeight == 0
                                                        || totalHeight + dimension.height <= vSpace) {
                                                if (totalHeight >; 0)
                                                        totalHeight += vgap;
                                                totalHeight += dimension.height;
                                                width = Math.max(width, dimension.width);
                                        } else {
                                                moveComponents(container, insets.top + vgap, left,
                                                                width, componentStart, i);
                                                totalHeight = 0;
                                                left += hgap + width;
                                                width = dimension.width;
                                                componentStart = i;
                                        }
                                }
                        }
                        moveComponents(container, insets.top + vgap, left, width,
                                        componentStart, componentCount);
                }
        }

        private void moveComponents(Container container, int top, int left,
                        int width, int componentStart, int componentEnd) {
                synchronized (container.getTreeLock()) {
                        int count = container.getComponentCount();
                        for (int i = componentStart; i < count; i++) {
                                Component component = container.getComponent(i);
                                if (component.isVisible()) {
                                        component.setLocation(left, top);
                                        top += component.getPreferredSize().height + vgap;
                                }
                        }
                }
        }

        public void setHgap(int i) {
                this.hgap = i;
        }

        public void setVgap(int i) {
                this.vgap = i;
        }

        public int getHgap() {
                return (this.hgap);
        }

        public int getVgap() {
                return (this.vgap);
        }
}
[/code]

請高手指點程序錯在何處,該如何改?

真不愧是大法師,編譯通過了,多謝版主指點!
但是運行時還是有問題:
D:\>;appletviewer VFlowLayout.html
java.lang.ClassCastException
        at sun.applet.AppletPanel.createApplet(Appl
        at sun.applet.AppletPanel.runLoader(AppletP
        at sun.applet.AppletPanel.run(AppletPanel.j
        at java.lang.Thread.run(Thread.java:534)
還請版主指點該如何運行。

其中 VFlowLayout.html為
<html>;
<head>;
<title>;
first Applet
</title>;
</head>;
<body>;
<applet code="VFlowLayout.class" WIDTH=500 HEIGHT=500>;
</applet>;
</body>;
</html>;

請高手指點程序錯在何處,該如何改?

這個代碼不是Applet,不能當Applet來用

請高手指點程序錯在何處,該如何改?

感謝版主指點迷津! :em02:



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com