[英]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
中,问题是我看不到它:( 我已经覆盖了JPanel
的paintComponent
以获得背景图像,但我什么都看不到。当我删除我已经覆盖的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.KeyEventDispatcher
和dispatchKeyEvent(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:还:
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 执行此操作,但再次存在几个问题。
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();
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.