Java GUI,为什么不是我的出现?

时间:2015-10-31 03:26:45

标签: java swing

//leaving out import statements


public class MVCView extends JFrame{

private JButton add = new JButton("Click me");
private JTextArea center = new JTextArea(200,300);
private JTextField bottom = new JTextField(200);

public MVCView() {

    setLayout(new BorderLayout());
    add(add, BorderLayout.NORTH);
    add(center, BorderLayout.CENTER);
    add(bottom, BorderLayout.SOUTH);

    pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    }

}


public class MVCTester {
public static void main(String[] args) {
    MVCView view = new MVCView();
    }
}

我只是想让它显示在我的屏幕上。我用main创建一个类来创建一个不同类的对象。当我点击运行时,弹出任何东西。我一直在关注一些教程,我的代码看起来并没有太大的不同。我已经尝试过将所有内容放在JPanel中,然后才能正常工作......我不知道自己要忘记什么或做错了什么。

1 个答案:

答案 0 :(得分:0)

我刚刚修改了您发布的代码并且工作正常。

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class MVCView extends JFrame{

private JButton add = new JButton("Click me");
private JTextArea center = new JTextArea(200,300);
private JTextField bottom = new JTextField(200);

public MVCView() {

    setLayout(new BorderLayout());
    add(add, BorderLayout.NORTH);
    add(center, BorderLayout.CENTER);
    add(bottom, BorderLayout.SOUTH);

    pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    }





public static void main(String[] args) {
    MVCView view = new MVCView();
    }
}
相关问题