gpt4 book ai didi

java - 空白 Java 小程序

转载 作者:行者123 更新时间:2023-12-01 12:13:16 27 4
gpt4 key购买 nike

我在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com