Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Cory Nelson   on Jan 05 In Java Category.

  
Question Answered By: Samuel Costa   on Jan 05

There are few things you need to fix:

1. You set the JPanel's layout to FlowLayout. This will
make all components put into one  length row, and you
can't control it. So, I would suggest that you make
it into GridLayout, eg.

JPanel container  = new JPanel();
//container.setLayout( new FlowLayout( FlowLayout.CENTER));
container.setLayout(new GridLayout(4, 8)); // 4 rows and 8 columns

Then you add  your button components as your wish, by
filling the first row, second row, etc.

// First Row
//pi
pi = new JButton( "PI" );
pi.addActionListener(this);
container.add( pi );
//mem
mem = new JButton( "MEM" );
mem.addActionListener(this);
container.add( mem );
//mrc
mrc = new JButton( "MRC" );
mrc.addActionListener(this);
container.add( mrc );
//seven
seven = new JButton( "7" );
seven.addActionListener(this);
container.add( seven );
//eight
eight = new JButton( "8" );
eight.addActionListener(this);
container.add( eight );
//nine
nine = new JButton( "9" );
nine.addActionListener(this);
container.add( nine );
//div
div = new JButton( "/" );
div.addActionListener(this);
container.add( div );
// blank
blank = new JLabel();
container.add( blank  );

// Second row
//x2
x2 = new JButton( "X2" );
x2.addActionListener(this);
container.add( x2 );
//sin
sin = new JButton( "SIN" );
sin.addActionListener(this);
container.add( sin );
//asin
asin = new JButton( "ASIN" );
asin.addActionListener(this);
container.add( asin );
//four
four = new JButton( "4" );
four.addActionListener(this);
container.add( four );
//five
five = new JButton( "5" );
five.addActionListener(this);
container.add( five );
//six
six = new JButton( "6" );
six.addActionListener(this);
container.add( six );
//mult
mult = new JButton( "*" );
mult.addActionListener(this);
container.add( mult );
// blank
blank = new JLabel();
container.add( blank );

// third row
//sqrt
sqrt = new JButton( "SQRT" );
sqrt.addActionListener(this);
container.add( sqrt );
//cos
cos = new JButton( "COS" );
cos.addActionListener(this);
container.add( cos );
//acos
acos = new JButton( "ACOS" );
acos.addActionListener(this);
container.add( acos );
.... continue with the rest  and it would look like

PI MEM MRC 7 8 9 / [blank]
X2 SIN ASIN 4 5 6 * [blank]
SQRT COS ACOS 1 2 3 - %
EXP TAN ATAN 0 . = + CE

2. The second thing that I would like to highlight is
remove the setSize and setVisible methods from init.
This won't help  you set anything.
It's better to put it inside the main method.

public static void  main(String args[]){
//execute applet  as application
//applet's window
JFrame applicationWindow = new JFrame("calculator");

//applet instance
Calcg appletObject = new Calcg();

//init and start methods
appletObject.init();
//appletObject.start();

// Don't forget to add appletObject into the
// frame. The applet Object is considered as a component now.
applicationWindow.getContentPane().add(appletObject);

applicationWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//applicationWindow.setSize(190, 285);
// pack will automatically resize the window as default look.
applicationWindow.pack();
applicationWindow.setVisible(true);
} // end main

3. Just to make it nicer, maybe you can shift the result
to right hand side

output = new JLabel();
output.setBorder(new MatteBorder(2,2,2,2,Color.gray));
// set it to right hand side.
output.setHorizontalAlignment(SwingConstants.RIGHT);
output.setPreferredSize(new Dimension(1,26));

Share: