import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Test extends JFrame{

    JLabel label = new JLabel("Leann will come again");
    JButton yesButton = new JButton("Yes");
    JButton noButton = new JButton("Yes");
    JPanel panel = new JPanel();

    public Test(){

        panel.add(label);
        panel.add(yesButton);
        panel.add(noButton);
        add(panel);
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addAction();

    }

    public void addAction(){
        yesButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JOptionPane.showMessageDialog(null, "Are you sure?");
            }

        });

        noButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JOptionPane.showMessageDialog(null, "Too bad!");
            }

        });
    }

    public static void main(String args[]){
        Test app = new Test();

    }

}


当我用eclipse在我的ubuntu计算机上运行它时,它停止(终止),没有任何错误。控制台中也没有错误。而且没有语法错误。

怎么了?是因为我运行openjdk吗?

最佳答案

您不会将框架设置为可见的setVisible(true)

您应该查看本教程:http://download.oracle.com/javase/tutorial/uiswing/components/frame.html

10-06 03:53