补充:有一段少打了个括号
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == open )
open.setText("OFF");
}
});
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == open )
open.setText("OFF");
}
});
解决方案 »
- 请问,哪款Java剖析工具,可以显示函数的调用过程?
- 解决这个问题你就XX了(java)
- 如何实现打印到不同打印机的功能?
- 请前辈们给讲一下接口的相关知识?
- 关于jdbc连oracle的小问题
- jdbc PreparedStatement 的 setString() 方法的问题
- java 日期类的问题 在此先谢过了! 急!
- 如何进行国际化?
- MD5简单问题
- 请问jdk1.3的原代码在哪里可以得到?用什么可以看,notpad行不行??
- 在java中是不是只要在基类(非抽象类)中定义的公有方法都可以在子类复写,好像c++和c#中子类只能复写基类的虚拟方法啊.谢谢!
- 急问该怎么实现这么一个记时功能呢? 谢谢。。。
import javax.swing.UIManager;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test_inside
{
private JButton A,open;
public static void main(String[] args)
{
JButton A = new JButton("a");
final JButton open = new JButton("open");
JPanel JP = new JPanel();
JP.setLayout(new FlowLayout());
JFrame JF = new JFrame("true or false");
JF.getContentPane().add(JP, BorderLayout.CENTER);
JP.add(open);
JP.add(A);
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setBounds(220, 80, 250, 500);
JF.setVisible(true);
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == open) //local variable open is accessed from within inner class;needs to be declared final
open.setText("OFF");
}
});
}
}提醒一下,类名一定要用大写
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test_inside extends JFrame
{
private JButton jbA;
private JButton jbOpen;
public test_inside()
{
jbA = new JButton("a");
jbOpen = new JButton("open");
JPanel jp = new JPanel();
jp.setLayout(new FlowLayout());
getContentPane().add(jp,BorderLayout.CENTER);
jp.add(jbOpen);
jp.add(jbA);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(220,80,250,200);
setVisible(true);
jbOpen.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbOpen )
{
if ( jbOpen.getText().equals("open") )
jbOpen.setText("close");
else
jbOpen.setText("open");
}
}
}
);
}
public static void main(String[] args)
{
test_inside app = new test_inside();
app.addWindowListener
(
new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
将private JButton A,open;改为
static JButton A,open;2、
JButton A=new JButton("a");
JButton open=new JButton("open");
改为
A=new JButton("a");
open=new JButton("open");
有3种方法都可行:
1, 将private JButton A,open;改为 static JButton A,open;(这样就不需要 JButton source = (JButton)e.getSource();进行强制转化了)
public class test_inside extends JFrame
{
static JButton A,open;
public static void main(String[] args)
{
A=new JButton("a");
open=new JButton("open");2, 使用 JButton source = (JButton)e.getSource(); 也可以解决这个问题import javax.swing.*;
import java.awt.event.*;public class Check extends JFrame
{
private JButton jb1 = new JButton("Button1"); public Check()
{
setSize(300,200);
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton source = (JButton)e.getSource();
String str = source.getText();
if( source == jb1 )
if(str == "Button1")
jb1.setText("按钮1");
else
jb1.setText("Button1");
}
}; jb1.addActionListener(al);
getContentPane().add(jb1); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
show();
}
public static void main(String[] args)
{
Check check=new Check();
}
}
3, 使用final final JButton open = new JButton("open");