当前位置: 代码迷 >> Java相关 >> [求助]我的这个如何编译?
  详细解决方案

[求助]我的这个如何编译?

热度:293   发布时间:2006-02-26 23:48:00.0
[求助]我的这个如何编译?

import java.awt.*;
import javax.swing.*;

class GUI extends JFrame
{
JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
JRadioButton radYes=new JRadioButton("Yes",true);
JRadioButton radNo=new JRadioButton("No",false);
ButtonGroup radioGroup1 = new ButtonGroup();

RadioButtonListener rbListener=new RadioButtonListener();
ButtonListener btListener=new ButtonListener();

GUI()
{
Container c=getContentPane();
c.setLayout(null);

c.add(btn1);
c.add(btn2);
c.add(radYes);
c.add(radNo);

radioGroup1.add(radYes);
radioGroup1.add(radNo);

radYes.addActionListener(rbListener);
radNo.addActionListener(rbListener);
btn1.addActionListener(btnListener);
btn2.addActionListener(btnListener);

radYes.setBounds(30,30,50,40);
radNo.setBounds(30,80,50,40);
btn1.setBounds(130,30,200,40);
btn2.setBounds(130,80,200,40);

setSize(380,200);
setTitle("GUI Demo with Radio Button "+"and Button Listener");
setVisible(true);
setResizable(false);
}

}


不知道怎么搞的,我怎么编译都出现错误,楼主能不能帮帮我啊!小弟这里先谢谢了!

搜索更多相关的解决方案: new  JButton  JRadioButton  编译  

----------------解决方案--------------------------------------------------------
你看行吗!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class xuce
{
public static void main(String[] args)
{
CreatFrame frame=new CreatFrame();
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
frame.setResizable(false);
}
}
class CreatFrame extends JFrame
{
public CreatFrame()
{
setTitle("GUI Demo with Radio Button "+"and Button Listener");
setSize(380,200);

ActionListener rbListener=new RadioButtonListener();
ActionListener btListener=new ButtonListener();


JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
JRadioButton radYes=new JRadioButton("Yes",true);
JRadioButton radNo=new JRadioButton("No",false);
ButtonGroup radioGroup1 = new ButtonGroup();

radioGroup1.add(radYes);
radioGroup1.add(radNo);

btn1.addActionListener(btListener);
btn2.addActionListener(btListener);
radYes.addActionListener(rbListener);
radNo.addActionListener(rbListener);

Container c=getContentPane();
c.setLayout(null);

c.add(btn1);
c.add(btn2);
c.add(radYes);
c.add(radNo);

radYes.setBounds(30,30,50,40);
radNo.setBounds(30,80,50,40);
btn1.setBounds(130,30,200,40);
btn2.setBounds(130,80,200,40);
}
private JButton btn1;
private JButton btn2;
private JRadioButton radYes;
private JRadioButton radNo;
private ButtonGroup radioGroup1;
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

}
}
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

}
}

}


----------------解决方案--------------------------------------------------------


RadioButtonListener rbListener=new RadioButtonListener();
ButtonListener btListener=new ButtonListener();
这两句是哪里来的,你又没有定义RadioButtonListener,ButtonListener。
就算你定义了,你也是从别的Listener那里继承的吧
那么你至少要导入一个包import java.awt.event.*;


----------------解决方案--------------------------------------------------------
好象有点超了吧!

当然首先得谢谢楼主你的帮忙,不过遗憾的是我看的不是太懂,你帮我改的那程序太难了。有没有简单一点的啊,我也是刚开始学这个java,我那段程序是照抄的书上的一段,谁知道出现了错误 ,楼主你看能不能用简化点的语言来表达那个,让我好理解一下,这里先谢谢了!


----------------解决方案--------------------------------------------------------

程序代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*; //需要导入这个包

class GUI extends JFrame
{
JButton btn1 = new JButton(\"Button 1\");
JButton btn2 = new JButton(\"Button 2\");
JRadioButton radYes=new JRadioButton(\"Yes\",true);
JRadioButton radNo=new JRadioButton(\"No\",false);
ButtonGroup radioGroup1 = new ButtonGroup();

RadioButtonListener rbListener=new RadioButtonListener(); //对RadioButtonListener类进行实例化
ButtonListener btnListener=new ButtonListener(); //对ButtonListener类进行实例化

GUI()
{
Container c=getContentPane();
c.setLayout(null);

c.add(btn1);
c.add(btn2);
c.add(radYes);
c.add(radNo);

radioGroup1.add(radYes);
radioGroup1.add(radNo);

radYes.addActionListener(rbListener);
radNo.addActionListener(rbListener);
btn1.addActionListener(btnListener);
btn2.addActionListener(btnListener);

radYes.setBounds(30,30,50,40);
radNo.setBounds(30,80,50,40);
btn1.setBounds(130,30,200,40);
btn2.setBounds(130,80,200,40);

setSize(380,200);
setTitle(\"GUI Demo with Radio Button \"+\"and Button Listener\");
setVisible(true);
setResizable(false);
}

class RadioButtonListener implements ActionListener{ //内部类继承了ActionListener
public void actionPerformed(ActionEvent e){

}
}

class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){

}
}

}



可能是漏抄了2个继承了ActionListener的类吧


----------------解决方案--------------------------------------------------------
编译完后只有那两句有问题
那两个监听的接口
肯定没有声明!

----------------解决方案--------------------------------------------------------
太感谢了

我看了一下,成功了,太感谢楼主了,刚看了一下别人发的帖子,楼主真的很敬业!


----------------解决方案--------------------------------------------------------
  相关解决方案