我写了三个小程序:DrwOval.java,DrwTriangle.java,DrwRectangle.java,怎样再写一个图形界面用它上面三个按钮访问那三个小程序?有那个高手详细写出来,小弟不胜感激:'(
我已经搞了几天都弄不好,不知是哪里出问题了.注意:这四个程序是分开的.我写的源代码入下:
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowGraphics extends JFrame implements ActionListener {
Frame f=new Frame("画图小程序");
Button a1=new Button("画三角形");
Button a2=new Button("画矩形");
Button a3=new Button("画圆形");
public ShowGraphics()
{
setLayout(new FlowLayout());
add(a1);
add(a2);
add(a3);
setSize(300,200);
setVisible(true);
}
a1.addActionListener(new ActionListener)
a2.addActionListener(new ActionListener)
a3.addActionListener(new ActionListener){
public void actionPerformed(ActionEvent e)
{
DrwTriangle a=new DrwTriangle();
a.getName()
DrwRectangle b=new DrwRectangle();
b.getName()
DrwOval c=new DrwOval();
c.getName()
}
}
}
有那个好心人告诉我上面哪里错了?
我已经搞了几天都弄不好,不知是哪里出问题了.注意:这四个程序是分开的.我写的源代码入下:
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowGraphics extends JFrame implements ActionListener {
Frame f=new Frame("画图小程序");
Button a1=new Button("画三角形");
Button a2=new Button("画矩形");
Button a3=new Button("画圆形");
public ShowGraphics()
{
setLayout(new FlowLayout());
add(a1);
add(a2);
add(a3);
setSize(300,200);
setVisible(true);
}
a1.addActionListener(new ActionListener)
a2.addActionListener(new ActionListener)
a3.addActionListener(new ActionListener){
public void actionPerformed(ActionEvent e)
{
DrwTriangle a=new DrwTriangle();
a.getName()
DrwRectangle b=new DrwRectangle();
b.getName()
DrwOval c=new DrwOval();
c.getName()
}
}
}
有那个好心人告诉我上面哪里错了?
楼主【RINTO】截止到2008-06-26 10:15:52的历史汇总数据(不包括此帖):
发帖数:0 发帖分:0
结贴数:0 结贴分:0
未结数:0 未结分:0
结贴率:-------------------结分率:-------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
public void actionPerformed(ActionEvent e) {}
}
这个不对吧?
内部类的写法有问题
应该是
a3.addActionListener(new ActionListener{
public void actionPerformed(ActionEvent e) {}
});
public void actionPerformed(ActionEvent e)
{
DrwTriangle a=new DrwTriangle();
a.getName()
DrwRectangle b=new DrwRectangle();
b.getName()
DrwOval c=new DrwOval();
c.getName()
}
});
new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
}
E:\j\ShowGraphics.java:19: 需要 <标识符>
a1.addActionListener(new ActionListener)
^
E:\j\ShowGraphics.java:19: 非法的类型开始
a1.addActionListener(new ActionListener)
^
E:\j\ShowGraphics.java:22: 非法的表达式开始
public void actionPerformed(ActionEvent e)
^
E:\j\ShowGraphics.java:25: 需要 ';'
a.getName()
^
E:\j\ShowGraphics.java:27: 需要 ';'
b.getName()
^
E:\j\ShowGraphics.java:29: 需要 ';'
c.getName()
^
E:\j\ShowGraphics.java:31: 需要为 class、interface 或 enum
});
^
E:\j\ShowGraphics.java:32: 需要为 class、interface 或 enum
}
^
8 错误处理已完成。
JButton button = new JButton();
JButton button1 = new JButton();
JButton button2 = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//调用的你的功能方法!
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//调用的你的功能方法!
}
});
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//调用的你的功能方法!
}
});
JButton button = new JButton();
JButton button1 = new JButton();
JButton button2 = new JButton(); button.addActionListener(new myActionListener());
button1.addActionListener(new myActionListener());
button2.addActionListener(new myActionListener()); class myActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//调用的你的功能方法!
}
}
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
public class ShowGraphics extends JFrame implements ActionListener {
JButton a1 = new JButton("画三角形");
JButton a2 = new JButton("画矩形");
JButton a3 = new JButton("画圆形");
public ShowGraphics()
{
super("画图小程序");
setLayout(new FlowLayout());
add(a1);
add(a2);
add(a3);
setSize(300,200);
setVisible(true);a1.addActionListener(new ActionListener);{
public void actionPerformed(ActionEvent e)
{
DrwTriangle a=new DrwTriangle();
a.getName()}
});
a2.addActionListener(new ActionListener);{
public void actionPerformed(ActionEvent e)
{
DrwRectangle b=new DrwRectangle();
b.getName()}
});
a3.addActionListener(new ActionListener);{
public void actionPerformed(ActionEvent e)
{
CircleFrame c=new CircleFrame();
c.getName()
}
});
}
}
为什么错误更多了?错误提示:E:\j\ShowGraphics.java:19: 需要为 '(' 或 '['
a1.addActionListener(new ActionListener);{
^
E:\j\ShowGraphics.java:20: 非法的表达式开始
public void actionPerformed(ActionEvent e)
^
E:\j\ShowGraphics.java:23: 需要 ';'
a.getName()}
^
E:\j\ShowGraphics.java:24: 需要为 class、interface 或 enum
});
^
E:\j\ShowGraphics.java:25: 需要为 class、interface 或 enum
a2.addActionListener(new ActionListener);{
^
E:\j\ShowGraphics.java:25: 需要为 class、interface 或 enum
a2.addActionListener(new ActionListener);{
^
E:\j\ShowGraphics.java:26: 需要为 class、interface 或 enum
public void actionPerformed(ActionEvent e)
^
E:\j\ShowGraphics.java:29: 需要为 class、interface 或 enum
b.getName()}
^
E:\j\ShowGraphics.java:31: 需要为 class、interface 或 enum
a3.addActionListener(new ActionListener);{
^
E:\j\ShowGraphics.java:31: 需要为 class、interface 或 enum
a3.addActionListener(new ActionListener);{
^
E:\j\ShowGraphics.java:32: 需要为 class、interface 或 enum
public void actionPerformed(ActionEvent e)
^
E:\j\ShowGraphics.java:35: 需要为 class、interface 或 enum
c.getName()
^
E:\j\ShowGraphics.java:38: 需要为 class、interface 或 enum
}
^
13 错误处理已完成。这是为什么啊?我哪里又错了????
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class ShowGraphics extends JFrame {
JButton a1 = new JButton("画三角形");
JButton a2 = new JButton("画矩形");
JButton a3 = new JButton("画圆形"); public ShowGraphics() {
super("画图小程序");
setLayout(new FlowLayout());
add(a1);
add(a2);
add(a3);
setSize(300, 200);
setVisible(true); a1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// DrwTriangle a=new DrwTriangle();
// a.getName();
}
}); a2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// DrwRectangle b=new DrwRectangle();
// b.getName();}
}
});
a3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// CircleFrame c=new CircleFrame();
// c.getName();
}
});
}
}