Chcę dodać w oknie about (window2) swoje zdjęcie i mam mały problem, bo coś mi to nie chce działać. Referencja do pliku jest teoretycznie robiona, bo obsługa błędów nic nie wyłapuje. Może ktoś pomóc to zmodyfikować?
Z góry thx.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
/* Eventy dla okna - componentListener - zmiana rozmiaru */
class window1Events extends calc implements ComponentListener {
private int width;
private int height;
window1Events() {
}
public void getStats() {
width = window1.getWidth();
height = window1.getHeight();
}
public void componentResized(ComponentEvent ev) {
getStats();
but0.setBounds(10, 140, 50, 50);
but1.setBounds(10, 195, 50, 50);
but2.setBounds(65, 140, 105, 105);
butAdd.setBounds(10, 85, 50, 50);
butSub.setBounds(65, 85, 50, 50);
butMul.setBounds(120, 85, 50, 50);
butDiv.setBounds(175, 85, 50, 50);
butMod.setBounds(230, 85, 50, 50);
butCE.setBounds(175, 140, 105 - (2*(300-width)/5), 105);
if(width < minWidth || height < minHeight) {
Object[] options = {"Zamknij program", "Zmień rozmiar okna"};
int n = JOptionPane.showOptionDialog(window1,
"Zmieniono rozmiar okna na nieprawidlowy!\n" +
"Wybierz opcję:",
"Error",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //zwykła ikona
options, //tytuły przycisków
options[0]); //domyślny tytuł przycisku
if(n == 0) System.exit(0);
else if(n == 1) window1.setSize(minWidth, minHeight);
}
}
public void componentMoved(ComponentEvent ev) {
}
public void componentShown(ComponentEvent ev) {
}
public void componentHidden(ComponentEvent ev) {
}
}
class menuEvents extends calc implements ItemListener, ActionListener {
//private JFrame window2 = new JFrame("About...");
menuEvents() {
}
public void itemStateChanged(ItemEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void actionPerformed(ActionEvent e) {
window2.setBounds(450, 270, 200, 200);
window2.setVisible(true);
repaint();
}
}
/* Główna klasa */
public class calc extends JApplet {
/* Główne obiekty i zmienne */
public static int minWidth = 150;
public static int minHeight = 150;
public static JFrame window1 = new JFrame("Calc 1.0 - kalkulator binarny");
public static JPanel mainPanel = new JPanel();
public static JButton but0 = new JButton("0");
public static JButton but1 = new JButton("1");
public static JButton but2 = new JButton("=");
public static JButton butAdd = new JButton("+");
public static JButton butSub = new JButton("-");
public static JButton butDiv = new JButton("/");
public static JButton butMul = new JButton("*");
public static JButton butMod = new JButton("%");
public static JButton butCE = new JButton("CE");
public static JMenuBar menuBar = new JMenuBar();
public static JMenu menu;
public static JMenuItem menuItem;
private BufferedImage nickon;
public static JFrame window2 = new JFrame("About...");
public static JPanel secPanel = new JPanel();
public void paint(Graphics gdc) {
Insets i = secPanel.getInsets();
Dimension d = secPanel.getSize();
gdc.drawImage(nickon, i.left, i.top, d.width - i.left - i.right, d.height - i.top - i.bottom, secPanel);
}
public void init() {
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window1.setBounds(400, 200, 300, 310);
window1.setVisible(true);
window1.setResizable(true);
window1.toFront();
window1.add(mainPanel, BorderLayout.CENTER);
window2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
window2.setVisible(false);
window2.setBounds(450, 270, 200, 200);
window2.setAlwaysOnTop(true);
window2.setResizable(false);
window2.add(secPanel, BorderLayout.CENTER);
secPanel.setBounds(0, 0, 200, 200);
try {
nickon = ImageIO.read(new File("images/nickon.jpg"));
}
catch (IOException ex) {
System.out.println("KUUUUURWA");
}
mainPanel.setLayout(null);
mainPanel.add(but0);
mainPanel.add(but1);
mainPanel.add(but2);
mainPanel.add(butAdd);
mainPanel.add(butSub);
mainPanel.add(butDiv);
mainPanel.add(butMul);
mainPanel.add(butMod);
mainPanel.add(butCE);
menu = new JMenu("Pomoc");
menuBar.add(menu);
menuItem = new JMenuItem("About...", new ImageIcon("images/info.gif"));
menuItem.addActionListener(new menuEvents());
menu.add(menuItem);
window1.setJMenuBar(menuBar);
but0.setBounds(10, 150, 50, 50);
but1.setBounds(10, 205, 50, 50);
but2.setBounds(65, 150, 105, 105);
butAdd.setBounds(10, 95, 50, 50);
butSub.setBounds(65, 95, 50, 50);
butMul.setBounds(120, 95, 50, 50);
butDiv.setBounds(175, 95, 50, 50);
butMod.setBounds(230, 95, 50, 50);
butCE.setBounds(175, 150, 105, 105);
/* event dla wielkości okienka */
window1.addComponentListener(new window1Events());
}
public void start() {
}
public void stop() {
}
public static void main(String[] args) {
final calc applet = new calc();
//aby uniknąć zakleszczeń tworzenie GUI zawsze zlecamy dla wątku obsługi zdarzeń
javax.swing.SwingUtilities.invokeLater(
new Runnable() {
public void run() {
applet.init();
applet.start();
}
}
);
}
}