框架为什么不显示?

时间:2015-07-21 23:06:19

标签: java user-interface jframe

在实现Observer / Observable模式后,我无法真正理解为什么在执行代码时Frame没有显示。

我已经看了一百万次,但无法理解。

我只是想知道我哪里出错了以及如何修复,以便显示框架。

代码转储道歉。非常感谢你的帮助。

    public class MainGameFrame extends JFrame implements KeyListener,Observer {

        private final JLabel lblPythonChallenge = DefaultComponentFactory
        .getInstance().createTitle("Code Wars");
        protected JTextArea textAreaEditable;
        protected JTextArea textAreaQuestion;
        protected JTextArea textAreaResult;
        protected JTextArea textAreaScore;
        protected PrintWriter output; // to write textArea stuff to file
        private JTextArea textAreaPreview;
        private JLabel lblPreview;

        private ServerToClient model;
        private Socket sock;

        /**
        * Create the application.
        */
        public MainGameFrame() {

    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {

                sock = new Socket("127.0.0.1", 6789);
                model = new ServerToClient(sock);

            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    });
    initialize();
    setVisible(true);

     }

        /**
        * Initialize the contents of the frame.
        */
        private void initialize() {
            model.addObserver(this);

            getContentPane().setBackground(new Color(153, 204, 102));
            setForeground(Color.GREEN);
            setBackground(Color.GREEN);
            setTitle("Code Wars");
            setBounds(100, 100, 762, 511);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            getContentPane().setLayout(null);

            // TextArea 1
            textAreaEditable = new JTextArea();
            textAreaEditable.setBounds(10, 117, 353, 130);
            getContentPane().add(textAreaEditable);
            textAreaEditable.setTabSize(2); // fix tab size
            // auto-indentation
            textAreaEditable.registerKeyboardAction(new IndentNextLine(),
            KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
            JComponent.WHEN_FOCUSED);

            // add KeyListener
            textAreaEditable.addKeyListener(this);

            // TextArea 2
            textAreaQuestion = new JTextArea();
            textAreaQuestion.setBackground(new Color(255, 255, 255));
            textAreaQuestion.setBounds(10, 34, 353, 46);
            getContentPane().add(textAreaQuestion);

            // TextArea 3
            textAreaResult = new JTextArea();
            textAreaResult.setBounds(10, 357, 713, 104);
            getContentPane().add(textAreaResult);

            textAreaResult.setEditable(false);

            // JTextPane textPane_1 = new JTextPane();
            // textPane_1.setBounds(463, 158, 273, 269);
            // frmPythonChallenge.getContentPane().add(textPane_1);

            // component 4
            JButton btnRunCode = new JButton("Run Code");
            btnRunCode.setBounds(10, 258, 330, 31);
            getContentPane().add(btnRunCode);
            btnRunCode.addActionListener(new RunButtonListener());

            // TextArea 4
            textAreaScore = new JTextArea();
            textAreaScore.setBounds(373, 34, 350, 46);
            getContentPane().add(textAreaScore);
            lblPythonChallenge.setBounds(23, -31, 143, 31);
            getContentPane().add(lblPythonChallenge);

            JLabel lblEnterCodeIn = DefaultComponentFactory.getInstance()
            .createLabel("Enter code in the box below:");
            lblEnterCodeIn.setForeground(new Color(0, 100, 0));
            lblEnterCodeIn.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblEnterCodeIn.setBounds(10, 91, 209, 15);
            getContentPane().add(lblEnterCodeIn);

            JLabel lblQuestion = DefaultComponentFactory.getInstance().createLabel(
            "Question:");
            lblQuestion.setForeground(new Color(0, 100, 0));
            lblQuestion.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblQuestion.setBounds(10, 9, 92, 14);
            getContentPane().add(lblQuestion);

            JLabel lblScores = DefaultComponentFactory.getInstance().createLabel(
            "Scores:");
            lblScores.setForeground(new Color(0, 100, 0));
            lblScores.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblScores.setBounds(373, 9, 92, 14);
            getContentPane().add(lblScores);

            JLabel lblThisIsThe = DefaultComponentFactory.getInstance()
            .createLabel("This is the result from your code!");
            lblThisIsThe.setForeground(new Color(0, 100, 0));
            lblThisIsThe.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblThisIsThe.setBounds(10, 328, 238, 15);
            getContentPane().add(lblThisIsThe);

            // TextArea 5
            textAreaPreview = new JTextArea();
            textAreaPreview.setBounds(373, 117, 350, 130);
            textAreaPreview.setTabSize(2);
            textAreaPreview.setEditable(false);
            getContentPane().add(textAreaPreview);

            lblPreview = DefaultComponentFactory.getInstance().createLabel(
            "Preview");
            lblPreview.setForeground(new Color(0, 128, 0));
            lblPreview.setFont(new Font("Tahoma", Font.BOLD, 12));
            lblPreview.setLabelFor(lblPreview);
            lblPreview.setBounds(373, 84, 200, 31);
            getContentPane().add(lblPreview);

        }

        /**
        * Launch the application.
        */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        MainGameFrame window = new MainGameFrame();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    }

2 个答案:

答案 0 :(得分:1)

将main()方法更改为:

public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainGameFrame window = new MainGameFrame();
                    window.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

答案 1 :(得分:0)

initialize()方法的最后,添加一些使MainGameFrame()可见的代码:

this.setVisible(true);

我还建议您使用布局管理器来组织组件。 Oracle提供了一些关于如何使用不同布局的精彩教程。 How to Use Various Layout Managers