我对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);
}
}
你有什么想法吗?
原始类如下所示:
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);
}
}
发布于 2014-11-26 16:09:55
将您的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
不应该打开其他窗口,小程序应该是自包含的。由于不能将顶级容器(如JFrame
)添加到其他容器中,所以很难进行扩展,因此我选择从JPanel
扩展OtherClass
的原因是……
发布于 2014-11-26 16:01:41
这些线条看上去很可疑:
// 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方法,而不是现在,因为它们位于另一个类中。
当代码都在一个类中时,你能贴出代码的外观吗?
https://stackoverflow.com/questions/27160887
复制