Vererbung (Java)

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen

Programm 1

<code=java>     
     

            

           
      
      

                                                                                               </code=java>

<code=java>import javax.swing.*;

import java.awt.*;

public class myProgram {

 public static void main(String[] args) {
   JFrame f = new JFrame("My Window");
   JPanel p = new JPanel();
   JButton b1 = new JButton("Button 1");
   JButton b2 = new JButton("Button 2");
   JButton b3 = new JButton("Button 3");
   b1.setBackground(new Color(255,0,0));
   b3.setBackground(new Color(255,0,0));
   p.add(b1);
   p.add(b2);
   p.add(b3);
   f.add(p);
   f.pack();
   f.setVisible(true);
 }

}</code=java>



Programm 2


<code=java>import javax.swing.*;

public class myFrame extends JFrame{

 myFrame(String name) {
   super(name);
 }

}                                                                                                                               </code=java>

<code=java>import javax.swing.*;

import java.awt.*;

public class myProgram {

 public static void main(String[] args) {
   myFrame f = new myFrame("My Window");
   JPanel p = new JPanel();
   JButton b1 = new JButton("Button 1");
   JButton b2 = new JButton("Button 2");
   JButton b3 = new JButton("Button 3");
   b1.setBackground(new Color(255,0,0));
   b3.setBackground(new Color(255,0,0));
   p.add(b1);
   p.add(b2);
   p.add(b3);
   f.add(p);
   f.pack();
   f.setVisible(true);
 }

}</code=java>






Programm 3


<code=java>import javax.swing.*;

import java.awt.*;

public class myFrame extends JFrame{

 myFrame() {
   super("My Window");
   JPanel p = new JPanel();
   JButton b1 = new JButton("Button 1");
   JButton b2 = new JButton("Button 2");
   JButton b3 = new JButton("Button 3");
   b1.setBackground(new Color(255,0,0));
   b3.setBackground(new Color(255,0,0));
   p.add(b1);
   p.add(b2);
   p.add(b3);
   this.add(p);
   this.pack();
   this.setVisible(true);
 }

}</code=java>

<code=java>public class myProgram {
 public static void main(String[] args) {
   myFrame f = new myFrame();
 }

}                                                                                           </code=java>






Programm 4


<code=java>import javax.swing.*;

import java.awt.*;

public class myFrame extends JFrame{

 myFrame() {
   super("My Window");
   JPanel p = new JPanel();
   JButton b1 = new JButton("Button 1");
   JButton b2 = new JButton("Button 2");
   JButton b3 = new JButton("Button 3");
   b1.setBackground(new Color(255,0,0));
   b3.setBackground(new Color(255,0,0));
   p.add(b1);
   p.add(b2);
   p.add(b3);
   this.add(p);
   this.pack();
   this.setVisible(true);
 }
 public static void main(String[] args) {
   myFrame f = new myFrame();
 }

}</code=java>

<code=java>     

                                                                                                                                                     </code=java>




Programm 5


<code=java>import javax.swing.*;

import java.awt.*;

public class RedButton extends JButton {

 RedButton(String name) {
   super(name);
   this.setBackground(new Color(255,0,0));
 }

}                                                 </code=java>

<code=java>import javax.swing.*;

public class myProgram {

 public static void main(String[] args) {
   myFrame f = new myFrame("My Window");
   JPanel p = new JPanel();
   RedButton b1 = new RedButton("Button 1");
   JButton b2 = new JButton("Button 2");
   RedButton b3 = new RedButton("Button 3");
   p.add(b1);
   p.add(b2);
   p.add(b3);
   f.add(p);
   f.pack();
   f.setVisible(true);
 }

}</code=java>




Programm 6


<code=java>import javax.swing.*;

import java.awt.*;

public class RedButton extends JButton {

 RedButton(String name) {
   super(name);
   this.setBackground(new Color(255,0,0));
 }

}                                                                               </code=java>

<code=java>import javax.swing.*;

import java.awt.*;

public class myFrame extends JFrame{

 myFrame() {
   super("My Window");
   JPanel p = new JPanel();
   RedButton b1 = new RedButton("Button 1");
   JButton b2 = new JButton("Button 2");
   RedButton b3 = new RedButton("Button 3");
   p.add(b1);
   p.add(b2);
   p.add(b3);
   this.add(p);
   this.pack();
   this.setVisible(true);
 }
 public static void main(String[] args) {
   myFrame f = new myFrame();
 }

}</code=java>