Niedawno zacząłem swoją przygodę z javą, poznałem własnie obsługę zdarzeń i chciałem zrobić dla testu 3 przyciski:
- zmiana koloru tła
- zmiana koloru przycisku
- zmiana tekstu przycisku
Wszystko działa bez problemu z wyjątkiem punktu pierwszego [GreenButton], a najgorsze jest to, że nie dostaje żadnego errora.
Ogólnie cały program jest z poprzedniej zabawy z obrazami.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.imageio.ImageIO;
public class ObrazekPanel extends JPanel {
private BufferedImage discImage, netImage;
private int panelHeight;
private JButton tlo, przycisk, nazwa;
private JPanel obrazekPanel;
public ObrazekPanel() {
super();
obrazekPanel = this;
try {
File imageFile = new File("mu.png");
discImage = ImageIO.read(imageFile);
URL imageURL = new URL(
"http://www.newspl.pl/wp-content/uploads/2010/05/Real-Madryt.jpg");
netImage = ImageIO.read(imageURL);
} catch (IOException e) {
System.err.println("Błąd wczytania obrazka!");
e.printStackTrace();
}
panelHeight = netImage.getHeight();
Dimension dimension = new Dimension(
(netImage.getWidth() + discImage.getWidth()), panelHeight);
setPreferredSize(dimension);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(discImage, 0, 0, this);
g2d.drawImage(netImage, discImage.getWidth(), 0, this);
setLayout(null);
tlo = new GreenButton();
przycisk = new ColorButton();
nazwa = new TitleButton();
add(tlo);
add(przycisk);
add(nazwa);
tlo.setBounds(20, 300, 70, 30);
przycisk.setBounds(100, 300, 70, 30);
nazwa.setBounds(180, 300, 70, 30);
}
class GreenButton extends JButton implements ActionListener {
GreenButton() {
super("Green");
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
obrazekPanel.setBackground(Color.GREEN);
}
}
class ColorButton extends JButton implements ActionListener {
ColorButton() {
super("Button");
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
setBackground(Color.MAGENTA);
}
}
class TitleButton extends JButton implements ActionListener {
TitleButton() {
super("Title");
addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
setText("Done");
}
}
}
Nic mi już nie przychodzi do głowy... Wydaje mi się, że wszystko powinno być ok. Jak naciskam przycisk do zmiany tła to widoczna jest lekka zmiana cienia na jednym z obrazków.
Dla uzupełnienia dodam jeszcze źródło ramki i klasy testową
import javax.swing.*;
public class ObrazekFrame extends JFrame {
public ObrazekFrame() {
super("Program obrazkowy ver. 0.1");
JPanel obrazekPanel = new ObrazekPanel();
add(obrazekPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
import java.awt.EventQueue;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
new ObrazekFrame();
}
});
}
}