diff --git a/.vscode/launch.json b/.vscode/launch.json index 45336c5..2a70d79 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,6 +6,20 @@ "configurations": [ { "type": "java", + "name": "SecondGUI2026", + "request": "launch", + "mainClass": "SecondGUI2026", + "projectName": "ASD2024_84d436a2" + }, + { + "type": "java", + "name": "IntListTest", + "request": "launch", + "mainClass": "IntListTest", + "projectName": "ASD2024_84d436a2" + }, + { + "type": "java", "name": "FirstGUI2026", "request": "launch", "mainClass": "FirstGUI2026", diff --git a/FirstGUI2026.java b/FirstGUI2026.java index 01f75bb..911808e 100644 --- a/FirstGUI2026.java +++ b/FirstGUI2026.java @@ -2,10 +2,15 @@ import javax.swing.JButton; import javax.swing.JFrame; +//import java.lang.*; +import javax.swing.JOptionPane; -public class FirstGUI2026 implements java.awt.event.ActionListener { +public class FirstGUI2026 implements java.awt.event.ActionListener, Runnable { JFrame frame; JButton button2; + int sec = 10; + Thread thread; + JButton timerButton; public FirstGUI2026() { frame = new JFrame("First GUI 2026"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -18,6 +23,18 @@ button2 = new MyButton("Java"); button2.addActionListener(this); frame.getContentPane().add(button2); + timerButton = new JButton("Timer: " + sec); + frame.getContentPane().add(timerButton); + timerButton.addActionListener(e -> { thread = new Thread(FirstGUI2026.this) ; thread.start(); }); + timerButton.addActionListener(System.out::println); + // timerButton.addActionListener(new ActionListener(){ + // public void actionPerformed(ActionEvent e){ + // thread = new Thread(FirstGUI2026.this); + // thread.start(); + // } + // }); + + } public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println(e.getActionCommand()+" button clicked!"); @@ -28,6 +45,22 @@ public static void main(String[] args) { new FirstGUI2026(); } + @Override + public void run() { + while (sec > 0) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + sec--; + timerButton.setText("Timer: " + sec); + } + JOptionPane.showMessageDialog(frame, "Time's up!"); + thread = null; + sec = 10; + timerButton.setText("Timer: " + sec); + } } // class MyButton extends JButton { diff --git a/IntListTest.java b/IntListTest.java new file mode 100644 index 0000000..fd08ae6 --- /dev/null +++ b/IntListTest.java @@ -0,0 +1,82 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class IntListTest { + + public static void main(String[] args) { + new IntListTest(); + } + + Scanner scan; + IntListTest(){ + scan = new Scanner(System.in); + AvgIL intlist = new AvgIL(); + System.out.println("Input integer or n|sum|avg|list|exit"); + while(true) { + String in = scan.next(); + if ( in.equals("exit") ) { + System.exit(0); + } else if ( in.equals("list") ) { + intlist.show(); + } else if ( in.equals("n") ) { + System.out.println("n="+intlist.size()); + } else if ( in.equals("sum") ) { + System.out.println("sum="+intlist.getSum()); + } else if ( in.equals("avg") ) { + System.out.println("avg="+intlist.getAvg()); + } else if ( in.matches("[0-9]+") ){ // Int value + int num = Integer.parseInt(in); + intlist.add(num); +// System.out.println("sum="+intlist.getSum()+" avg="+intlist.getAvg()+" n="+intlist.size()); + } + } + } +} + +class IntList { + protected ArrayList intlist; // storage of int values + IntList(){ + intlist = new ArrayList(); + } + public void add(int n) { + intlist.add(n); + } + public int size() { + return intlist.size(); + } + public void show() { + for(int k: intlist) { + System.out.print(k+" "); + } + System.out.println(""); + } +} + +class SumIL extends IntList { + private float sum = 0; + + public void add(int n) { + super.add(n); + // update sum + sum = 0; + for(int k: intlist) { + sum += k; + } + } + public float getSum() { + return sum; + } +} + +class AvgIL extends SumIL { + private float avg = 0; + + public void add(int n) { + super.add(n); + // update avg + avg = getSum() / (float)size(); + } + public float getAvg() { + return avg; + } +} \ No newline at end of file diff --git a/SecondGUI2026.java b/SecondGUI2026.java new file mode 100644 index 0000000..54d9cb5 --- /dev/null +++ b/SecondGUI2026.java @@ -0,0 +1,22 @@ +import javax.swing.JButton; +import javax.swing.JFrame; + +public class SecondGUI2026 extends JFrame { + public SecondGUI2026() { + setTitle("Second GUI 2026"); + setSize(400, 300); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + + JButton button = new JButton("Click Me"); + add(button, java.awt.BorderLayout.CENTER); + JButton button2 = new JButton(" North"); + add(button2, java.awt.BorderLayout.NORTH); + button.addActionListener(e -> System.out.println("Button clicked!")); + button2.addActionListener(e -> System.out.println("Button 2 clicked!")); + } + + public static void main(String[] args) { + new SecondGUI2026(); + } +}