[英]Dynamically Adding Components to JPanel on click of JButton
This is my block class: 这是我的块类:
public class Block extends JComponent {
public int width, height;
public Color colour;
public Block( int width, int height, Color colour) {
super();
setSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
setBackground(colour);
this.width = width;
this.height = height;
this.colour = colour;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(colour);
g.fillRect(0, 0, width, height);
}
}
And my ball class: 我的球课:
public class Ball extends JComponent {
public int width, height;
public Color colour;
public Ball(int width, int height, Color colour) {
super();
setSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
setBackground(colour);
this.width = width;
this.height = height;
this.colour = colour;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(colour);
g.fillOval(0, 0, width, height);
}
}
And this is my Main class: 这是我的主班:
public class Main extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setContentPane(new Main());
frame.setSize(new Dimension(1000, 1000));
frame.setPreferredSize(new Dimension(1000, 1000));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Main() {
JComboBox<String> shapes = new JComboBox(new String[]{"Square", "Oval"});
JSpinner width = new JSpinner();
SpinnerNumberModel widthModel = new SpinnerNumberModel();
widthModel.setMinimum(1);
widthModel.setMaximum(200);
width.setModel(widthModel);
JSpinner height = new JSpinner();
SpinnerNumberModel heightModel = new SpinnerNumberModel();
heightModel.setMinimum(1);
heightModel.setMaximum(200);
height.setModel(heightModel);
JButton submit = new JButton("Add Shape");
submit.addActionListener((ActionEvent e) -> {
if (shapes.getSelectedItem().equals("Square")) {
add(new Block((Integer)widthModel.getValue(), (Integer)heightModel.getValue(), Color.BLUE));
} else {
add(new Ball((Integer)widthModel.getValue(), (Integer)heightModel.getValue(), Color.BLUE));
}
});
add(shapes);
add(width);
add(height);
add(submit);
}
}
When you click the "submit" button, it should add either a square or an oval (depending on your choice), with the height and width of which you specify in the spinners. 单击“提交”按钮时,它应添加一个正方形或一个椭圆形(取决于您的选择),其高度和宽度在微调器中指定。 However, it does not have anything. 但是,它没有任何内容。 I printed out the width and height and they were valid, so there is no problem with that. 我打印出了宽度和高度,它们是有效的,因此没有问题。 Also, I tried adding the custom components by themselves and it worked. 另外,我尝试自己添加自定义组件,并且可以正常工作。
You've forget 2 important things: 您忘记了2件重要的事情:
getPreferredSize()
for Brick
and Ball
您需要为Brick
和Ball
定义方法getPreferredSize()
revalidate()
and repaint()
您需要使用revalidate()
和repaint()
方法更新布局 Here is the fixed code: 这是固定代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
public class Main extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setContentPane(new Main());
frame.setSize(new Dimension(1000, 1000));
frame.setPreferredSize(new Dimension(1000, 1000));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Main() {
JComboBox<String> shapes = new JComboBox(new String[] {"Square", "Oval"});
JSpinner width = new JSpinner();
SpinnerNumberModel widthModel = new SpinnerNumberModel();
widthModel.setMinimum(1);
widthModel.setMaximum(200);
width.setModel(widthModel);
JSpinner height = new JSpinner();
SpinnerNumberModel heightModel = new SpinnerNumberModel();
heightModel.setMinimum(1);
heightModel.setMaximum(200);
height.setModel(heightModel);
JButton submit = new JButton("Add Shape");
submit.addActionListener(e -> {
if (shapes.getSelectedItem().equals("Square")) {
add(new Block((Integer) widthModel.getValue(), (Integer) heightModel.getValue(), Color.BLUE));
} else {
add(new Ball((Integer) widthModel.getValue(), (Integer) heightModel.getValue(), Color.BLUE));
}
revalidate();
repaint();
});
add(shapes);
add(width);
add(height);
add(submit);
}
public static class Block extends JComponent {
public int width, height;
private final Dimension size;
public Color colour;
public Block(int width, int height, Color colour) {
super();
setSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
setBackground(colour);
this.width = width;
this.height = height;
this.colour = colour;
this.size = new Dimension(width, height);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(colour);
g.fillRect(0, 0, width, height);
}
@Override
public Dimension getPreferredSize() {
return size;
}
}
public class Ball extends JComponent {
public int width, height;
private final Dimension size;
public Color colour;
public Ball(int width, int height, Color colour) {
super();
setSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
setBackground(colour);
this.width = width;
this.height = height;
this.colour = colour;
this.size = new Dimension(width, height);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(colour);
g.fillOval(0, 0, width, height);
}
@Override
public Dimension getPreferredSize() {
return size;
}
}
}
添加组件后,调用repaint()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.