我的java applet有问题。 我正在尝试制作一个简单的Java小程序,看它是如何工作的。
当我将所有内容放在一个类中时,一切看起来都不错,但是当我将它分成其他类时,applet窗口什么都不显示:
头等舱:
package com.gmv.klinika;
import javax.swing.*;
/**
* Created by gumovvy on 27.11.14.
*/
public class mainClass extends JApplet {
OtherClass fr = new OtherClass();
public void init() {
fr.guiInit();
}
}
第二课:
package com.gmv.klinika;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Created by gumovvy on 27.11.14.
*/
public class OtherClass extends JFrame {
JButton jbtnOne;
JButton jbtnTwo;
JLabel jlab;
public void guiInit() {
// Set the applet to use flow layout.
setLayout(new FlowLayout());
// Create two buttons and a label.
jbtnOne = new JButton("One");
jbtnTwo = new JButton("Two");
jlab = new JLabel("Press a button.");
// Add action listeners for the buttons.
jbtnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Button One pressed.");
}
});
jbtnTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Button Two pressed.");
}
});
// Add the components to the applet's content pane.
getContentPane().add(jbtnOne);
getContentPane().add(jbtnTwo);
getContentPane().add(jlab);
}
}
你有任何想法吗?
------- ------- EDITED
原始课程如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
This HTML can be used to launch the applet:
<object code="MyApplet" width=240 height=100>
</object>
*/
public class MyApplet extends JApplet {
JButton jbtnOne;
JButton jbtnTwo;
JLabel jlab;
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
guiInit(); // initialize the GUI
}
});
} catch(Exception exc) {
System.out.println("Can't create because of "+ exc);
}
}
// Called second, after init(). Also called
// whenever the applet is restarted.
public void start() {
// Not used by this applet.
}
// Called when the applet is stopped.
public void stop() {
// Not used by this applet.
}
// Called when applet is terminated. This is
// the last method executed.
public void destroy() {
// Not used by this applet.
}
// Setup and initialize the GUI.
private void guiInit() {
// Set the applet to use flow layout.
setLayout(new FlowLayout());
// Create two buttons and a label.
jbtnOne = new JButton("One");
jbtnTwo = new JButton("Two");
jlab = new JLabel("Press a button.");
// Add action listeners for the buttons.
jbtnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Button One pressed.");
}
});
jbtnTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Button Two pressed.");
}
});
// Add the components to the applet's content pane.
getContentPane().add(jbtnOne);
getContentPane().add(jbtnTwo);
getContentPane().add(jlab);
}
}
答案 0 :(得分:1)
将您的OtherClass
更改为JPanel
而不是......
public class OtherClass extends JPanel {
JButton jbtnOne;
JButton jbtnTwo;
JLabel jlab;
public OtherClass() {
// Set the applet to use flow layout.
setLayout(new FlowLayout());
// Create two buttons and a label.
jbtnOne = new JButton("One");
jbtnTwo = new JButton("Two");
jlab = new JLabel("Press a button.");
// Add action listeners for the buttons.
jbtnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Button One pressed.");
}
});
jbtnTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Button Two pressed.");
}
});
// Add the components to the applet's content pane.
add(jbtnOne);
add(jbtnTwo);
add(jlab);
}
}
将OtherClass
添加到MainClass
...
public class MainClass extends JApplet {
OtherClass fr = new OtherClass();
@Override
public void init() {
add(fr);
}
}
从概念上讲,JAppelt
不应该打开其他窗口,applet应该是自包含的。因为您无法将顶级容器(如JFrame
)添加到其他容器中,所以它的选择范围很小,因此我选择从{{1}扩展OtherClass
而不是......
答案 1 :(得分:0)
这些线看起来很腥:
// Set the applet to use flow layout.
setLayout(new FlowLayout());
...
// Add the components to the applet's content pane.
getContentPane().add(jbtnOne);
getContentPane().add(jbtnTwo);
getContentPane().add(jlab);
如果它全部在一个类中,setLayout()和getContentPane()将调用JApplet的setLayout和getContentPane方法,但现在不是因为它们位于另一个类中。
你可以在一个班级中发布代码看起来的样子吗?