package name; //swing library found in javax called to use the graphical contents. import javax.swing.*; //creating the contents for the calculator public class calc { JButton btn1= new JButton("1 "); JButton btn2= new JButton("2"); JButton btn3= new JButton("3"); JButton btn4= new JButton("4"); JButton btn5= new JButton("5"); JButton btn6= new JButton("6"); JButton btn7= new JButton("7"); JButton btn8= new JButton("8"); JButton btn9= new JButton("9"); JButton btn0= new JButton("0"); JButton btnAdd= new JButton("+"); JButton btnMinus= new JButton("-"); JButton btnDivide= new JButton("/"); JButton btnMultiply= new JButton("x"); JButton btnBulletPoint= new JButton("."); JButton btnEqual= new JButton("="); JButton btnC= new JButton("C"); JButton btnCE= new JButton("CE"); JButton btnPercent= new JButton("%"); JButton btnSqrt= new JButton("sqrt"); JButton btn1X= new JButton("1/X"); JButton btn1X2= new JButton("1/X"); JButton btnBack= new JButton("Backspace"); JTextField Text= new JTextField(); //public class to layout all the buttons in individual panels public JPanel ContentButton(){ //parent panel to add the sub (child) panels JPanel ButtonLayout= new JPanel(); //allows me to manually set the layout ButtonLayout.setLayout(null); //panel for the number,T/X and bullet point JPanel NumberPanel = new JPanel(); NumberPanel.setSize(150, 150); NumberPanel.setLocation(10,100); NumberPanel.add(btn7); NumberPanel.add(btn8); NumberPanel.add(btn9); NumberPanel.add(btn4); NumberPanel.add(btn5); NumberPanel.add(btn6); NumberPanel.add(btn1); NumberPanel.add(btn2); NumberPanel.add(btn3); NumberPanel.add(btn1X); NumberPanel.add(btn0); NumberPanel.add(btnBulletPoint); ButtonLayout.add(NumberPanel); //panel for the math symbols JPanel mathsSymbol= new JPanel(); mathsSymbol.setSize(110,160); mathsSymbol.setLocation(200, 70); mathsSymbol.add(btnC); mathsSymbol.add(btnCE); mathsSymbol.add(btnDivide); mathsSymbol.add(btnSqrt); mathsSymbol.add(btnMultiply); mathsSymbol.add(btn1X2); mathsSymbol.add(btnMinus); mathsSymbol.add(btnPercent); mathsSymbol.add(btnAdd); mathsSymbol.add(btnEqual); ButtonLayout.add(mathsSymbol); //panel for the Backspace JPanel backspace = new JPanel(); backspace.setSize(100, 50); backspace.setLocation(26,65); backspace.add(btnBack); ButtonLayout.add(backspace); //panel for the TextField.Y AXIS (up or down) is 40 and X AXIS (left or right) is -5. JPanel text = new JPanel(); text.setSize(200, 200); text.setLocation(140,30); text.add(Text); ButtonLayout.add(text); return ButtonLayout; } private static void TheFrame(){ //creating the frame to be assigned to the panel JFrame TitleFrame = new JFrame("Calculator"); //to be run when the application is required to be run calc calc = new calc(); /*The panels created in the ContentButton class is * assigned with the Frame created in this class */ TitleFrame.setContentPane(calc.ContentButton()); TitleFrame.setSize(400, 300); TitleFrame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { TheFrame(); }}); //AdvancedCalculator end } }
I have tried different sizes for the 'TextField' but when I change the size, the position changes instead of the size itself. can someone explain why this is and how can I resolve this issue. btw I am currently a beginner at Java programming and I am a learner who needs explanation of how the code works by a experienced Java programmer while coding and using Java books to understand the language. So plz be patient with me while explaining the solution to me.
thanks in advancement.
here is how the application currently looks like.
advanced calc print screen.jpg