这个java程序不能显示第一个按钮上的图片,并且还不能实现按钮的功能,但是代码是完好的。。。
帮忙看一下第一个按钮是JButtonTrans类的,不能显示,也似乎没有被监听第二、三个按钮式JButton类的,可以显示怎样才能让我的三个按钮上都能显示图片,并且能被监...
帮忙看一下
第一个按钮是JButtonTrans类的,不能显示,也似乎没有被监听
第二、三个按钮式JButton类的,可以显示
怎样才能让我的三个按钮上都能显示图片,并且能被监听,实现点击按钮JTextField能被更改的效果。。。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class simple extends JFrame{
ImageIcon dog=new ImageIcon("images/dog.jpg");
ImageIcon cat=new ImageIcon("images/cat.jpg");
ImageIcon pig=new ImageIcon("images/pig.jpg");
JButtonTrans a=new JButtonTrans(dog);
JButton b=new JButton(cat);
JButton c=new JButton(pig);
JTextField d=new JTextField("do you think what are you?");
simple(){
super("simple");
setSize(300,170);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel aa=new JPanel();
aa.setLayout(new GridLayout(1,3,10,10));
aa.add(a);
aa.add(b);
aa.add(c);
JPanel bb=new JPanel();
bb.setLayout(new GridLayout(2,1));
d.setFont(new Font(null,1,14));
d.setHorizontalAlignment(JTextField.CENTER);
d.setEditable(false);
bb.add(d);
bb.add(aa);
setContentPane(bb);
setVisible(true);
}
class JButtonTrans extends JButton implements MouseListener{
JButton a;
JButtonTrans(ImageIcon aa){
a=new JButton(aa);
a.addMouseListener((MouseListener) this);
}
public void mouseClicked (MouseEvent e){
if(e.getSource() instanceof JButton){
if(e.getSource()==dog){
d.setText("you are a stupid dog");
}
else if(e.getSource()==cat){
d.setText("you are a stupid cat");
}
else{
d.setText("you are a stupid pig");
}
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
if(e.getSource() instanceof JButton){
if(e.getSource()==dog){
d.setText("you are a stupid dog");
}
else if(e.getSource()==cat){
d.setText("you are a stupid cat");
}
else{
d.setText("you are a stupid pig");
}
}
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
public static void main(String arg[]){
JFrame a=new simple();
}
} 展开
第一个按钮是JButtonTrans类的,不能显示,也似乎没有被监听
第二、三个按钮式JButton类的,可以显示
怎样才能让我的三个按钮上都能显示图片,并且能被监听,实现点击按钮JTextField能被更改的效果。。。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class simple extends JFrame{
ImageIcon dog=new ImageIcon("images/dog.jpg");
ImageIcon cat=new ImageIcon("images/cat.jpg");
ImageIcon pig=new ImageIcon("images/pig.jpg");
JButtonTrans a=new JButtonTrans(dog);
JButton b=new JButton(cat);
JButton c=new JButton(pig);
JTextField d=new JTextField("do you think what are you?");
simple(){
super("simple");
setSize(300,170);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel aa=new JPanel();
aa.setLayout(new GridLayout(1,3,10,10));
aa.add(a);
aa.add(b);
aa.add(c);
JPanel bb=new JPanel();
bb.setLayout(new GridLayout(2,1));
d.setFont(new Font(null,1,14));
d.setHorizontalAlignment(JTextField.CENTER);
d.setEditable(false);
bb.add(d);
bb.add(aa);
setContentPane(bb);
setVisible(true);
}
class JButtonTrans extends JButton implements MouseListener{
JButton a;
JButtonTrans(ImageIcon aa){
a=new JButton(aa);
a.addMouseListener((MouseListener) this);
}
public void mouseClicked (MouseEvent e){
if(e.getSource() instanceof JButton){
if(e.getSource()==dog){
d.setText("you are a stupid dog");
}
else if(e.getSource()==cat){
d.setText("you are a stupid cat");
}
else{
d.setText("you are a stupid pig");
}
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
if(e.getSource() instanceof JButton){
if(e.getSource()==dog){
d.setText("you are a stupid dog");
}
else if(e.getSource()==cat){
d.setText("you are a stupid cat");
}
else{
d.setText("you are a stupid pig");
}
}
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
public static void main(String arg[]){
JFrame a=new simple();
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询