2016-09-24 6 views
2

サブクラスを使用してJbuttonに機能を追加しようとしていますbutt Jbuttonは実際に宣言され、スーパークラスで定義されています。 は、ここに私のコードです:サブクラスのスーパークラスにあるJbuttonにfuntionallityを追加するにはどうすればよいですか?

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

これである私は、このボタンのアクションリスナーを追加しようとしていた中で、サブクラスを作成している私のproject.Iにrealestateという名前のスーパークラスであり、私のJButton。ここ は私のサブクラスである:これは私の完全なコードです Package 'zz' does not existなど :私は間違って

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(); 
    } 

} 

何をやっている私はこのプログラムを実行

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

お尻は私のような異なるタイプのエラーを得ました?

+0

上記はコンパイルされていません。 – GOXR3PLUS

+0

OK wait a min GoX – Mcolo

+0

あなたはいくつかの選択肢があります: 'zz'がサブクラス( 'public'、' protected'またはパッケージ可視 'field')に表示されていることを確認するか、スーパークラスにゲッターを追加してください。 – c0der

答えて

0
  • ファイルRealState.javaに次のクラスを置く:

    public class RealEstate extends JFrame { 
    
        protected JButton zz = null; 
    
        RealEstate() { 
         JLabel l = new JLabel("Label"); 
         l.setBounds(100, 50, 300, 250); 
         add(l); 
    
         // ImageIcon ss = new ImageIcon("path_to_image_file"); 
         zz = new JButton("MyButton"); 
         zz.setBounds(470, 70, 35, 35); 
         zz.setBackground(new Color(0, 170, 120)); 
         add(zz); 
        } 
    } 
    
  • とサブクラスを置きますそれ自身のファイルでも:Assan.java

    public class Assan extends RealEstate { 
    
        public Assan() { 
         zz.addActionListener(new ActionListener(){ 
          public void actionPerformed(ActionEvent o) { 
           System.out.println("Cena"); 
          } 
         }); 
        } 
    
    
        public static void main(String[] args) { 
         Assan v = new Assan(); 
         v.setUndecorated(true); 
         v.setVisible(true); 
         v.setBounds(350, 200, 600, 350); 
         v.setForeground(Color.WHITE); 
        } 
    } 
    
0123私は2つの変更を行った

注:

  • はあなたがイベントハンドラが実行されるようにしたい場合は、サブクラスにサブクラスでprotected
  • 移動mainメソッドを変更したい変数を作ります。
  • そして最後に、あなたは一例
    1. Hereのために、あなたが相続
    2. Java Swingチュートリアルについて学びます、いくつかのチュートリアルに従わなければなりません。
+0

のujuluを参照してください – Mcolo

1

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); 
    } 
} 
+0

GoXR3Plus Jbuttonで公開したときにエラーが表示される: '不正な式の開始' – Mcolo

+0

l.add(ボタン)の意味/目的は何ですか? – c0der

+0

@Mcolo私はコードを編集しました。それは必要とレイアウトが正しく動作するために必要です。私はコンピュータに戻ったときに完全な例を提供します:) – GOXR3PLUS

0

を持っています。コードはいくつかのエラーを強調するだけでなく、MCVEのアイデアを実証するだけでなく、意図されています

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

import javax.swing.JButton; 
import javax.swing.JFrame; 

//Java is case sensetive. You can't call a class Realestate 
//and later use RealEstate v= new RealEstate(); 
public class RealEstate /*Realestate*/ extends JFrame { 

    //to make zz visible to other classes it should be a field 
    public JButton zz; 

    RealEstate() 
    { 
     //remove what is not needed to demonstrate the problem, remove it 
     //JLabel l 
     //ImageIcon ss=new ImageIcon("C:\\Users\\MUHAMMAD SHAHAB\\Documents\\NetBeansProjects\\Real Estate\\src\\real\\estate\\1470672145_Help_mark_query_question_support_talk.png"); 

     //wrong 
     //public JButton zz=new JButton("Button"); 
     zz=new JButton("Button"); 
     zz.setBounds(470,70,35,35); 
     zz.setBackground(new Color(0,170,120)); 

     add(zz); 
     //you can't add a button to a JLabel 
     //l.add(zz); 
    } 

    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(); 
    } 
} 

//Use right naming convention. Assan and not assan 
class Assan extends RealEstate{ 

    //this code has to be enclosed in a constructor 
    //or a method 
    //zz.addActionListener(new ActionListener(){ 
    // public void actionPerformed(ActionEvent o) 
    // { 
    //  System.out.println("Cena"); 
    // } 
    //}); 

    Assan(){ 

     zz.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent o) 
      { 
       System.out.println("Cena"); 
      } 
     }); 
    } 
} 
+0

私は上記のコードを実行しましたが、私は ' 。 – Mcolo

+0

私はまだ同じ例外を受けています。 – Mcolo

+0

このコードは例外を生成しません。私はそれを表示するために 'add(zz);'を追加しましたが、コードは正しいソリッドな作業コードではありません。あなたはサブクラスから 'JButton'にアクセスすることについて尋ねました。あなたはいくつかの有効な答えを得ました。その他の問題/質問については、新しい質問を投稿する必要があります(***には*** [MCVE]が含まれます)。また、http://stackoverflow.com/help/someone-answers – c0der

関連する問題