我有一个创造笑脸的课程。当我运行下面的代码时,一切都工作了,创建了9个笑脸对象并显示在网格布局中。但是,我想采用循环9次的代码块并创建/显示9个表情符号,并在单击creatSmiley按钮时调用它。
然而,当我将那段代码移动到actionEvent时,当我运行代码并按下按钮时没有任何反应。
创建对象的笑脸类扩展了jpanel并使用paint方法创建了一个笑脸。
任何提示?
public class SmileyGrid extends JFrame implements ActionListener
{
private JPanel buttonPanel, panel, facePanel;
private JButton pumpkinButton, smileyButton;
public static void main(String[] args)
{
SmileyGrid myGrid = new SmileyGrid();
myGrid.setSize(700, 700);
myGrid.setLayout(new BorderLayout());
myGrid.createGUI();
myGrid.setVisible(true);
}
public SmileyGrid()
{
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout (new BorderLayout());
facePanel = new JPanel();
facePanel.setLayout(new GridLayout(3,3));
facePanel.setPreferredSize(new Dimension(700, 700));
add(facePanel);
smileyButton = new JButton("Create Smiley");
pumpkinButton = new JButton("Create Pumpkin");
smileyButton.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.setBackground(Color.white);
add (buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(smileyButton);
buttonPanel.add(pumpkinButton);
for(int i=0;i<9;i++)
{
Random r = new Random();
Color color1=new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
Color color2=new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
Color color3=new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
Color color4=new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
Smiley mySmiley = new Smiley(color1, color2, color3, color4);
facePanel.add(mySmiley);
}
}
public void actionPerformed(ActionEvent ae)
{
}
}
答案 0 :(得分:0)
所以,我修改了你的基本代码,因为你没有包含Smiley
类,将for-loop
移到ActionListener
&#39; s {{ 1}}方法,在我创建了一堆新的&#34;虚假的表情符号后,在actionPerformed
上调用revalidate
和repaint
&#34;它对我来说效果很好
facePanel
如果您有任何其他问题,那么可能是您的import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SmileyGrid extends JFrame implements ActionListener {
private JPanel buttonPanel, panel, facePanel;
private JButton pumpkinButton, smileyButton;
public static void main(String[] args) {
SmileyGrid myGrid = new SmileyGrid();
myGrid.setSize(700, 700);
myGrid.setLayout(new BorderLayout());
myGrid.createGUI();
myGrid.setVisible(true);
}
public SmileyGrid() {
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout());
facePanel = new JPanel();
facePanel.setLayout(new GridLayout(3, 3));
facePanel.setPreferredSize(new Dimension(700, 700));
add(facePanel);
smileyButton = new JButton("Create Smiley");
pumpkinButton = new JButton("Create Pumpkin");
smileyButton.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.setBackground(Color.white);
add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(smileyButton);
buttonPanel.add(pumpkinButton);
// for (int i = 0; i < 9; i++) {
// Random r = new Random();
// Color color1 = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
// Color color2 = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
// Color color3 = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
// Color color4 = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
//
// Smiley mySmiley = new Smiley(color1, color2, color3, color4);
//
// facePanel.add(mySmiley);
// }
}
@Override
public void actionPerformed(ActionEvent ae) {
for (int i = 0; i < 9; i++) {
Random r = new Random();
Color color1 = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
Color color2 = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
Color color3 = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
Color color4 = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
// Smiley mySmiley = new Smiley(color1, color2, color3, color4);
JPanel mySmiley = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(20, 20);
}
};
mySmiley.setBackground(color1);
facePanel.add(mySmiley);
}
facePanel.revalidate();
facePanel.repaint();
}
}
班级