有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java小程序和Swing在eclipse中不显示组件

我已经在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"
    }
}
}
`

我不太确定我哪里出错了,所以我把整件事都贴了出来。我感谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    您不需要在applet中创建新的JFrames,而是使用applet本身作为控件的容器。您的构造函数不调用showEvent(),其他控件从未实例化