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. . 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);
}
}
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?