Sat Jan 24, 2009 2:04 am
import javax.swing.JFrame;
public class Driver09
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Sum the Series");
frame.setSize(200, 200);
frame.setLocation(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Panel09());
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Panel09 extends JPanel
{
private JLabel label;
private double total;
private JButton we;
public Panel09()
{
setLayout(new BorderLayout());
total = 0.0;
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 3, 10, 0));
add(panel, BorderLayout.CENTER);
addButton(panel, "+1.00", 1.0);
addButton(panel, "+0.10", 0.1);
addButton(panel, "+0.01", 0.01);
addButton(panel, "-1.00", -1.0);
addButton(panel, "-0.10", -0.1);
addButton(panel, "-0.01", -0.01);
label = new JLabel("$0.00");
label.setFont(new Font("Serif", Font.BOLD, 30));
label.setHorizontalAlignment(SwingConstants.CENTER);
add(label, BorderLayout.NORTH);
}
private void addButton(JPanel panel, String s, double x)
{
we = new JButton();
we.addActionListener(Listener(x));
add(we);
}
private class Listener implements ActionListener
{
private double myX;
public Listener(double x)
{
myX = x;
}
public void actionPerformed(ActionEvent e)
{
double s = Double.parseDouble(label.getText());
s = s + myX;
label.setText("$:"+s);
}
}
}
Sat Jan 24, 2009 10:51 am
we.addActionListener(Listener(x));
we.addActionListener(new Listener(x));
Tue Jan 27, 2009 2:57 am
Tue Jan 27, 2009 10:13 am
setLayout(new BorderLayout());
|
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.