简体   繁体   English

我在 JApplet 中看不到我的 JPanel 及其组件

[英]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:(我想将JPanel放在JApplet中,问题是我看不到它:( 我已经覆盖了JPanelpaintComponent以获得背景图像,但我什么都看不到。当我删除我已经覆盖的paintComponenet方法,并为此面板的背景设置了颜色,似乎JPanel填充了JApplet并且仍然没有可见的组件:-S我尝试了不同的布局。我也把我的面板放在了中心填充我的JApplet但没有任何改变的另一个面板,仍然没有可见的组件和背景图像:(

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 .我什至不能将Keylistener添加到我的JPanel和我的JApplet中。 I used java.awt.KeyEventDispatcher and in dispatchKeyEvent(KeyEvent e) I printed something in console but, it was printed 3 times.我使用java.awt.KeyEventDispatcherdispatchKeyEvent(KeyEvent e)我在控制台打印了一些东西,但是打印了 3 次。 :( :(

I've overridden the paintComponent of my JPanel inorder to have a background image,我已经覆盖了我的 JPanel 的paintComponent 以获得背景图像,

But you didn't add the custom component to your applet:但是您没有将自定义组件添加到您的小程序中:

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

Also:还:

  1. get rid of setVisible().摆脱 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.默认情况下,除了顶级容器(Jframe、JDialog 等)之外的所有组件都已经可见。 In the case of JApplet, you don't need to make it visible as this is part of the process of displaying an applet.对于 JApplet,您不需要使其可见,因为这是显示小程序的过程的一部分。
  2. get rid of setSize() and setLocation() you can't control the position of the applet this way.摆脱 setSize() 和 setLocation() 你不能用这种方式控制小程序的 position。
  3. Don't read the image file in the paintComponent() method.不要在paintComponent() 方法中读取图像文件。 This is not efficient as this method is invoked whenever Swing determines the component needs to be repainted.这效率不高,因为只要 Swing 确定组件需要重新绘制,就会调用此方法。
  4. JLabels are opaque by default so there is not need to invoke the setOpaque method. JLabel 默认是不透明的,因此不需要调用 setOpaque 方法。
  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.在进行自定义绘画时,您还应该覆盖组件的 getPreferredSize() 方法以返回自定义绘画的正确大小,以便布局管理器可以使用此信息。 It works in this case because you added the panel to the CENTER of the BorderLayout.它在这种情况下有效,因为您将面板添加到 BorderLayout 的 CENTER。 Try adding the panel to the NORTH to see what happens.尝试将面板添加到 NORTH 以查看会发生什么。

Edit:编辑:

Now I see where you are adding the ClientViewManager.现在我看到您在哪里添加 ClientViewManager。 I'm not sure why you are trying to do this with a Thread but once again there are several problems.我不确定您为什么要尝试使用 Thread 执行此操作,但再次存在几个问题。

  1. When you add/remove components from a visble GUI then the basic code is:当您从可见 GUI 添加/删除组件时,基本代码是:

    panel.add(...);面板.add(...);
    panel.revalidate(); panel.revalidate();
    panel.repaint(); 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.但是这仍然不起作用,因为您使用的是 null 布局并且面板的大小为 0。使用适当的布局管理器并按照上面的建议实现 getPreferredSize() 方法,组件将正确显示。

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 的 GUI Builder 来构建这样的 GUI,然后将生成的代码与您的代码进行比较。 Netbeans results really useful to help you create swing code. Netbeans 结果对帮助您创建 swing 代码非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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