我正在尝试将面板加载到 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
}
最佳答案
设置方法为:
private void createGUI(){
TestPanel myPanel = new TestPanel();
getContentPane().add(myPanel);
}
TestPanel 类为
public class TestPanel extends JPanel{
TestPanel(){
super();
System.out.println("Running in constructor. ");
JLabel myLabel = new JLabel("Hello World");
add(myLabel);
System.out.println("Still running in constructor. ");
}
}
关于java - 在java小程序中加载面板内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15373997/