Добавление JButton - Java

Узнай цену своей работы

Формулировка задачи:

есть код, где двигается шар по фрейму. Когда добавляю кнопку то все меркнет и становиться ничего не видно. Как сделать, чтобы кнопка была на виду?
Листинг программы
  1. package com.company;
  2. import com.sun.corba.se.impl.orbutil.graph.Graph;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. public class MainFrame extends JFrame implements ActionListener
  7. {
  8. int bx=200;
  9. int by=200;
  10. int speedx=10;
  11. double speedy=10;
  12. int bsize=20;
  13. Timer tm = new Timer(30, this);
  14. public void actionPerformed(ActionEvent arg0)
  15. {
  16. Graphics g = (Graphics)this.getGraphics();
  17. this.update(g);
  18. g.translate(0, 0);
  19. g.fillOval(bx, by, bsize, bsize);
  20. bx+=speedx;
  21. by+=speedy;
  22. if (bx < bsize-10)
  23. {
  24. speedx=-speedx;
  25. }
  26. if (bx>this.getWidth()-30)
  27. {
  28. speedx=-speedx;
  29. }
  30. if (by < 33)
  31. {
  32. speedy=-speedy;
  33. }
  34. if (by>this.getHeight()-30)
  35. {
  36. speedy=Math.random()*10;
  37. speedy=-speedy;
  38. }
  39. }
  40. public MainFrame()
  41. {
  42. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43. this.setSize(500, 500);
  44. setVisible(true);
  45. tm.start();
  46. getContentPane().setBackground(Color.yellow);
  47. }
  48. public static void main(String[] args)
  49. {
  50. SwingUtilities.invokeLater(new Runnable() {
  51. @Override
  52. public void run() {
  53. new MainFrame();
  54. }
  55. });
  56. }
  57. }

Решение задачи: «Добавление JButton»

textual
Листинг программы
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class MainFrame extends JFrame {
  7.     private JButton jButton = new JButton("Button");
  8.     private MPanel jPanel = new MPanel();
  9.  
  10.     Timer tm = new Timer(30, new ActionListener() {
  11.         @Override
  12.         public void actionPerformed(ActionEvent e) {
  13.             repaint();
  14.             //tm.start();
  15.         }
  16.     });
  17.  
  18.     private class MPanel extends JPanel {
  19.         int bx = 200;
  20.         int by = 200;
  21.         int speedx = 10;
  22.         double speedy = 10;
  23.         int bsize = 20;
  24.  
  25.         public MPanel() {
  26.             setPreferredSize(new Dimension(500, 450));
  27.         }
  28.  
  29.         @Override
  30.         public void paintComponent(Graphics g) {
  31.             super.paintComponent(g);
  32.             g.translate(0, 0);
  33.             g.fillOval(bx, by, bsize, bsize);
  34.             bx += speedx;
  35.             by += speedy;
  36.  
  37.             if (bx < bsize - 10) {
  38.                 speedx = -speedx;
  39.             }
  40.             if (bx > this.getWidth() - 30) {
  41.                 speedx = -speedx;
  42.             }
  43.             if (by < 33) {
  44.                 speedy = -speedy;
  45.             }
  46.             if (by > this.getHeight() - 30) {
  47.                 speedy = Math.random() * 10;
  48.                 speedy = -speedy;
  49.             }
  50.         }
  51.     }
  52.  
  53.     private class BListener implements ActionListener {
  54.         @Override
  55.         public void actionPerformed(ActionEvent e) {
  56.             if (jPanel.by < 50) {
  57.                 jPanel.by = +50;
  58.             }
  59.         }
  60.     }
  61.  
  62.  
  63.     public MainFrame() {
  64.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65.         setSize(500, 500);
  66.         setVisible(true);
  67.         tm.start();
  68.         jButton.addActionListener(new BListener());
  69.         add(jButton, BorderLayout.NORTH);
  70.         jPanel.setBackground(Color.yellow);
  71.         add(jPanel);
  72.     }
  73.  
  74.     public static void main(String[] args) {
  75.         SwingUtilities.invokeLater(new Runnable() {
  76.             @Override
  77.             public void run() {
  78.                 new MainFrame();
  79.             }
  80.         });
  81.     }
  82. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 4.333 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут