N
naw84az
the programe is basic Calculator , i add new Class extends the
Calculator class add Override method inside method i use
Super.createPanel then i want add text Filed in this Method return in
runtime NotllPointer Wxception
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CCalculator1 extends JFrame {
// to use in sub class
private Container con = new Container ();
//
private JPanel tpanel = new JPanel ();
private JPanel bpanel = new JPanel();
private JPanel npanel = new JPanel ();
private JPanel pluspanel = new JPanel ();
private JPanel equalpanel = new JPanel ();
private JPanel clearpanel = new JPanel ();
private JPanel advpanel = new JPanel ();
protected JTextField text = new JTextField();
private String label [] =
{ "7","8","9","4","5","6","1","2" ,"3 ","0","."," -/+ ","/","*","-",
"+","C","="};
private String advlabel [] =
{"PI","E","sqr","Sin","Cos",
"Tan","Log","Log10","a^2"};
private JButton advbutton [] = new JButton [advlabel.length];
private JButton button [] = new JButton [label.length];
private String str1 = new String ("0");
private String str2 = new String ("");
private String str3 = new String ("");
private String pi = new String ("");
private float var1 =0;
private float var2 =0;
CCalculator1 (){
try {
this.jbinit ();
}
catch (Exception e ){
e.printStackTrace();
}
}
protected void jbinit () throws Exception{
// Set Content
this.con = getContentPane ();
this.con.setLayout(null);
// Panel
this.createPanel ();
// Frame
this.setTitle("Calcultor");
this.setSize(new Dimension (400,400));
this.setLocation(new Point (300,400));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
protected void createPanel () {
// Set Panel
tpanel.setLayout(new BorderLayout ());
tpanel.setSize(new Dimension (270,30));
tpanel.setLocation(new Point (10,10));
tpanel.setOpaque(true);
tpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel
bpanel.setLayout(new GridLayout (4,3));
bpanel.setSize(new Dimension (270,100));
bpanel.setLocation(new Point (10,50));
bpanel.setOpaque(true);
bpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel *,,/,-
npanel.setLayout(new GridLayout (1,3));
npanel.setSize(new Dimension (270,30));
npanel.setLocation(new Point (10,155));
npanel.setOpaque(true);
npanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel
pluspanel.setLayout(new GridLayout (1,1));
pluspanel.setSize(new Dimension (60,100));
pluspanel.setLocation(new Point (290,50));
pluspanel.setOpaque(true);
pluspanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel
equalpanel.setLayout(new GridLayout (1,1));
equalpanel.setSize(new Dimension (60,30));
equalpanel.setLocation(new Point (290,155));
equalpanel.setOpaque(true);
equalpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel
clearpanel.setLayout(new GridLayout (1,1));
clearpanel.setSize(new Dimension (60,30));
clearpanel.setLocation(new Point (290,10));
clearpanel.setOpaque(true);
clearpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel
advpanel.setLayout(new GridLayout (3,3));
advpanel.setSize(new Dimension (270,100));
advpanel.setLocation(new Point (10,200));
advpanel.setOpaque(true);
advpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Text Field
text.setText(str1);
text.setColumns(20);
text.setEditable(true);
text.setHorizontalAlignment(JTextField.CENTER);
//Add Componant
tpanel.add(text);
this.con.add(tpanel);
//Add Componant Button Number
for ( int i = 0 ; i < 12 ; i++){
button = new JButton ();
button.setText(label);
bpanel.add(button);
}
this.con.add(bpanel);
//Add Componant - , * ,/
for ( int i = 12 ; i < 15 ; i++){
button = new JButton ();
button.setText(label);
npanel.add(button);
}
this.con.add(npanel);
//Add Componant +
button [15] = new JButton ();
button[15].setText(label[15]);
pluspanel.add(button[15]);
this.con.add(pluspanel);
//Add Componant =
button [17] = new JButton ();
button[17].setText(label[17]);
equalpanel.add(button[17]);
this.con.add(equalpanel);
//Add Componant C
button [16] = new JButton ();
button[16].setText(label[16]);
clearpanel.add(button[16]);
this.con.add(clearpanel);
//Add Advance Componant
for ( int i = 0 ; i < advbutton.length ; i++){
advbutton = new JButton ();
advbutton.setText(advlabel);
advpanel.add(advbutton);
}
this.con.add(advpanel);
}
}
Extends
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AdvCCalculator extends CCalculator1 {
public AdvCCalculator () {
// JOptionPane.showMessageDialog(null,"test"+con);
}
protected void createPanel () {
super.createPanel();
// set Panel
}
private void ceateAdvPanel (){
}
public static void main (String arg [] ) {
AdvCCalculator c = new AdvCCalculator ();
}
}
how to add Text Field in Panel Display in Frame
Calculator class add Override method inside method i use
Super.createPanel then i want add text Filed in this Method return in
runtime NotllPointer Wxception
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CCalculator1 extends JFrame {
// to use in sub class
private Container con = new Container ();
//
private JPanel tpanel = new JPanel ();
private JPanel bpanel = new JPanel();
private JPanel npanel = new JPanel ();
private JPanel pluspanel = new JPanel ();
private JPanel equalpanel = new JPanel ();
private JPanel clearpanel = new JPanel ();
private JPanel advpanel = new JPanel ();
protected JTextField text = new JTextField();
private String label [] =
{ "7","8","9","4","5","6","1","2" ,"3 ","0","."," -/+ ","/","*","-",
"+","C","="};
private String advlabel [] =
{"PI","E","sqr","Sin","Cos",
"Tan","Log","Log10","a^2"};
private JButton advbutton [] = new JButton [advlabel.length];
private JButton button [] = new JButton [label.length];
private String str1 = new String ("0");
private String str2 = new String ("");
private String str3 = new String ("");
private String pi = new String ("");
private float var1 =0;
private float var2 =0;
CCalculator1 (){
try {
this.jbinit ();
}
catch (Exception e ){
e.printStackTrace();
}
}
protected void jbinit () throws Exception{
// Set Content
this.con = getContentPane ();
this.con.setLayout(null);
// Panel
this.createPanel ();
// Frame
this.setTitle("Calcultor");
this.setSize(new Dimension (400,400));
this.setLocation(new Point (300,400));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
protected void createPanel () {
// Set Panel
tpanel.setLayout(new BorderLayout ());
tpanel.setSize(new Dimension (270,30));
tpanel.setLocation(new Point (10,10));
tpanel.setOpaque(true);
tpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel
bpanel.setLayout(new GridLayout (4,3));
bpanel.setSize(new Dimension (270,100));
bpanel.setLocation(new Point (10,50));
bpanel.setOpaque(true);
bpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel *,,/,-
npanel.setLayout(new GridLayout (1,3));
npanel.setSize(new Dimension (270,30));
npanel.setLocation(new Point (10,155));
npanel.setOpaque(true);
npanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel
pluspanel.setLayout(new GridLayout (1,1));
pluspanel.setSize(new Dimension (60,100));
pluspanel.setLocation(new Point (290,50));
pluspanel.setOpaque(true);
pluspanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel
equalpanel.setLayout(new GridLayout (1,1));
equalpanel.setSize(new Dimension (60,30));
equalpanel.setLocation(new Point (290,155));
equalpanel.setOpaque(true);
equalpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel
clearpanel.setLayout(new GridLayout (1,1));
clearpanel.setSize(new Dimension (60,30));
clearpanel.setLocation(new Point (290,10));
clearpanel.setOpaque(true);
clearpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Set Panel
advpanel.setLayout(new GridLayout (3,3));
advpanel.setSize(new Dimension (270,100));
advpanel.setLocation(new Point (10,200));
advpanel.setOpaque(true);
advpanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Text Field
text.setText(str1);
text.setColumns(20);
text.setEditable(true);
text.setHorizontalAlignment(JTextField.CENTER);
//Add Componant
tpanel.add(text);
this.con.add(tpanel);
//Add Componant Button Number
for ( int i = 0 ; i < 12 ; i++){
button = new JButton ();
button.setText(label);
bpanel.add(button);
}
this.con.add(bpanel);
//Add Componant - , * ,/
for ( int i = 12 ; i < 15 ; i++){
button = new JButton ();
button.setText(label);
npanel.add(button);
}
this.con.add(npanel);
//Add Componant +
button [15] = new JButton ();
button[15].setText(label[15]);
pluspanel.add(button[15]);
this.con.add(pluspanel);
//Add Componant =
button [17] = new JButton ();
button[17].setText(label[17]);
equalpanel.add(button[17]);
this.con.add(equalpanel);
//Add Componant C
button [16] = new JButton ();
button[16].setText(label[16]);
clearpanel.add(button[16]);
this.con.add(clearpanel);
//Add Advance Componant
for ( int i = 0 ; i < advbutton.length ; i++){
advbutton = new JButton ();
advbutton.setText(advlabel);
advpanel.add(advbutton);
}
this.con.add(advpanel);
}
}
Extends
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AdvCCalculator extends CCalculator1 {
public AdvCCalculator () {
// JOptionPane.showMessageDialog(null,"test"+con);
}
protected void createPanel () {
super.createPanel();
// set Panel
}
private void ceateAdvPanel (){
}
public static void main (String arg [] ) {
AdvCCalculator c = new AdvCCalculator ();
}
}
how to add Text Field in Panel Display in Frame