java - 空白 Java 小程序

标签 java swing applet

我在使用 Java 小程序时遇到问题。 我正在尝试制作一个简单的 java 小程序来看看它是如何工作的。

当我将所有内容放在一个类中时,一切看起来都很好,但是当我将其划分到其他类时,小程序窗口什么也不显示:

头等舱:

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);     
  } 
}

最佳答案

将您的 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)添加到其他容器,所以它是一个糟糕的扩展选择,因此我选择扩展 OtherClass来自 JPanel 而不是...

关于java - 空白 Java 小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27160887/

相关文章:

java - 从 GUI 代码外部请求用户输入,在这种情况下我是否需要事件或操作来与 GUI 进行通信?

java - 为 JTable 单元格着色

java - GridBagLayout 不使用 anchor

java - 如何在我的小程序中加载并显示远程 jar 小程序?

javascript - 从 JavaScript 调用 applet 的方法

java - 处理中的不可预测行为

java - 将 CXF 与具有未发布元数据的 WSDL 结合使用

java - 使用 FT 搜索检索父文档时出现延迟

java - @ComponentScan 中的 excludeFilters 与 @EnableAutoConfiguration 中的排除

用于打开包含文本、图像和格式的 PDF 的 Java 库?