gru*_*321 1 java awt frame netbeans-7
我有一个小模板,我为了通过 netbeans 7.1 通过一个简单的 Java 应用程序实现小程序而构建的(如不使用 Javacard、Netbeans 平台等......只是一个简单的 Java 应用程序,在应用程序运行时初始化小程序)
当我按下 netbeans 中的运行按钮时,我设法让它调用了小程序,并且我在小程序中有功能,但是,我似乎无法关闭它,我有一种可怕的感觉人们会告诉我使用 jFrame 和实现 EXIT_ON_CLOSE 方法。
这不是我想知道怎么做的事情,我的任务是使用 Frames != jFrames 来实现它。我希望有人可以提供帮助,因为它现在困扰了我一段时间,我必须继续进行涉及其使用的 Java 分配。
附上 A:appletframe 和 B:applet 的代码/类
* 1.4 Write an applet to display a line of text.
* The text should then change its font size and style (bold, italic, underline)
* depending on where the mouse is clicked on the screen.
*/
package appletframe;
import java.awt.Graphics;
import java.awt.Frame;
import java.applet.Applet;
/**
* @author MuthaLoad aka Gruffy2012
*/
import java.awt.*;
public class AppletFrame extends Applet{
public static void main(String[] args) {
/*construct needs object instances*/
MrApplet mrApplet = new MrApplet(); // create instance/obj of MrApplet
Frame myFrame = new Frame("Applet"); // create frame "title optional"
//setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);(jFrame- not wanted)
/* add applet to the frame*/
//myFrame.addWindowListener();
myFrame.add(mrApplet, BorderLayout.CENTER);
myFrame.setBounds(10,10,500,500);
myFrame.setVisible(true); // step to make frame visible
/*initialize instance of mrApplet*/
mrApplet.init();
} // end main
} // end class
Run Code Online (Sandbox Code Playgroud)
B:小程序
package appletframe;
import java.awt.*; //for buttons
import java.awt.event.*; //for events
import java.applet.*; //main applet api`s
import java.awt.Graphics; //graphics
public class MrApplet extends Applet implements ActionListener
{
private static final long serialVersionUID = 1L;
Button btnClick;
String msg = "";
public void init()
{
// TODO start asynchronous download of heavy resources
setSize(500, 500);
Button btnClick = new Button("Press Me ");
btnClick.addActionListener(this);
add(btnClick);
}
public void actionPerformed(ActionEvent e)
{
//throw new UnsupportedOperationException("Not supported yet.");
msg = "Yay, the button works";
repaint();
}
public void paint (Graphics g)
{
g.setFont(new Font("Serif", Font.ITALIC, 30)); //new font obj, font , font style, font size
g.setColor(new Color(0,255,0)); //new color obj, r,g,b
g.drawString(msg, 40, 80);
}
// TODO overwrite start(), stop() and destroy() methods
}
Run Code Online (Sandbox Code Playgroud)
再次感谢您的阅读,并澄清任何混淆.. 我正在寻找一个关于在退出时关闭我的小程序和框架窗口的解决方案的指针,而不是使用 jFrame 重新实现它,尽管我知道这在第一个中会更容易实例。谢谢你,一如既往地感谢你的所有建议。粗暴321
在 ApplicationFrame 类的末尾添加这一行。
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Run Code Online (Sandbox Code Playgroud)
你想用 Frame 而不是 JFrame 来做,那么你必须将WindowListener事件添加到框架中,并且必须覆盖windowClosing()方法。
myFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5898 次 |
| 最近记录: |