我想將JPanel
放在JApplet
中,問題是我看不到它:(我已經覆蓋了我的JPanel
的paintComponent
以便有一個背景圖像,但我看不到任何東西當我刪除我已重寫的paintComponenet
方法,併爲此面板的背景設置顏色時,看起來JPanel
填充JApplet
,仍然沒有組件可見: - S我已經嘗試了不同的佈局,我也將面板放在另一個面板的中間,該面板填滿了我的JApplet
,但沒有任何變化,仍然沒有組件和無背景圖像可見:(我看不到我的JPanel及其組件在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);
}
}
上面的代碼可以編譯。我甚至不能將Keylistener
添加到我的JPanel
和我的JApplet
。我用java.awt.KeyEventDispatcher
和dispatchKeyEvent(KeyEvent e)
我在控制檯上打印了一些東西,但它被打印了3次。 :(
little bunny foo foo是對的。 – MirroredFate
@Maryam:*「Plzzzzzzzzz幫助」*請1)停止使用廢話拼寫,如「Plzzzzzzzzz」2)停止發送給我們。 3)問一個問題。 –