简体   繁体   中英

I can't see my JPanel and its components in the JApplet

I want to put a JPanel in a JApplet , the problem is that I can't see it:( I've overridden the paintComponent of my JPanel in order to have a background image, but I can't see anything. When I remove the paintComponenet method that I had overriden, and set a color to the background of this panel, it seems that JPanel fills the JApplet and still no component is visible:-S I've tried different layouts. I also put my panel in the center of another panel which fills my JApplet but nothing changed, and still no component and no background image is visible:(

import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class Main extends JApplet implements Runnable{

private JTextArea display;
private Thread outputThread;
JPanel boardPanel;

private ClientViewManager view;

@Override
public void init() {

    try {
        javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                createGUI();
            }
        });
    } catch (Exception e) {
        System.err.println("createGUI didn't successfully complete");
    }

}

private void createGUI() {

    display = new JTextArea(4, 30);
    display.setEditable(false);
    getContentPane().add(new JScrollPane(display), BorderLayout.SOUTH);

    setFocusable(true);
    setVisible(true);
    setName("CE Tanks");
    setSize(600, 600);
    setLocation(100, 100);

    boardPanel = new JPanel();
    boardPanel.setLayout(null);
    boardPanel.setBackground(new java.awt.Color(128, 255, 255));
    getContentPane().add(boardPanel, BorderLayout.CENTER);

}

public void start() {
    outputThread = new Thread(this);
    outputThread.start();
}

public void run() {
                view = new ClientViewManager();
                boardPanel.add(view);
                boardPanel.repaint();
                repaint();
    }
}


 class ClientViewManager extends JPanel {
private int rows=8;
private int columns=8;

public ClientViewManager() {
    super(null);

    JLabel lb= new JLabel("lb.jpg");
    lb.setLocation(10, 10);
    lb.setSize(50, 50);
    lb.setOpaque(false);
    lb.setVisible(true);

    this.add(lb);
}

public void paintComponent(Graphics g) {

    g.drawImage(new ImageIcon("ground.jpg").getImage(), 0, 0, columns * 50,
            rows * 50, this);
}

}

The code above can be compiled. I cant even add Keylistener to neither my JPanel nor to my JApplet . I used java.awt.KeyEventDispatcher and in dispatchKeyEvent(KeyEvent e) I printed something in console but, it was printed 3 times. :(

I've overridden the paintComponent of my JPanel inorder to have a background image,

But you didn't add the custom component to your applet:

//boardPanel = new JPanel();
boardPanel = new ClientViewManager();

Also:

  1. get rid of setVisible(). This is not required for any of the controls in your program. By default all components except top level Container (Jframe, JDialog etc) are already visible. In the case of JApplet, you don't need to make it visible as this is part of the process of displaying an applet.
  2. get rid of setSize() and setLocation() you can't control the position of the applet this way.
  3. Don't read the image file in the paintComponent() method. This is not efficient as this method is invoked whenever Swing determines the component needs to be repainted.
  4. JLabels are opaque by default so there is not need to invoke the setOpaque method.
  5. When doing custom painting you should also override the getPreferredSize() method of the component to return the proper size of the custom painting so layout managers can use this information. It works in this case because you added the panel to the CENTER of the BorderLayout. Try adding the panel to the NORTH to see what happens.

Edit:

Now I see where you are adding the ClientViewManager. I'm not sure why you are trying to do this with a Thread but once again there are several problems.

  1. When you add/remove components from a visble GUI then the basic code is:

    panel.add(...);
    panel.revalidate();
    panel.repaint();

  2. However this still won't work because you are using a null layout and the size of the panel is 0. Use a proper layout manager and implement the getPreferredSize() method as suggest above and the component will be displayed properly.

I recommend you to use the GUI Builder of Netbeans to build a GUI like that, and then compare the generated code to your code. Netbeans results really useful to help you create swing code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM