/** F3 ***** ITKP2-föreläsning3********************************* * En applikation som testkör klassen Fibonacci i ett GUI * * @author Peter Mozelius * @version 1.01 * Rapportera hittade fel till: *mozelius@dsv.su.se ****************************************************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class F3 extends JFrame implements ActionListener { private int antal = 5; private Fibonacci fibo; private Container korg; private JButton knapp; private JTextArea ta; private JPanel nordPanel, sydPanel; private ButtonGroup grupp; private JRadioButton[] radio; public F3() { super("Föreläsning 3"); setSize(200, 300); skapaGUI(); } public void skapaGUI() { korg = getContentPane(); korg.setLayout(new BorderLayout()); sydPanel = new JPanel(); knapp = new JButton("Visa det valda antalet fibonaccital"); knapp.addActionListener(this); sydPanel.add(knapp); korg.add("South", sydPanel); ta = new JTextArea(); ta.setBackground(new Color(255, 255, 225)); ta.setPreferredSize(new Dimension(200, 175)); korg.add("Center", ta); nordPanel = new JPanel(); grupp = new ButtonGroup(); radio = new JRadioButton[10]; for(int i = 0, j = 1; i < 10; i++, j++) { radio[i] = new JRadioButton(""+ j); radio[i].setActionCommand(""+ j); grupp.add(radio[i]); nordPanel.add(radio[i]); } radio[7].setSelected(true); korg.add("North", nordPanel); pack(); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if(e.getSource() == knapp) { antal = Integer.parseInt(grupp.getSelection().getActionCommand()); fibo = new Fibonacci(antal); ta.setText(fibo.toString()); } } public static void main(String[] args) { new F3(); } }//F3 class Fibonacci{ private int antal; private String str=""; public Fibonacci(int antal) { this.antal = antal; } public String toString() { int low = 1; int high = 1; for(int i = 1; i <= antal; i++){ str += "Fibonacci " + i + ": " +"\t" + high + "\n"; high += low; low = high-low; } return str; } }