注册 登录
编程论坛 JAVA论坛

这个调试通过不了,帮忙看看

ehszt 发布于 2018-03-17 11:29, 3147 次点击
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
class MyPanel extends JPanel{
    int x;JLabel label;
    MyPanel(int a){
        x=a;getSize();
        label=new JLabel("我是第"+x+"个标签");
        add(label);
    }
    public Dimension getPreferredSize()
    {return new Dimension(200,50);}

}

public class Example extends Applet implements Actionlistener{
    CardLayout mycard;MyPanel myPanel[];JPanel p;
    private void addButton(JPanel pan,String butName,ActionListener listener) {
        JButton aButton= new JButton(butName);
        aButton.setActionCommand(butName);
        aButton.addActionListener(listener);
        pan.add(aButton);
    }
    public void init() {
        setLayout(new BorderLayout());
        mycard=new CardLayout();
        this.setSize(400,150);
        p=new JPanel();p.setLayout(mycard);
        myPanel = new MyPanel[10];
        for(int i=0;i<10;i++) {
            myPanel[i]=new MyPanel(i+1);
            p.add("A"+i,myPanel[i]);
        }
        JPanel p2= new JPanel();
        addButton(p2,"第一个",this);
        addButton(p2,"最后一个",this);
        addButton(p2,"前一个",this);
        addButton(p2,"后一个",this);
        add(p,"Center");add(p2,"South");
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("第一个"))mycard.first(p);
        else if(e.getActionCommand().equals("最后一个"))mycard.last(p);
        else if(e.getActionCommand().equals("前一个"))mycard.previous(p);
        else if(e.getActionCommand().equals("后一个"))mycard.next(p);
    }
}
11 回复
#2
疯狂的小a2018-03-17 11:38
回复 楼主 ehszt
警告: 无法读取 AppletViewer 属性文件: C:\Users\niyite\.hotjava\properties。请使用默认值。我的报这个警告
#3
疯狂的小a2018-03-17 11:40
只有本站会员才能查看附件,请 登录
#4
ehszt2018-03-17 11:43
回复 3楼 疯狂的小a
java.lang.Error: Unresolved compilation problems:
    Actionlistener cannot be resolved to a type
    Syntax error on token "super", Identifier expected
    The method addButton(JPanel, String, ActionListener) in the type Example is not applicable for the arguments (JPanel, String, Example)
    The method addButton(JPanel, String, ActionListener) in the type Example is not applicable for the arguments (JPanel, String, Example)
    The method addButton(JPanel, String, ActionListener) in the type Example is not applicable for the arguments (JPanel, String, Example)
    The method addButton(JPanel, String, ActionListener) in the type Example is not applicable for the arguments (JPanel, String, Example)

    at Example.<init>(Example.java:17)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.applet.AppletPanel.createApplet(Unknown Source)
    at sun.applet.AppletPanel.runLoader(Unknown Source)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
这是错误信息,文件名为Example.java

[此贴子已经被作者于2018-3-17 11:44编辑过]

#5
疯狂的小a2018-03-17 12:26
回复 4楼 ehszt
Actionlistener导错包了
#6
ehszt2018-03-17 13:19
回复 5楼 疯狂的小a
什么意思啊,不懂!
#7
疯狂的小a2018-03-17 13:59
程序代码:
package com.xiaoa.demo;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

class MyPanel extends JPanel{
    int x;JLabel label;
    MyPanel(int a){
        x=a;getSize();
        label=new JLabel("我是第"+x+"个标签");
        add(label);
    }
    public Dimension getPreferredSize()
    {return new Dimension(200,50);}

}

public class Example extends Applet implements ActionListener{
    CardLayout mycard;MyPanel myPanel[];JPanel p;
    private void addButton(JPanel pan,String butName,ActionListener listener) {
        JButton aButton= new JButton(butName);
        aButton.setActionCommand(butName);
        aButton.addActionListener(listener);
        pan.add(aButton);
    }
    public void init() {
        setLayout(new BorderLayout());
        mycard=new CardLayout();
        this.setSize(400,150);
        p=new JPanel();p.setLayout(mycard);
        myPanel = new MyPanel[10];
        for(int i=0;i<10;i++) {
            myPanel[i]=new MyPanel(i+1);
            p.add("A"+i,myPanel[i]);
        }
        JPanel p2= new JPanel();
        addButton(p2,"第一个",this);
        addButton(p2,"最后一个",this);
        addButton(p2,"前一个",this);
        addButton(p2,"后一个",this);
        add(p,"Center");add(p2,"South");
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("第一个"))mycard.first(p);
        else if(e.getActionCommand().equals("最后一个"))mycard.last(p);
        else if(e.getActionCommand().equals("前一个"))mycard.previous(p);
        else if(e.getActionCommand().equals("后一个"))mycard.next(p);
    }
}
#8
ehszt2018-03-17 14:25
回复 7楼 疯狂的小a
你通过了?
我的试验结果和你不一样。
#9
疯狂的小a2018-03-17 15:04
回复 8楼 ehszt
你有什么结果
#10
ehszt2018-03-17 15:12
回复 9楼 疯狂的小a
去掉第一句好了。
你是改了什么地方吗?

[此贴子已经被作者于2018-3-17 15:15编辑过]

#11
疯狂的小a2018-03-17 15:16
import java.awt.event.ActionListener;
这句的问题
#12
ehszt2018-03-17 15:21
回复 11楼 疯狂的小a
不是这个,是public class Example extends Applet implements Actionlistener中的ActionListener的“l”没大写
不过还是要谢谢你
1