我正在尝试将面板加载到 java 小程序中,但面板的内容没有填充。从下面的代码中可以看到,我设置了一个测试,看看面板中的代码在哪里运行失败,我的测试结果表明getRootPane().add(MyLabel)是触发的代码行例外。
重新创建此问题所需的所有代码都包含在下面。谁能告诉我如何更改下面的代码,以便将面板的内容加载到小程序中?
下面是 TestApplet.java 的代码:
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
public class TestApplet extends JApplet {
public void init(){//Called when this applet is loaded into the browser.
//Execute a job on the event-dispatching thread; creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
private void createGUI(){
TestPanel myPanel = new TestPanel();
myPanel.setOpaque(true);
setContentPane(myPanel);
}
}
这是 TestPanel.java 的代码:
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestPanel extends JPanel{
TestPanel(){
System.out.println("Running in constructor. ");
JLabel myLabel = new JLabel("Hello World");
getRootPane().add(myLabel);
System.out.println("Still running in constructor. ");
}
}
编辑:
根据迄今为止给出的建议,我按如下方式编辑了我的代码。使用 this.add 确实会导致 JLabel 加载,但是,内部类仍未加载,我已将其添加到下面的代码中。此外,下面更改的代码不再触发异常;它只加载 JLabel 但不加载内部类。关于如何加载内部类的任何建议?
这是新的 TestApplet.java:
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
public class TestApplet extends JApplet {
public void init(){//Called when this applet is loaded into the browser.
//Execute a job on the event-dispatching thread; creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
System.err.println(e);
e.printStackTrace();
}
}
private void createGUI(){
TestPanel myPanel = new TestPanel();
myPanel.setOpaque(true);
setContentPane(myPanel);
}
}
这是新的 TestPanel.java:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestPanel extends JPanel{
DrawingLines myDrawingLines = new DrawingLines();
TestPanel(){
System.out.println("Running in constructor. ");
JLabel myLabel = new JLabel("Hello World");
this.add(myLabel);
this.add(myDrawingLines);
myDrawingLines.repaint();
System.out.println("Still running in constructor. ");
}
//inner class to override paint method
class DrawingLines extends Canvas{
int width, height;
public void paint( Graphics g ) {
width = getSize().width;
height = getSize().height;
g.setColor( Color.green );
for ( int i = 0; i < 10; ++i ) {
g.drawLine( width, height, i * width / 10, 0 );
}
System.out.println("Running in paint method.");
}
}//end of inner class
}