0

Was trying to do an ActionListener JButton, but the size of the button does not change to what I want to, it follows the size of the frame. I am very new and read many tutorials on that, seems right, but it doesn’t work anyways, pls help.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TimeTester extends JPanel implements ActionListener {

   private int x = 10;
   private static TimeTester test = new TimeTester();
   private static JFrame frame = new JFrame();
   private static JButton button = new JButton("Next Frame");

   public static void main(String[] args) {
       button.setLocation(100, 100);
       button.setVisible(true);
       button.addActionListener(test);
       button.setMaximumSize(new Dimension(50, 60));
       frame.add(button);
       frame.setSize(500, 500);
       frame.setVisible(true);
       frame.setLayout(null);
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       frame.setResizable(false);
       frame.setLocationRelativeTo(null);
       frame.add(test);
       System.out.println(test.calcTime());
   }

   @Override
   public void actionPerformed(ActionEvent e) {

   }

   private synchronized long calcTime(){
       double a;
       long start = System.nanoTime();

       long end = System.nanoTime();
       return (end - start);
   }

   @Override
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.setColor(Color.black);
       g.drawLine(x, x, 100 + x, 100 + x);
   }
}

Anonymous Asked question May 13, 2021