尽管没有错误,NetBeans Java项目仍不会显示GUI窗口 [英] NetBeans Java Project Won't Display GUI Window Despite No Error

查看:95
本文介绍了尽管没有错误,NetBeans Java项目仍不会显示GUI窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Netbeans IDE 7.1构建Java项目.

I am trying to build a java project using Netbeans IDE 7.1.

我无法以某种方式查看或查看我创建的GUI窗口.

I somehow can't see or view the GUI window that I have created.

请告知.

在我的课上:

package rmiSimpleCalc;

public class RMISimpleCalculatorMain {

    public static void main(String[] args) {
        MainCalculator calc = new MainCalculator();
        calc.setVisible(true);
    }
}

MainCalculator是我要运行的GUI窗口.它不会以某种方式显示.

the MainCalculator is the GUI window I would like to run. It somehow wont't display.

控制台中出现错误消息.

There is NO error message in my console tough..

这是MainCalculator代码:

package rmiSimpleCalc;

import java.rmi.*;

public class MainCalculator extends javax.swing.JPanel {

    public MainCalculator() {
        initComponents();
        ComboBoxOperator.addItem("+");
        ComboBoxOperator.addItem("-");
        ComboBoxOperator.addItem("/");
        ComboBoxOperator.addItem("*");             
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        txtFirstDigit = new javax.swing.JTextField();
        txtSecondDigit = new javax.swing.JTextField();
        btnCalculate = new javax.swing.JButton();
        lblFirstDigit = new javax.swing.JLabel();
        lblSecondDigit = new javax.swing.JLabel();
        ComboBoxOperator = new javax.swing.JComboBox();
        lblOperator = new javax.swing.JLabel();
        lblResult = new javax.swing.JLabel();
        lblHeader = new javax.swing.JLabel();
        btnConfigureServer = new javax.swing.JButton();
        txtResult = new javax.swing.JTextField();

        btnCalculate.setText("Calculate");
        btnCalculate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalculateActionPerformed(evt);
            }
        });

        lblFirstDigit.setText("First Digit");

        lblSecondDigit.setText("Second Digit");

        ComboBoxOperator.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lblOperator.setText("Operator");

        lblResult.setText("Result");

        lblHeader.setText("RMI Simple Calculator");

        btnConfigureServer.setText("Configure Server");
        btnConfigureServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfigureServerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblFirstDigit, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblSecondDigit, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblOperator, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblResult, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblHeader)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ComboBoxOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCalculate))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtSecondDigit)
                        .addComponent(txtFirstDigit, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnConfigureServer)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(lblHeader)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFirstDigit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblFirstDigit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSecondDigit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSecondDigit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ComboBoxOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblOperator)
                    .addComponent(btnCalculate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblResult)
                    .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnConfigureServer)
                .addContainerGap(24, Short.MAX_VALUE))
        );
    }                       

    private void btnConfigureServerActionPerformed(java.awt.event.ActionEvent evt) {                                                   

    }                                                  

    private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {                                             
        double firstdigit;
        double seconddigit;
        String operator;

        firstdigit = Double.valueOf(txtFirstDigit.getText());

        seconddigit = Double.valueOf(txtSecondDigit.getText());

        operator = ComboBoxOperator.getSelectedItem().toString();

        try
        {
            CoreInterface coreobj = (CoreInterface) Naming.lookup("localhost/Core");
            double result = (coreobj.calc(firstdigit,seconddigit,operator));
            txtResult.setText(Double.toString(result));
        }

        catch(Exception e)
        {
            txtResult.setText("e");
        }   
    }                                            

    private javax.swing.JComboBox ComboBoxOperator;
    private javax.swing.JButton btnCalculate;
    private javax.swing.JButton btnConfigureServer;
    private javax.swing.JLabel lblFirstDigit;
    private javax.swing.JLabel lblHeader;
    private javax.swing.JLabel lblOperator;
    private javax.swing.JLabel lblResult;
    private javax.swing.JLabel lblSecondDigit;
    private javax.swing.JTextField txtFirstDigit;
    private javax.swing.JTextField txtResult;
    private javax.swing.JTextField txtSecondDigit;
    // End of variables declaration                   
}

任何建议都值得赞赏.

推荐答案

MainCalculator类是JPanel. JPanel不能只是这样显示,它必须是Window的一部分.将其添加到JFrame并在JFrame上调用setVisible(true).

The MainCalculator class is a JPanel. A JPanel can't just be displayed like that, it has to be part of a Window. Add it to a JFrame and call setVisible(true) on the JFrame.

@npinti的建议也很好:在EDT线程中执行与GUI相关的代码.

Also, @npinti's advice is very good: execute GUI-related code in the EDT thread.

这篇关于尽管没有错误,NetBeans Java项目仍不会显示GUI窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆