gpt4 book ai didi

java - 如何向位于子类的父类(super class)中的 Jbutton 添加功能?

转载 作者:行者123 更新时间:2023-11-30 10:38:29 27 4
gpt4 key购买 nike

我正在尝试使用子类向 Jbutton 添加功能,但是 Jbutton 实际上是在父类(super class)中声明和定义的。这是我的代码:

 JButton zz=new JButton(ss);
zz.setBounds(470,70,35,35);
zz.setBorder(oo);
zz.setBackground(new Color(0,170,120));
l.add(zz);

这是我的 Jbutton,位于我的项目中名为 realestate 的父类(super class)中。我创建了子类,我试图在其中为此按钮添加 Action 监听器。这是我的子类:

public class assan extends RealEstate{
zz.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent u)
{
System.out.println("kk");
}
});}

但我运行这个程序时遇到了不同类型的错误,例如:包 'zz' 不存在 等等。这是我的完整代码:

 package realestate;

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

public class Realestate extends JFrame {
Realestate()
{
JLabel l=new JLabel(new ImageIcon("C:\\Users\\MUHAMMAD SHAHAB\\Documents\\NetBeansProjects\\Real Estate\\src\\real\\estate\\file (2).jpg"));
l.setBounds(100,50,300,250);
add(l);
ImageIcon ss=new ImageIcon("C:\\Users\\MUHAMMAD SHAHAB\\Documents\\NetBeansProjects\\Real Estate\\src\\real\\estate\\1470672145_Help_mark_query_question_support_talk.png");
public JButton zz=new JButton(ss);
zz.setBounds(470,70,35,35);
zz.setBackground(new Color(0,170,120));
l.add(zz);
}
public class assan extends Realestate{
zz.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent o)
{
System.out.println("Cena");
}
});
}
public static void main(String[] args) {
RealEstate v=new RealEstate();
v.setUndecorated(true);
v.setVisible(true);
v.setBounds(350,200,600,350);
v.setForeground(Color.WHITE);
assan n=new assan();
}

}

我做错了什么?

最佳答案

Imagine that this is the superclass:

public class RealEstate{
public JButton button = new JButton("Button Name");
}

And this the subclass:

public class SubClass extends RealEstate{

/**
* Constructor
*/
public SubClass(){

//The Button is public so it is visible to the subclass
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent u){
System.out.println("Hello World!");
}
});
}
}

也看看这个 ( tutorial )

A minimal example of the code you may need:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;

public class RealEstate extends JFrame {

public JButton button = new JButton("Button");

/**
* Constructor
*/
public RealEstate() {


//Button
button.setText("I am a JButton");

// ...rest of the code below
}

/**
* The SubClass
*/
protected class SubClass extends RealEstate {

/**
* Constructor
*/
public SubClass() {

//accessing the button from the SubClass
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent u) {
System.out.println("Hello World!");
}
});
}

}

/**
* Main Method
*
* @param args
*/
public static void main(String[] args) {
RealEstate v = new RealEstate();
v.setUndecorated(true);
v.setVisible(true);
v.setSize(600,600);
v.setForeground(Color.WHITE);

v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

关于java - 如何向位于子类的父类(super class)中的 Jbutton 添加功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39672872/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com