gpt4 book ai didi

java - 动态添加JPanel Swing

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:37 26 4
gpt4 key购买 nike

您好,我想要以下用户界面:

enter image description here

当用户单击开始时,我想在这些元素下动态添加一个JPanel。像这样的东西: enter image description here

我能够在空的 JFrame 中生成网格,但是当我尝试在 JFrame 内有更多元素时添加它时,它会出现,但非常小。

这是我尝试过的代码。 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);
}

动态添加网格:

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

}

这是网格类:

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();
}

提前致谢!!!

最佳答案

您可以尝试如下所示的操作。

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);
}
}
}

关于java - 动态添加JPanel Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37991561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com