hi am trying to code a program that uses a panel that has a functional calculator in it, and the calculator can be re-sized using a slider.
here is my code
please help me out
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class CalcInSlider extends JPanel{
JSlider slider;
JLabel label;
private JPanel MasterFieldP, calcPanel;
private JLabel Tx;
Container c;
private JButton buttons[]; //array of buttons
private final String names[]={"7","8","9","/","*","4","5","6","+","-","1","2","3","=",",","0","."};
public static void main(String[] args){
CreateSlider cs = new CreateSlider();
Calculator calc=new Calculator();
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public CalcInSlider(){
JFrame frame = new JFrame("Slider Frame");
slider = new JSlider();
slider.setValue(70);
slider.addChangeListener(new MyChangeAction());
label = new JLabel("Calclator");
JPanel panel = new JPanel();
panel.add(slider);
panel.add(label);
frame.add(panel, BorderLayout.WEST);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Calculator() {
super ("Calculator");
c=getContentPane();
MasterFieldP=new JPanel();
Tx=new JLabel("0");
Tx.setHorizontalTextPosition(JLabel.LEFT);
Tx.setBackground(Color.WHITE);
Tx.setOpaque(true);
getContentPane().add(Tx, BorderLayout.NORTH);
calcPanel=new JPanel();
buttons=new JButton[names.length];//create array of JButtons
calcPanel.setLayout(new GridLayout(4,4,5,5));
for (int count=0;count<names.length;count++)
{
buttons[count]=new JButton(names[count]);
buttons[count].addActionListener(this);//register Listener
calcPanel.add(buttons[count]);//add button to JFrame
}
MasterFieldP.setLayout(new BorderLayout());
MasterFieldP.add(calcPanel,BorderLayout.SOUTH);
getContentPane().add(MasterFieldP,BorderLayout.SOUTH);
setSize(300,200);//set frame size
setVisible(true);//display frame
}//end class Calculator
public class MyChangeAction implements ChangeListener{
public void stateChanged(ChangeEvent ce){
int value = slider.getValue();
String str = Integer.toString(value);
label.setText(str);
}
}
}//end class CalcInSlider