Java OOP Hellow World

时间:2019-03-08 00:40:34

标签: java oop

这是我的第一个OOP应用程序
我创建了此代码,但未运行

package try01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class try001 extends javax.swing.JPanel {
    public try001() {
        initComponents();
    }

    private void initComponents() {
        jButton1 = new javax.swing.JButton();

        jButton1.setText("Press");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGap(133, 133, 133)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(142, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addGap(89, 89, 89)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(95, Short.MAX_VALUE))
        );
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        JOptionPane.showMessageDialog(null, "Hello World");
    }                                        

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration 
    public static void main(String[] args) {
        JFrame Frame= new JFrame("Try01");
        Frame.setContentPane(new Try01().panelMain);
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame.pack();
        Frame.setVisible(true);
    }                  
}

2 个答案:

答案 0 :(得分:1)

Try01.class没有类,因此编译器将无法在new try01()处识别Frame.setContentPane(new Try01().panelMain); 另外,未定义panelMain。

将其更改为此-Frame.setContentPane(new try001());

希望这会有所帮助。

答案 1 :(得分:1)

您的代码有一个编译错误,如果仅更改上面的行就可以了。

更改-> Frame.setContentPane(new Try01()。panelMain);

至-> Frame.setContentPane(Frame.getContentPane());

Try01()类不存在,这是因为您的代码未生成。