import javax.swing.JFrame;
public class MainApp
{
public static void main(String[] args)
{
new MainApp().start();
}
public void start()
{
CalculatorUI obj = new CalculatorUI();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setSize(275,275);
obj.setVisible(true);
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class CalculatorUI extends JFrame
{
//Variables.
private JTextField text1;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton button0;
public CalculatorUI()
{
super ("Calculator");
setLayout(new FlowLayout());
text1 = new JTextField(10);
text1.setEditable(false);
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
button0 = new JButton("0");
theHandler handler = new theHandler();
text1.addActionListener(handler);
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);
button4.addActionListener(handler);
button5.addActionListener(handler);
button6.addActionListener(handler);
button7.addActionListener(handler);
button8.addActionListener(handler);
button9.addActionListener(handler);
button0.addActionListener(handler);
}
private class theHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String string = "";
if(event.getSource() == text1)
{
string = String.format("Field1: %s", event.getActionCommand());
}
else if(event.getSource() == button1)
{
string = String.format("Field2: %s", event.getActionCommand());
}
else if(event.getSource() == button2)
{
string = String.format("Field3: %s", event.getActionCommand());
}
else if(event.getSource() == button3)
{
string = String.format("Pasword Field: %s", event.getActionCommand());
}
JOptionPane.showMessageDialog(null, string);
}
}
}
pendo826
0
Light Poster
Recommended Answers
Jump to PostWhere do you add any buttons to the GUI container object?
Jump to PostHere's another tutorial re GUI
Click HereUse the add() method to add components to a container.
All 8 Replies
NormR1
563
Posting Sage
Team Colleague
pendo826
0
Light Poster
NormR1
563
Posting Sage
Team Colleague
pendo826
0
Light Poster
pendo826
0
Light Poster
NormR1
563
Posting Sage
Team Colleague
pendo826
0
Light Poster
pendo826
0
Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.