views:

270

answers:

4

Hi guys

I have a GUI window that I've created using Netbeans. I then ported the code into my own program so that I can display .png's at my will.

However, the GUI components are not displaying, and the window opens up with no size by default.

I need the window to initially open up with the GUI components visible, with the window of the correct size for everything to be visible.

Can anyone help me out?

thanks

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;

public class AwtImage extends javax.swing.JFrame {

    private Image img;

    // Variables declaration - do not modify                     
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration            

    public static void main(String[] args){
        AwtImage ai = new AwtImage();
    }

    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jCheckBox2 = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Not Logged In");
        getContentPane().setLayout(null);

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(jTextField1);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(0, 540, 170, 22);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(0, 440, 166, 96);

        jCheckBox2.setText("Sit Out Next Hand");
        jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox2ActionPerformed(evt);
            }
        });
        getContentPane().add(jCheckBox2);
        jCheckBox2.setBounds(0, 410, 113, 23);

        pack();
    }// </editor-fold>


    private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {                                     
        // TODO add your handling code here:
    }               



    public AwtImage() {
        super("Image Frame");
        MediaTracker mt = new MediaTracker(this);
        img = Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\TableAndChairs.png");
        mt.addImage(img,0);
        setSize(600,600);


        initComponents();
        setVisible(true);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we) {
                dispose();
            }
        });
    }

    public void update(Graphics g){
        paint(g);
    }

    public void paint(Graphics g) {
        if(img != null)
            g.drawImage(img, 0, 10, this);
       // else
        //    g.clearRect(0, 0, getSize().width, getSize().height);
    }
}

alt text alt text

+1  A: 

without seeing your code, it's near impossible to help you however:

it sounds like you are missing a call to pack on your Window/JFrame which would cause the window to calculate it's size based on it's content

and your UI components (checkbox + text boxes) are probably not showing up because you haven't added them correctly ie via the contents pane and a layout manager.

just a guess!

pstanton
A: 

I persume you are calling ...

.setSize(500,500);

on the main window?

if you know the size of the image, then I'd personally call...

.setPreferedSize(x,x);
.setMinimenSize(x,x);
.setSize(x,x);
.pack();

on the main window/panel.

can you post your code?

jeff porter
A: 

setSize() should be moved after initComponents()

But again, you wont need to port the code, you can edit the source code in Netbeans without difficulties, just keep initComponents folded.

medopal
+1  A: 

There are several problems with the code.

In addition to the other suggestions of using pack() or setting the frame size "after" adding the components to the frame and just before invoking the setVisible(true) method you need to look at the following.

The code is based on old AWT painting techniques, which should NOT be used with Swing. You should NEVER override the update() or paint() methods of the JFrame. The reason the child components are not painted is that the paint() mehtod is responsbile for painting them but you overrode this default behaviour.

When using Swing custom painting is done by overriding the paintComponent() method of a JComponent or JPanel, then you add this component to the content pane of the frame. Read the section from the Swing tutorial on Custom Painting for more information and working examples. One of the key points in the tutorial is to provide a preferred size for the component so it can be layed out properly by the layout manager.

You should NOT be using a KeyListener. Again, this is an old AWT technique. Swing now uses Key Bindings to map KeyStrokes to an Action. Read the section from the Swing tutorial on "How to Use Key Bindings" for more information.

You should NOT be using a WindowListener to close the frame again this is an old AWT technique. Swing applications now use the frame.setDefaultCloseOperation(...) method to control this.

The tutorial section on "How to Use Icons" shows you easier ways to load an image.

So overall I suggested you start by downloading the Swing tutorial to learn the Swing programming techniques. The tutorial covers the basicis and many simple examples to get you started.

camickr