简体   繁体   English

动态添加JPanel Swing

[英]Add JPanel dynamically Swing

Hi I would like to have the following user interface: 嗨,我想拥有以下用户界面:

在此输入图像描述

And when the user clicks on start , I would like to add dynamically a JPanel underneath these elements. 当用户点击start时 ,我想在这些元素下动态添加JPanel Something like this: 像这样的东西: 在此输入图像描述

I am able to generate the grid in an empty JFrame , but when I try to add it when there are more elements inside the JFrame it appears, but very small. 我能够在一个空的JFrame生成网格,但是当我尝试添加它时, JFrame有更多的元素会出现,但非常小。

This is the code that I have tried. 这是我试过的代码。 The class UITable creates the buttons and input text UITable类创建按钮和输入文本

public UITable(){

    jfrm = new JFrame("Plants experiment");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(1000, 1000);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //JLabel titles for input texts
    JLabel jlab_plants = new JLabel(" Enter nº plants: ");
    JLabel jlab_time = new JLabel(" Enter evolution time: ");
    jlab_prueba = new JLabel("");

    //Input texts
    jtf_plants = new JTextField(10);
    jtf_plants.setActionCommand("numPlants");
    jtf_time = new JTextField(10);
    jtf_time.setActionCommand("time");

    //Buttons
    jbtnStart = new JButton("Start");

    //Add components
    jfrm.add(jlab_plants);
    jfrm.add(jtf_plants);
    jfrm.add(jlab_time);
    jfrm.add(jtf_time);
    jfrm.add(jbtnStart);
    jfrm.add(jlab_prueba);

    //Set visibility
    jfrm.setVisible(true);
}

Adding dynamically the grid: 动态添加网格:

@Override
public void actionPerformed(ActionEvent ae) {
    // ...
    this.view.jfrm.add(new Grid());
    this.view.jfrm.revalidate();
    this.view.jfrm.repaint();

}

This is the Grid class: 这是Grid类:

public class Grid extends JPanel{
//Change Point to Plant in order to have a different color for each object
private List<Plant> fillCells;

public Grid() {
    //fillCells = new ArrayList<>(25);
    fillCells = PlantsControler.myPlants;
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    System.out.println("Width: "+getWidth()+" Height: "+ getHeight());
----  //Returns Width: 10 Height: 10 ------
    g.clearRect(0, 0, getWidth(), getHeight());
    for (Plant fillCell : fillCells) {
        int cellX = 10 + (fillCell.getX() * 10);
        int cellY = 10 + (fillCell.getY() * 10);
        g.setColor(fillCell.getColor());
        g.fillRect(cellX, cellY, 10, 10);
    }
    g.setColor(Color.BLACK);
    g.drawRect(10, 10, 800, 500);

    for (int i = 10; i <= 800; i += 10) {
        g.drawLine(i, 10, i, 510);
    }

    for (int i = 10; i <= 500; i += 10) {
        g.drawLine(10, i, 810, i);
    }
}

public void fillCell(int x, int y, Color color) {
    fillCells.add(new Plant(x, y, color));
    repaint();
}

public void fillCell(Plant plant){
    fillCells.add(plant);
    repaint();
}
public void fillCell(){
    repaint();
}

public void clearGrid(){
    fillCells.clear();
}

Thanks in advance!!! 提前致谢!!!

You can try something like below. 你可以试试下面的东西。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class DemoFrame extends JFrame {

    JTextField  field = new JTextField();

    public DemoFrame() {
        setLayout(new BorderLayout());
        JPanel controlsPane = new JPanel(new FlowLayout());
        controlsPane.add(new JLabel("I m a Label"));
        field.setPreferredSize(new Dimension(100,20));
        controlsPane.add(field);
        JButton button = new JButton("I am add drawPanel");
        controlsPane.add(button);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent paramActionEvent) {
                DemoFrame.this.add(new DrawPanel(), BorderLayout.CENTER);
                DemoFrame.this.revalidate();
            }
        });
        add(controlsPane,BorderLayout.NORTH);
    }

    public static void main(String[] args) {
        DemoFrame frame = new DemoFrame();
        frame.setVisible(true);
        frame.pack();
    }

    class DrawPanel extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {
            // TODO Auto-generated method stub
            super.paintComponent(g);
            g.setColor(Color.BLUE);
            g.drawString(field.getText(), this.getWidth()/2, this.getHeight()/2);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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