首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >空白java小程序

空白java小程序
EN

Stack Overflow用户
提问于 2014-11-26 23:51:31
回答 2查看 183关注 0票数 0

我对java applet有个问题。我正在尝试制作一个简单的java小程序,看看它是如何工作的。

当我把所有的内容都放在一个类中时,一切看起来都很好,但是当我把它分成其他类时,applet窗口什么也没有显示:

头等舱:

代码语言:javascript
运行
复制
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();

    }

}

二等舱:

代码语言:javascript
运行
复制
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);
    }




}

你有什么想法吗?

原始类如下所示:

代码语言:javascript
运行
复制
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);     
  } 
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-27 00:09:55

将您的OtherClass改为从JPanel扩展.

代码语言:javascript
运行
复制
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..。

代码语言:javascript
运行
复制
public class MainClass extends JApplet {
    OtherClass fr = new OtherClass();
    @Override
    public void init() {
        add(fr);
    }

}

从概念上讲,JAppelt不应该打开其他窗口,小程序应该是自包含的。由于不能将顶级容器(如JFrame )添加到其他容器中,所以很难进行扩展,因此我选择从JPanel扩展OtherClass的原因是……

票数 1
EN

Stack Overflow用户

发布于 2014-11-27 00:01:41

这些线条看上去很可疑:

代码语言:javascript
运行
复制
    // 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方法,而不是现在,因为它们位于另一个类中。

当代码都在一个类中时,你能贴出代码的外观吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27160887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档