Добавление JButton - Java
Формулировка задачи:
есть код, где двигается шар по фрейму. Когда добавляю кнопку то все меркнет и становиться ничего не видно. Как сделать, чтобы кнопка была на виду?
Листинг программы
- package com.company;
- import com.sun.corba.se.impl.orbutil.graph.Graph;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class MainFrame extends JFrame implements ActionListener
- {
- int bx=200;
- int by=200;
- int speedx=10;
- double speedy=10;
- int bsize=20;
- Timer tm = new Timer(30, this);
- public void actionPerformed(ActionEvent arg0)
- {
- Graphics g = (Graphics)this.getGraphics();
- this.update(g);
- g.translate(0, 0);
- g.fillOval(bx, by, bsize, bsize);
- bx+=speedx;
- by+=speedy;
- if (bx < bsize-10)
- {
- speedx=-speedx;
- }
- if (bx>this.getWidth()-30)
- {
- speedx=-speedx;
- }
- if (by < 33)
- {
- speedy=-speedy;
- }
- if (by>this.getHeight()-30)
- {
- speedy=Math.random()*10;
- speedy=-speedy;
- }
- }
- public MainFrame()
- {
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(500, 500);
- setVisible(true);
- tm.start();
- getContentPane().setBackground(Color.yellow);
- }
- public static void main(String[] args)
- {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new MainFrame();
- }
- });
- }
- }
Решение задачи: «Добавление JButton»
textual
Листинг программы
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class MainFrame extends JFrame {
- private JButton jButton = new JButton("Button");
- private MPanel jPanel = new MPanel();
- Timer tm = new Timer(30, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- repaint();
- //tm.start();
- }
- });
- private class MPanel extends JPanel {
- int bx = 200;
- int by = 200;
- int speedx = 10;
- double speedy = 10;
- int bsize = 20;
- public MPanel() {
- setPreferredSize(new Dimension(500, 450));
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.translate(0, 0);
- g.fillOval(bx, by, bsize, bsize);
- bx += speedx;
- by += speedy;
- if (bx < bsize - 10) {
- speedx = -speedx;
- }
- if (bx > this.getWidth() - 30) {
- speedx = -speedx;
- }
- if (by < 33) {
- speedy = -speedy;
- }
- if (by > this.getHeight() - 30) {
- speedy = Math.random() * 10;
- speedy = -speedy;
- }
- }
- }
- private class BListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (jPanel.by < 50) {
- jPanel.by = +50;
- }
- }
- }
- public MainFrame() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(500, 500);
- setVisible(true);
- tm.start();
- jButton.addActionListener(new BListener());
- add(jButton, BorderLayout.NORTH);
- jPanel.setBackground(Color.yellow);
- add(jPanel);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new MainFrame();
- }
- });
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д