2

I am using windows 8, 64 bit. I am learning GUI in Java. Generally I use eclipse IDE. I have also installed Net-beans. I am trying to do a simple GUI program which will add two number and show their result. But the problem is that when I am running the program from IDE it is looking like this. Picture One . But when I am running this same class file from command line it is showing correctly. I have test the same code in both eclipse and Net-beans. But the result is same. Source code for first picture.

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

then I have made another simple program by window-builder which will perform the four arithmetic operattion between two number. But the result is same for this case. Another picture has added named picture two. source code for second picture.

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

it is not appearing correctly in IDE, but when the class file is running in command line, it is appearing correctly.

I have updated my JDK.

what may the cause and solution?

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
Black Swan
  • 813
  • 13
  • 35
  • This is hard to say without the source code for your application. Try to provide the minimum class that shows this error. – GhostCat Apr 08 '15 at 11:29
  • what the fudge,It seems like a display problem rather than the code problem – nobalG Apr 08 '15 at 11:30
  • @ specializt thanks for ur advice...u given me a answer of a question which was unanswered to me for a long time.. – Black Swan Apr 08 '15 at 11:59
  • @ Jägermeister, source code added.. – Black Swan Apr 08 '15 at 12:01
  • 2
    This reminds me about this question: http://stackoverflow.com/questions/22737535/swing-rendering-appears-broken-in-jdk-1-8-correct-in-jdk-1-7 – Luke Apr 08 '15 at 12:01
  • 2
    @specializt that sounds like the advice my drunk dad use to give me - "Son, never try, you might fail and look like an idiot!" – Ian2thedv Apr 08 '15 at 12:26
  • @ Luke, i read it and tried some of it solutions, but not working. My problem is quit identical with ur link.. – Black Swan Apr 08 '15 at 12:27
  • @Ian2thedv "trying" wont help while trying to understand software which is entirely written in english, uses english nouns for method names, has english documentation and whatnot. "Trying" has nothing to do with "not being able to learn because of language barriers". – specializt Apr 08 '15 at 12:31

0 Answers0