java - Applet 和 Swing 在 Eclipse 中不显示组件

标签 java eclipse swing applet components

我已在 Eclipse 中将以下代码作为应用程序运行,并且它运行良好,但是当我尝试将其作为小程序运行时,我得到一个显示“按钮到文本”的窗口,但没有出现任何按钮。此外,还会弹出一个空的“小程序查看器”窗口。这是代码。

/**This program completes the requirements described
 * in Option 3 which is to create a frame that contains 3 buttons.
 * These buttons then will update the text shown on the frame.
 * The program uses AWT and Swing to accomplish this task.
 * For more information on the particular methods of this program, look below.
 * Press a button to change the text on the bottom to the text displayed on the button
 */
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingOption3 extends Applet {

    private JFrame mainFrame;
    private JLabel header;
    private JLabel status;
    private JPanel control;
    //initialize the variables by creating them
    public SwingOption3(){
        prepareGUI();
        //this constructor runs the prepareGUI method
    }
public static void main(String[] args){
    SwingOption3 swingOption3 = new SwingOption3();
    swingOption3.showEvent();
    //creates an object from the main class and runs the showEvent method
}
/** The following method performs these actions
 * 1. Adds a title to the frame ("Button to Text")
 * 2. Sets the size to 400 by 400
 * 3. Uses the GridLayout to proportion the frame
 * 4. Initializes the header and status variables
 * 5. Adds a listener to the main frame
 * 6. Adds the header, status, and control to the main frame
 * 7. Sets the main frame to visible
 * 8. Sets the control layout to flow
 */
private void prepareGUI(){
    mainFrame = new JFrame("Button to Text");
    mainFrame.setSize(400,400);
    mainFrame.setLayout(new GridLayout(3, 1));

    header = new JLabel("",JLabel.CENTER );
    status = new JLabel("", JLabel.CENTER);

    status.setSize(350, 100);
    mainFrame.addWindowListener(new WindowAdapter() {
        //this method allows the program window to be closed
        public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
        }
    });
    control = new JPanel();
    control.setLayout(new FlowLayout());

    mainFrame.add(header);
    mainFrame.add(control);
    mainFrame.add(status);
    mainFrame.setVisible(true);

}
/**The following method completes these tasks:
 * 1. Sets the text of the header to inform the user what they should do
 * 2. Adds the buttons to the JPanel "control"
 * 3. Creates the listener methods and describes the ActionEvent 
 * that will be sent for each button press
 **/
private void showEvent(){
    header.setText("Press a button to change the text at the bottom");
    //informs the user to press a button in order to change the text
    JButton appleButton = new JButton("Apple");
    JButton pearButton = new JButton("Pear");
    JButton bananaButton = new JButton("Banana");
    //creates the buttons and titles them
    appleButton.setActionCommand("Apple");
    pearButton.setActionCommand("Pear");
    bananaButton.setActionCommand("Banana");
    //sets the command that will be received and interpreted by the program
    appleButton.addActionListener(new ButtonClickListener());
    pearButton.addActionListener(new ButtonClickListener());
    bananaButton.addActionListener(new ButtonClickListener());
    //creates listeners for the button clicks
    control.add(appleButton);
    control.add(pearButton);
    control.add(bananaButton);
    //adds the buttons to the JPanel "control"
    mainFrame.setVisible(true);
    //makes the frame visible
}
private class ButtonClickListener implements ActionListener {
    /**
     * This method performs the following actions
     * 1.Receives the command/ActionEvent from the previous method
     * 2. Displays the appropriate text based on the ActionEvent
     */
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if(command.equals("Apple")) {
            status.setText("Display: Apple");
        } //sets the display to "Display: Apple"
        else if( command.equals("Pear")) {
            status.setText("Display: Pear");
        } //sets the display to "Display: Pear"
        else {
            status.setText("Display: Banana");
        } //sets the display to "Display: Banana"
    }
}
}
`

我不太确定我哪里出错了,所以我发布了整个内容。感谢您的帮助。

最佳答案

您不必在小程序中创建新的 JFrame,而是使用小程序本身作为控件的容器。并且您的构造函数不会调用 showEvent()——其他控件从未被实例化。

关于java - Applet 和 Swing 在 Eclipse 中不显示组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34230365/

相关文章:

java - 在 JTable 中隐藏一列

java - 如何将计时器放入 GUI 中?

java - Spring 数据 JPA : How not to repeat myself in countQueries?

java - 如何定期生成敌人

java - GWT 项目编译不工作 - 没有错误,在 SUPERDEV 模式下完美运行

java - 终止操作中执行什么事件

java - jvm 版本 1.4.2_03 不适合此产品。 1.5或更高是必需的问题

java - 在过滤的 JTable 上选择特定的表模型元素

java - 将本地目录中的 zip 文件放入 java spring boot 中的列表中

java - 这是最适合 2 个离散池的 Java 集合