0

I'm having a problem with my About Frame class. I call it through an action listener in a JMenuItem. It comes up but it doesn't show it centered and the frame doesn't show the icon image as I request. I have the icon working in the mainframe so it's the image size isn't the problem. Does this have something to do with the private JPanel contentPane;?

Code in the JmenuItem:

aboutMItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jAboutMN_actionPerformed(e);

About Frame code:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BoxLayout;


public class AboutFrame extends JFrame {



private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                AboutFrame frame = new AboutFrame();
                frame.setIconImage(new ImageIcon("resources/Image.png").getImage());
                frame.setVisible(true);

                //frame.setLocationRelativeTo(null);
                frame.setLocationRelativeTo(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public AboutFrame() {
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    //AboutFrame.setLocationRelativeTo(null); 
    setTitle("About MyAPP");
    setBounds(100, 100, 370, 250);
    //contentPane.setLocationRelativeTo(null);

    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    JLabel lbVerNum = new JLabel("MYAPP,  Beta 1.2");
    lbVerNum.setBounds(57, 11, 233, 19);
    lbVerNum.setVerticalAlignment(SwingConstants.TOP);
    lbVerNum.setFont(new Font("Tahoma", Font.BOLD, 15));
    lbVerNum.setHorizontalAlignment(SwingConstants.CENTER);
    contentPane.add(lbVerNum);

    JLabel lbCopyright = new JLabel("Copyright 2015 ");
    lbCopyright.setBounds(86, 41, 170, 16);
    lbCopyright.setIcon(null);
    lbCopyright.setFont(new Font("Tahoma", Font.PLAIN, 13));
    lbCopyright.setVerticalAlignment(SwingConstants.TOP);
    lbCopyright.setHorizontalAlignment(SwingConstants.CENTER);
    contentPane.add(lbCopyright);


}
}
javajoejuan
  • 323
  • 3
  • 9
  • 1
    Sorry, but I see so many issues with that code including your using an application window (a JFrame) where a dialog window (JDialog) is absolutely called for, your using null layouts absolute positioning and sizing of components, your getting images as files rather than as resources... It's like nails on a chalkboard for me. – Hovercraft Full Of Eels May 20 '16 at 23:53
  • Thanks for your comments, It runs fine when I run the class by itself. Any thoughts on why? – javajoejuan May 20 '16 at 23:56
  • `new ImageIcon("resources/Image.png")` should probably be `new ImageIcon(AboutFrame.class.getResource("/resources/Image.png"))` or `new ImageIcon(AboutFrame.class.getResource("resources/Image.png"))` – MadProgrammer May 21 '16 at 00:03
  • I'd also consider using [`JFrame#setIconImages`](https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setIconImages(java.util.List)) which will provide better OS intergration – MadProgrammer May 21 '16 at 00:04
  • 2
    You might consider using `setLocationRelativeTo` to have the child frame centered within the parent frame ... also, `null` layouts are a bad, bad idea – MadProgrammer May 21 '16 at 00:05

0 Answers0