Java GUI在IDE中无法正确显示

时间:2015-04-08 11:25:58

标签: java eclipse user-interface

我使用的是Windows 8,64位。 我正在学习Java中的GUI。一般我使用eclipse IDE。我还安装了Net-beans。 我正在尝试做一个简单的GUI程序,它将添加两个数字并显示其结果。但问题是,当我从IDE运行程序时,它看起来像这样。 Picture One。但是当我从命令行运行这个相同的类文件时,它正确显示。我在eclipse和Net-beans中测试了相同的代码。但结果是一样的。 第一张图片的源代码。

import javax.swing.JOptionPane; // program uses JOptionPane

public class Addition 
{
   public static void main( String[] args )
   {
      // obtain user input from JOptionPane input dialogs
      String firstNumber = 
         JOptionPane.showInputDialog( "Enter first integer" );
      String secondNumber =
          JOptionPane.showInputDialog( "Enter second integer" );

      // convert String inputs to int values for use in a calculation
      int number1 = Integer.parseInt( firstNumber ); 
      int number2 = Integer.parseInt( secondNumber );

      int sum = number1 + number2; // add numbers

      // display result in a JOptionPane message dialog
      JOptionPane.showMessageDialog( null, "The sum is " + sum, 
         "Sum Integers", JOptionPane.PLAIN_MESSAGE );
   } // end method main
} // end class Addition

然后我通过window-builder创建了另一个简单的程序,它将执行两个数字之间的四个算术运算。但结果与此案相同。另一张图片添加了命名图片二。 第二张图片的源代码。

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Button;
import java.awt.Label;
import javax.swing.JMenuBar;
import javax.swing.JToggleButton;
import javax.swing.JCheckBox;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.SwingConstants;
import javax.swing.JTable;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class test extends JFrame {

    private JPanel contentPane;
    private JTextField one;
    private JTextField two;
    private JTextField ans;
    private JLabel lblNumone;
    private JLabel lblNumtwo;
    private JButton btnDiv;
    private JButton btnMult;

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

    /**
     * Create the frame.
     */
    public test()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 294, 322);
        contentPane = new JPanel();
        contentPane.setBackground(Color.GREEN);
        contentPane.setForeground(SystemColor.desktop);
        contentPane.setBorder(new EmptyBorder(19, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        one = new JTextField();
        one.setBounds(125, 5, 89, 25);
        contentPane.add(one);
        one.setColumns(10);

        two = new JTextField();
        two.setBounds(125, 50, 89, 25);
        contentPane.add(two);
        two.setColumns(20);

        JButton btnAdd = new JButton("Add");
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                int num1,num2,answer;

                try{

                    num1=Integer.parseInt(one.getText());
                    num2=Integer.parseInt(two.getText());

                    answer =num1 + num2;

                    ans.setText(Integer.toString(answer));

                }catch (Exception e)
                {
                    JOptionPane.showMessageDialog(null, "Please Enter Correct Number:");
                }
            }
        });
        btnAdd.setBounds(125, 86, 89, 23);
        contentPane.add(btnAdd);

        JButton btnMinus = new JButton("Minus");
        btnMinus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


                  int num1,num2,answer;

                try{

                    num1=Integer.parseInt(one.getText());
                    num2=Integer.parseInt(two.getText());

                    answer =num1 - num2;

                    ans.setText(Integer.toString(answer));

                }catch (Exception e)
                {
                    JOptionPane.showMessageDialog(null, "Please Enter Correct Number:");
                }
            }
        });
        btnMinus.setBounds(125, 119, 89, 23);
        contentPane.add(btnMinus);

        ans = new JTextField();
        ans.setBounds(125, 221, 89, 25);
        contentPane.add(ans);
        ans.setColumns(10);

        JLabel lblAnswer = new JLabel("Answer");
        lblAnswer.setBounds(58, 230, 46, 14);
        contentPane.add(lblAnswer);

        lblNumone = new JLabel("NumberOne ");
        lblNumone.setBounds(10, 10, 78, 14);
        contentPane.add(lblNumone);

        lblNumtwo = new JLabel("NumberTwo");
        lblNumtwo.setBounds(10, 55, 78, 14);
        contentPane.add(lblNumtwo);

        btnDiv = new JButton("Div");
        btnDiv.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
int num1,num2,answer;

                try{

                    num1=Integer.parseInt(one.getText());
                    num2=Integer.parseInt(two.getText());

                    answer =num1 / num2;

                    ans.setText(Integer.toString(answer));

                }catch (Exception eDiv)
                {
                    JOptionPane.showMessageDialog(null, "Please Enter Correct Number:");
                }
            }
        });
        btnDiv.setBounds(125, 153, 89, 23);
        contentPane.add(btnDiv);

        btnMult = new JButton("Mult");
        btnMult.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
int num1,num2,answer;

                try{

                    num1=Integer.parseInt(one.getText());
                    num2=Integer.parseInt(two.getText());

                    answer =num1 * num2;

                    ans.setText(Integer.toString(answer));

                }catch (Exception eMult)
                {
                    JOptionPane.showMessageDialog(null, "Please Enter Correct Number:");
                }
            }
        });
        btnMult.setBounds(125, 187, 89, 23);
        contentPane.add(btnMult);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBounds(293, 28, 200, 50);
        contentPane.add(menuBar);
    }
}

picture two

它在IDE中没有正确显示,但是当类文件在命令行中运行时,它正确显示。

我已经更新了我的JDK。

原因和解决方案可能是什么?

0 个答案:

没有答案