gpt4 book ai didi

java - 如何通过 ActionListener 类的 actionPerformed() 方法在 Frame 上绘制对象

转载 作者:行者123 更新时间:2023-12-01 14:05:40 25 4
gpt4 key购买 nike

我试图在 ActionListener 类的 actionPerformed() 方法内绘制一个 Shape 对象并将其添加到 InternalFrame 中。

public class InternalFrame extends JInternalFrame{

public JSlider redSlider, greenSlider, blueSlider;
public TextField redField = new TextField();
public TextField greenField = new TextField();
public TextField blueField = new TextField();
Checkbox checkBox = new Checkbox("Filled", true);
String[] shapeNames = {"Oval", "Rectangle", "Line", "NewShape"};

public InternalFrame(String string, boolean b,
boolean c, boolean d, boolean e) {
super(string,b,c,d);

this.setLayout(new BorderLayout());

redSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 256, 32);
greenSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 256, 32);
blueSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 256, 32);

redField.setText(" " + redSlider.getValue());
greenField.setText(" " + greenSlider.getValue());
blueField.setText(" " + blueSlider.getValue());

redSlider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
redField.setText(" " + redSlider.getValue());
//System.out.println("red value: " + redSlider.getValue());
}
});

greenSlider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
greenField.setText(" " + greenSlider.getValue());
}
});

blueSlider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
blueField.setText(" " + blueSlider.getValue());
}
});

JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1, 4, 6, 0));

JComboBox shapesDropDown = new JComboBox(shapeNames);
shapesDropDown.setSelectedIndex(0);

shapesDropDown.addActionListener(new ActionListener (){
@Override
public void actionPerformed(ActionEvent arg0) {
//if(arg0.getSource() == shapeNames[1] ){
MyShape myShape = new MyRectangle();
add(myShape, BorderLayout.CENTER);

System.out.println("HHHH");
//}
}
});

JPanel empty1 = new JPanel();
JPanel empty2 = new JPanel();

topPanel.add(empty1);
topPanel.add(shapesDropDown);
topPanel.add(checkBox);
topPanel.add(empty2);

JPanel sliderPanel = new JPanel();
sliderPanel.setLayout(new GridLayout(3, 3));

sliderPanel.add(new Label("Red:"));
sliderPanel.add(redSlider);
sliderPanel.add(redField);
sliderPanel.add(new Label("Green:"));
sliderPanel.add(greenSlider);
sliderPanel.add(greenField);
sliderPanel.add(new Label("Blue:"));
sliderPanel.add(blueSlider);
sliderPanel.add(blueField);

add(topPanel, BorderLayout.NORTH);
add(sliderPanel, BorderLayout.SOUTH);
}
}

形状对象也被创建,但它没有显示在InternalFrame上。

最佳答案

不要扩展JInternalFrame,而是使用工厂方法来创建内部框架及其内容。在此examplecreateInternalFrame() 在构造函数中调用,但也可以从 ActionListener 调用。请注意,内部框架的默认布局是 BorderLayout,默认位置是 CENTER;在调整容器的大小和可见性之前,请务必先pack()容器。

image

关于java - 如何通过 ActionListener 类的 actionPerformed() 方法在 Frame 上绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18930047/

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