[英]display object from separate class using actionListener
我有一堂課,創造一個笑臉。 當我在下面運行代碼時,一切正常,並創建了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)
{
}
}
因此,我修改了您的基本代碼,因為您沒有包括Smiley
類,而是將for-loop
移至ActionListener
的actionPerformed
方法,並在創建一堆新的“”后在facePanel
上調用revalidate
和repaint
假笑臉”對我來說效果很好
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();
}
}
如果您還有其他問題,可能與您的Smiley
班有關
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.