简体   繁体   中英

Dynamically create JPanels

I am trying to make a GUI that has a JTabbedPane and shows connections between radios on an RF network. I am not able to figure out how to make the tabbedPanes create dynamically. I have tried lots of things without success. Here is what I have so far.

This is the draw class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class draw extends JPanel implements MouseListener{

    private Color currentColor = Color.RED;

    //Array to hold the grid dimensions used for drawing radio[]
    private int[] gD = {40, 100, 160, 220, 280, 340, 400, 460};

    //Flag for changing colors in the grid
    private boolean colorChanged = false;

    public void drawing() {
        repaint();
    }

    public void paintComponent(final Graphics g) {
        super.paintComponent(g);
        final Graphics2D g2d = (Graphics2D)g;

        //Smooth Graphics
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        //Quality Color Rendering
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
                RenderingHints.VALUE_COLOR_RENDER_QUALITY);

        //Array to hold 40 radios
        final Ellipse2D.Double radio[] = new Ellipse2D.Double[39];

        //Array to hold lines that show radio connections
        final Line2D connection[] = new Line2D[49];

        /****************************************************************
        This section of code draws the Ellipses that represent the
        different radios.
        *****************************************************************/
        radio[0] = new Ellipse2D.Double(gD[2], gD[0], gD[0], gD[0]);
        g2d.setPaint(currentColor);
        g2d.fill(radio[0]);
        this.addMouseListener(this);

        radio[1] = new Ellipse2D.Double(gD[1], gD[1], gD[0], gD[0]);
        g2d.setPaint(currentColor);
        g2d.fill(radio[1]);
        this.addMouseListener(this);

        radio[2] = new Ellipse2D.Double(gD[0], gD[2], gD[0], gD[0]);
        g2d.setPaint(currentColor);
        g2d.fill(radio[2]);
        this.addMouseListener(this);

        radio[3] = new Ellipse2D.Double(gD[1], gD[3], gD[0], gD[0]);
        g2d.setPaint(currentColor);
        g2d.fill(radio[3]);
        this.addMouseListener(this);

        radio[4] = new Ellipse2D.Double(gD[2], gD[4], gD[0], gD[0]);
        g2d.setPaint(currentColor);
        g2d.fill(radio[4]);
        this.addMouseListener(this);

        radio[5] = new Ellipse2D.Double(gD[5], gD[0], gD[0], gD[0]);
        g2d.setPaint(currentColor);
        g2d.fill(radio[5]);
        this.addMouseListener(this);

        radio[6] = new Ellipse2D.Double(gD[6], gD[1], gD[0], gD[0]);
        g2d.setPaint(currentColor);
        g2d.fill(radio[6]);
        this.addMouseListener(this);

        radio[7] = new Ellipse2D.Double(gD[7], gD[2], gD[0], gD[0]);
        g2d.setPaint(currentColor);
        g2d.fill(radio[7]);
        this.addMouseListener(this);

        radio[8] = new Ellipse2D.Double(gD[6], gD[3], gD[0], gD[0]);
        g2d.setPaint(currentColor);
        g2d.fill(radio[8]);
        this.addMouseListener(this);

        radio[9] = new Ellipse2D.Double(gD[5], gD[4], gD[0], gD[0]);
        g2d.setPaint(currentColor);
        g2d.fill(radio[9]);
        this.addMouseListener(this);

        /****************************************************************
         This section of code draws the lines that represent the
         connections to the different radios.
        *****************************************************************/
        connection[0] = new Line2D.Double(radio[0].getCenterX(), radio[0].getCenterY(), radio[9].getCenterX(), radio[9].getCenterY());
        g2d.draw(connection[0]);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                /**************************************************************************
                 This block of statements changes the radio buttons to green when clicked
                 and sets the colorChanged flag to true.
                 *************************************************************************/
                if (!colorChanged && radio[0].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.GREEN;
                    colorChanged = true;
                    repaint(gD[2], gD[0], gD[0], gD[0]);
                } else if (!colorChanged && radio[1].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.GREEN;
                    colorChanged = true;
                    repaint(gD[1], gD[1], gD[0], gD[0]);
                } else if (!colorChanged && radio[2].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.GREEN;
                    colorChanged = true;
                    repaint(gD[0], gD[2], gD[0], gD[0]);
                } else if (!colorChanged && radio[3].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.GREEN;
                    colorChanged = true;
                    repaint(gD[1], gD[3], gD[0], gD[0]);
                } else if (!colorChanged && radio[4].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.GREEN;
                    colorChanged = true;
                    repaint(gD[2], gD[4], gD[0], gD[0]);
                } else if (!colorChanged && radio[5].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.GREEN;
                    colorChanged = true;
                    repaint(gD[5], gD[0], gD[0], gD[0]);
                } else if (!colorChanged && radio[6].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.GREEN;
                    colorChanged = true;
                    repaint(gD[6], gD[1], gD[0], gD[0]);
                } else if (!colorChanged && radio[7].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.GREEN;
                    colorChanged = true;
                    repaint(gD[7], gD[2], gD[0], gD[0]);
                } else if (!colorChanged && radio[8].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.GREEN;
                    colorChanged = true;
                    repaint(gD[6], gD[3], gD[0], gD[0]);
                } else if (!colorChanged && radio[9].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.GREEN;
                    colorChanged = true;
                    repaint(gD[5], gD[4], gD[0], gD[0]);

                /***************************************************************************
                 This block of statements changes the radio buttons to red when clicked
                 and sets the colorChanged flag to false
                ***************************************************************************/
                } else if (colorChanged && radio[0].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.RED;
                    colorChanged = false;
                    repaint(gD[0], gD[0], gD[0], gD[0]);
                } else if (colorChanged && radio[1].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.RED;
                    colorChanged = false;
                    repaint(gD[0], gD[1], gD[0], gD[0]);
                } else if (colorChanged && radio[2].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.RED;
                    colorChanged = false;
                    repaint(gD[0], gD[2], gD[0], gD[0]);
                } else if (colorChanged && radio[3].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.RED;
                    colorChanged = false;
                    repaint(gD[0], gD[3], gD[0], gD[0]);
                } else if (colorChanged && radio[4].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.RED;
                    colorChanged = false;
                    repaint(gD[0], gD[4], gD[0], gD[0]);
                } else if (colorChanged && radio[5].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.RED;
                    colorChanged = false;
                    repaint(gD[7], gD[0], gD[0], gD[0]);
                } else if (colorChanged && radio[6].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.RED;
                    colorChanged = false;
                    repaint(gD[7], gD[1], gD[0], gD[0]);
                } else if (colorChanged && radio[7].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.RED;
                    colorChanged = false;
                    repaint(gD[7], gD[2], gD[0], gD[0]);
                } else if (colorChanged && radio[8].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.RED;
                    colorChanged = false;
                    repaint(gD[7], gD[3], gD[0], gD[0]);
                } else if (colorChanged && radio[9].contains(e.getX(), e.getY())) {
                    e.getSource();
                    currentColor = Color.RED;
                    colorChanged = false;
                    repaint(gD[7], gD[4], gD[0], gD[0]);
                }
        }
    });
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent mouseEvent) {

    }

    @Override
    public void mouseReleased(MouseEvent mouseEvent) {

    }

    @Override
    public void mouseEntered(MouseEvent mouseEvent) {

    }

    @Override
    public void mouseExited(MouseEvent mouseEvent) {

    }
}

This is the frontPage class

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.*;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JFrame;


public class FrontPage extends JFrame{

    private JPanel rootPanel;
    private JButton buttonPushConfiguration;
    private JTabbedPane tabbedPane;

    public FrontPage() {

        this.setBounds(new Rectangle(0, 0, 600, 600));
        setContentPane(rootPanel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        draw object = new draw();

        //Creates the first tab
        tabbedPane.addTab("Switch 1", object);

        object.drawing();


        buttonPushConfiguration.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(FrontPage.this, "Configuration Pushed to Panel");
            }

        });
    }
}

This is the main class

import javax.swing.*;

public class Main {
    public static void main(String[] args) {

        //Set the theme to the current system theme.
        try {

            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        }
        catch(UnsupportedLookAndFeelException e){

            // handle exception
        }
        catch(ClassNotFoundException e){

            // handle exception
        }
        catch(InstantiationException e){

            // handle exception
        }
        catch(IllegalAccessException e){
        }

        //Instantiates FrontPage
        new FrontPage();

    }
}

If your goal is to add a new JPanel to your JTabbedPane within the button's ActionListener, then that's exactly what you will need to do, something like:

  @Override
  public void actionPerformed(ActionEvent e) {
     // int variable for the tab title
     tabIndex++;
     String title = "Switch " + tabIndex; 

     // I renamed your draw class to conform to Java standards
     DrawPanel tabComponent = new DrawPanel();
     tabbedPane.add(title, tabComponent);
  }

If you need to do more, please tell us. I also must reiterate my comments made to your original post:

  • You're adding a MouseListener within a painting method (here paintComponent) which is something you should never do. This will likely add many MouseListeners, an uncontrollable amount really, to the JPanel when you really only want just one. Do only painting within a paint or paintComponent method and nothing more.
  • Your code above compiles but will throw NullPointerExceptions from many locations as you're trying to use uninitialized components. Please try to fix this up before posting here.
  • Also your Ellipse2D and Line2D array should be made instance fields and not be local to the paintComponent method. Also, you're re-adding your MouseListener umpteen million times within paintComponent, not just once.
  • You appear to be trying to solve too many problems all at once, causing and multiplying bad code. Consider changing how you're creating this program -- start simple, start from first principles, solve small issues one at a time and in isolation, and only then put the items together into one GUI

An example of a MouseListener changing a spot's colors:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.*;

@SuppressWarnings("serial")
public class DrawPanelMain extends JPanel {  

   private static final int PREF_W = 600;
   private static final int PREF_H = PREF_W;
   private List<Point> POINT_LIST = Arrays.asList(new Point[] {
         new Point(160, 40),
         new Point(100, 100),
         new Point(40, 160),
         new Point(100, 220),
         new Point(160, 280),
         new Point(340, 40),
         new Point(400, 100),
         new Point(460, 160),
         new Point(400, 220),
         new Point(340, 280)
   });
   private JTabbedPane tabbedPane = new JTabbedPane();
   private int tabIndex = 0;

   public DrawPanelMain() {
      JPanel btnPanel = new JPanel();
      btnPanel.add(new JButton(new PushConfigAction("Push Config")));

      setLayout(new BorderLayout());
      add(tabbedPane, BorderLayout.CENTER);
      add(btnPanel, BorderLayout.PAGE_END);
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private class PushConfigAction extends AbstractAction {
      public PushConfigAction(String name) {
         super(name);
         int mnemonic = (int) name.charAt(0);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         tabIndex++;
         String title = "Switch " + tabIndex;
         DrawPanel2 tabComponent = new DrawPanel2(POINT_LIST);
         tabbedPane.add(title, tabComponent);
      }
   }

   private static void createAndShowGui() {
      DrawPanelMain mainPanel = new DrawPanelMain();

      JFrame frame = new JFrame("DrawPanelMain");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

@SuppressWarnings("serial")
class DrawPanel2 extends JPanel {
   private static final int OVAL_WIDTH = 40;
   private static final Color INACTIVE_COLOR = Color.RED;
   private static final Color ACTIVE_COLOR = Color.green;
   private List<Point> points;
   private List<Ellipse2D> ellipses = new ArrayList<>();
   private Map<Ellipse2D, Color> ellipseColorMap = new HashMap<>();

   public DrawPanel2(List<Point> points) {
      this.points = points;
      for (Point p : points) {
         int x = p.x - OVAL_WIDTH / 2;
         int y = p.y - OVAL_WIDTH / 2;
         int w = OVAL_WIDTH;
         int h = OVAL_WIDTH;
         Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
         ellipses.add(ellipse);
         ellipseColorMap.put(ellipse, INACTIVE_COLOR);
      }

      MyMouseAdapter mListener = new MyMouseAdapter();
      addMouseListener(mListener);
      addMouseMotionListener(mListener);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      for (Ellipse2D ellipse : ellipses) {
         g2.setColor(ellipseColorMap.get(ellipse));
         g2.fill(ellipse);
      }
   }

   private class MyMouseAdapter extends MouseAdapter {
      @Override
      public void mousePressed(MouseEvent e) {
         for (Ellipse2D ellipse : ellipses) {
            if (ellipse.contains(e.getPoint())) {
               Color c = ellipseColorMap.get(ellipse);
               c =  (c == INACTIVE_COLOR) ? ACTIVE_COLOR : INACTIVE_COLOR;
               ellipseColorMap.put(ellipse, c);
            }
         }
         repaint();
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM