在java小程序中加载面板的内容
我试图将面板加载到java小程序中,但面板的内容不会填充。从下面的代码中可以看到,我设置了一个测试,以查看面板中的代码在哪里无法运行,测试结果表明getRootPane().add(MyLabel)是触发异常的代码行 下面包含了重新创建此问题所需的所有代码。有人能告诉我如何修改下面的代码,以便将面板的内容加载到小程序中吗 下面是TestApplet.java的代码:在java小程序中加载面板的内容,java,swing,applet,Java,Swing,Applet,我试图将面板加载到java小程序中,但面板的内容不会填充。从下面的代码中可以看到,我设置了一个测试,以查看面板中的代码在哪里无法运行,测试结果表明getRootPane().add(MyLabel)是触发异常的代码行 下面包含了重新创建此问题所需的所有代码。有人能告诉我如何修改下面的代码,以便将面板的内容加载到小程序中吗 下面是TestApplet.java的代码: import javax.swing.JApplet; import javax.swing.SwingUtilities; p
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);
}
}
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 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. ");
}
}
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
}
编辑:
根据目前给出的建议,我对代码进行了如下编辑。使用this.add确实会导致JLabel加载,但是,内部类仍然没有加载,我已将其添加到下面的代码中。此外,下面更改的代码不再触发异常;它只加载JLabel,而不加载内部类。关于如何加载内部类有什么建议吗
以下是NewTestApplet.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);
}
}
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 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. ");
}
}
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
}
导入java.awt.Canvas;
导入java.awt.Color;
导入java.awt.Graphics;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公共类TestPanel扩展了JPanel{
DrawingLines myDrawingLines=新的DrawingLines();
测试面板(){
System.out.println(“在构造函数中运行”);
JLabel myLabel=新JLabel(“你好世界”);
添加(myLabel);
添加(myDrawingLines);
myDrawingLines.repaint();
System.out.println(“仍在构造函数中运行”);
}
//重写绘制方法的内部类
类DrawingLines扩展画布{
int宽度、高度;
公共空间涂料(图g){
宽度=getSize().width;
高度=getSize()。高度;
g、 setColor(Color.green);
对于(int i=0;i<10;++i){
g、 抽绳(宽度、高度、i*宽度/10,0);
}
System.out.println(“磨合绘制方法”);
}
}//内类结束
}
根窗格为空,因为Jpanel尚未添加到任何组件。向面板根窗格添加内容,如下所示。。很脏 根窗格为空,因为Jpanel尚未添加到任何组件。向面板根窗格添加内容,如下所示。。很脏 将方法设置为:
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. ");
}
}
将方法设置为:
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. ");
}
}
让我们从头开始
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. ");
}
}
使用getRootPane
向其中添加组件是错误的。您不需要向根窗格添加任何内容。相反,您应该使用内容窗格,但这不是您正在尝试做的(或者应该在这个上下文中做的)
相反,您只需调用add
public class TestPanel extends JPanel{
TestPanel(){
System.out.println("Running in constructor. ");
JLabel myLabel = new JLabel("Hello World");
add(myLabel);
System.out.println("Still running in constructor. ");
}
}
然后将标签添加到TestPane
让我们看一下扩展
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
}
我强烈建议你看看
- 让我们从头开始
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. ");
}
}
使用getRootPane
向其中添加组件是错误的。您不需要向根窗格添加任何内容。相反,您应该使用内容窗格,但这不是您正在尝试做的(或者应该在这个上下文中做的)
相反,您只需调用add
public class TestPanel extends JPanel{
TestPanel(){
System.out.println("Running in constructor. ");
JLabel myLabel = new JLabel("Hello World");
add(myLabel);
System.out.println("Still running in constructor. ");
}
}
然后将标签添加到TestPane
让我们看一下扩展
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
}
我强烈建议你看看
e.printStackTrace()
或System.err.println(e)
而不是System.err
来显示实际的异常。如果我不得不猜测,我会说rootpane在构造阶段为null,而您只想这样做。add()在那个阶段,@AdelBoutros我刚刚尝试了System.err.println(e),给出的异常是:java.lang.reflect.InvocationTargetException给我们整个stacktrace@MarkusMikkolainen这是我添加System.err.println(e)时给出的全部打印输出。除非您有建议,否则不确定如何获取更多堆栈跟踪。我正在使用eclipse。而不是捕获中的System.err
,而是执行e.printStackTrace()
或System.err.println(e)
来显示实际的异常。如果我不得不猜测,我会说rootpane在构造阶段为null,而您只想这样做。add()在那个阶段,@AdelBoutros我刚刚尝试了System.err.println(e),给出的异常是:java.lang.reflect.InvocationTargetException给我们整个stacktrace@MarkusMikkolainen这是我添加System.err.println(e)时给出的全部打印输出。除非您有建议,否则不确定如何获取更多堆栈跟踪。我正在使用eclipse。谢谢。你能建议一个更干净的方法来添加东西吗?这是一个Java应用程序,我正在将其转换为小程序。eclipse让我将getContentPane()切换到getRootPane()。好的。施工后进行添加,或直接添加到面板,或直接添加到框架。+1表示帮助。我已经在上面我的原始帖子的编辑中重申了我的问题。你能告诉我如何在我上面编辑过的代码中获得内部类来显示在面板中吗?我保持上面的代码简单,但是原始代码太过简化了问题。显示内部类应该是我问题的范围。如果这是可行的,我会考虑这个问题的答案。为什么不重写面板中的一个绘画方法呢?为什么是一个单独的内部类?我也不会使用画布,因为它是一个AWT组件,在swing组件中。您只需修改一个swing组件即可进行不同的绘制。谢谢。你能建议一个更干净的方法来添加东西吗?这是一个Java应用程序,我正在将其转换为小程序。eclipse让我将getContentPane()切换到getRootPane()。好的。