Aplety w javie

0

Witam,

Jestem początkującym programistą, obecnie uczę się tworzenia apletów w javie.
W internecie większość poradników obejmuje JFrame, mi jednak chodzi o aplety.

Mam 2 pytania, będę niezmiernie wdzięczny jeśli ktoś mi pomoże.

a) W jaki sposób mogę ustawić obramowanie przycisków oraz co zrobić aby przycisk po najechaniu na niego kursorem myszy zmienił swój kolor tła?
b) Chciałbym aby mój aplet był obiektowy, dlatego chcę stworzyć osobną klasę odpowiedzialną za grafikę, jak to zrobić ?

Oto mój kod :

 import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Font;

public class Kalkulator extends Applet implements MouseListener, ActionListener{
    
    Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bplus,bminus,bpomnoz,bpodziel,bpoteguj,bpierwiastkuj,bc,bkropka,bwynik;
    Label ekran,tlo;
    double wynik;
    
    Grafika graf = new Grafika();
    
    public void init() {
        b0 = new Button("0");
        add(b0);
        b0.addActionListener(this);
        b1 = new Button("1");
        add(b1);
        b1.addActionListener(this);
        b2 = new Button("2");
        add(b2);
        b2.addActionListener(this);
        b3 = new Button("3");
        add(b3);
        b3.addActionListener(this);
        b4 = new Button("4");
        add(b4);
        b4.addActionListener(this);
        b5 = new Button("5");
        add(b5);
        b5.addActionListener(this);
        b6 = new Button("6");
        add(b6);
        b6.addActionListener(this);
        b7 = new Button("7");
        add(b7);
        b7.addActionListener(this);
        b8 = new Button("8");
        add(b8);
        b8.addActionListener(this);
        b9 = new Button("9");
        add(b9);
        b9.addActionListener(this);
        bplus = new Button("+");
        add(bplus);
        bplus.addActionListener(this);
        bminus = new Button("-");
        add(bminus);
        bminus.addActionListener(this);
        bpomnoz = new Button("*");
        add(bpomnoz);
        bpomnoz.addActionListener(this);
        bpodziel = new Button("/");
        add(bpodziel);
        bpodziel.addActionListener(this);
        bpoteguj = new Button("^");
        add(bpoteguj);
        bpoteguj.addActionListener(this);
        bpierwiastkuj = new Button("//");
        add(bpierwiastkuj);
        bpierwiastkuj.addActionListener(this);
        bkropka = new Button(".");
        add(bkropka);
        bkropka.addActionListener(this);
        bc = new Button("C");
        add(bc);
        bc.addActionListener(this);
        bwynik = new Button("=");
        add(bwynik);
        bwynik.addActionListener(this);

        ekran = new Label("" + wynik, Label.RIGHT);
        add(ekran);
        tlo = new Label("");
        add(tlo);
    }
    
    public void mouseClicked(java.awt.event.MouseEvent evt) { }
    public void mouseReleased(java.awt.event.MouseEvent evt) {} 
    public void mouseEntered(java.awt.event.MouseEvent evt) { }  
    public void mouseExited(java.awt.event.MouseEvent evt) {}
    public void mousePressed(java.awt.event.MouseEvent evt) {}
    
    public void paint(Graphics g) {
        
       setBackground(new Color(30,30,30));
       
       b7.setBounds(30,80,40,25);
       b8.setBounds(80,80,40,25);
       b9.setBounds(130,80,40,25);
       b4.setBounds(30,115,40,25);
       b5.setBounds(80,115,40,25);
       b6.setBounds(130,115,40,25);
       b1.setBounds(30,150,40,25);
       b2.setBounds(80,150,40,25);
       b3.setBounds(130,150,40,25);
       bc.setBounds(30,185,40,25);
       b0.setBounds(80,185,40,25);
       bkropka.setBounds(130,185,40,25);
       bplus.setBounds(220,80,40,25);
       bminus.setBounds(270,80,40,25);
       bpomnoz.setBounds(220,115,40,25);
       bpodziel.setBounds(270,115,40,25);
       bpoteguj.setBounds(220,150,40,25);
       bpierwiastkuj.setBounds(270,150,40,25);
       bwynik.setBounds(220,185,90,25);
       ekran.setBounds(30,30,280,35);
       tlo.setBounds(20,20,300,200);
       
       Color czer = new Color(255,33,33);
       Color pom = new Color(255,99,0);
       
       b0.setBackground(czer);
       b1.setBackground(czer);
       b2.setBackground(czer);
       b3.setBackground(czer);
       b4.setBackground(czer);
       b5.setBackground(czer);
       b6.setBackground(czer);
       b7.setBackground(czer);
       b8.setBackground(czer);
       b9.setBackground(czer);
       bplus.setBackground(pom);
       bminus.setBackground(pom);
       bpomnoz.setBackground(pom);
       bpodziel.setBackground(pom);
       bpoteguj.setBackground(pom);
       bpierwiastkuj.setBackground(pom);
       bkropka.setBackground(pom);
       bc.setBackground(pom);
       bwynik.setBackground(pom);
       ekran.setBackground(new Color(235,235,235));
       tlo.setBackground(new Color(66,66,66));
       
       b0.setForeground(Color.white);
       b1.setForeground(Color.white);
       b2.setForeground(Color.white);
       b3.setForeground(Color.white);
       b4.setForeground(Color.white);
       b5.setForeground(Color.white);
       b6.setForeground(Color.white);
       b7.setForeground(Color.white);
       b8.setForeground(Color.white);
       b9.setForeground(Color.white);
       bplus.setForeground(Color.white);
       bminus.setForeground(Color.white);
       bpomnoz.setForeground(Color.white);
       bpodziel.setForeground(Color.white);
       bpoteguj.setForeground(Color.white);
       bpierwiastkuj.setForeground(Color.white);
       bc.setForeground(Color.white);
       bkropka.setForeground(Color.white);
       bwynik.setForeground(Color.white);
       ekran.setForeground(new Color(19,19,19));
       
       b0.setFont(new Font("SansSerif", Font.PLAIN, 16));
       b1.setFont(new Font("SansSerif", Font.PLAIN, 16));
       b2.setFont(new Font("SansSerif", Font.PLAIN, 16));
       b3.setFont(new Font("SansSerif", Font.PLAIN, 16));
       b4.setFont(new Font("SansSerif", Font.PLAIN, 16));
       b5.setFont(new Font("SansSerif", Font.PLAIN, 16));
       b6.setFont(new Font("SansSerif", Font.PLAIN, 16));
       b7.setFont(new Font("SansSerif", Font.PLAIN, 16));
       b8.setFont(new Font("SansSerif", Font.PLAIN, 16));
       b9.setFont(new Font("SansSerif", Font.PLAIN, 16));
       bplus.setFont(new Font("SansSerif",Font.BOLD,16));
       bminus.setFont(new Font("SansSerif",Font.BOLD,16));
       bpomnoz.setFont(new Font("SansSerif",Font.BOLD,16));
       bpodziel.setFont(new Font("SansSerif",Font.BOLD,16));
       bpoteguj.setFont(new Font("SansSerif",Font.BOLD,16));
       bpierwiastkuj.setFont(new Font("SansSerif",Font.BOLD,16));
       bkropka.setFont(new Font("SansSerif",Font.BOLD,16));
       bc.setFont(new Font("SansSerif",Font.BOLD,16));
       bwynik.setFont(new Font("SansSerif",Font.BOLD,16));
       ekran.setFont(new Font("SansSerif",Font.BOLD,24));
    }
    
    
    
    public void actionPerformed(java.awt.event.ActionEvent zdarzenie) {
       
    }
}
0

Pierwsza (i najważniejsza) rada - przestań się uczyć pisania apletów, aplety są na wymarciu.
Druga rada - jeśli musisz się uczyć, to skorzystaj z klas JApplet, JLabel, JButton.
Dodaj do każdego przycisku MouseListenera i oprogramuj jego metodę mouseEntered

0

ja dodam jeszcze jedno - nazywaj sensowniej Buttons itp bo nazwa typu b1, b2 itp nic nie mówią a z czasem nie będziesz wiedział co czym jest jak będzie potrzeba modyfikacji czegokolwiek...

1 użytkowników online, w tym zalogowanych: 0, gości: 1