0
我剛做了這個程序,我在面板上添加了一個按鈕,但我無法繼續使用actionListener來使按鈕正常工作。他們應該是面板上出現的圖片,當點擊該按鈕時,圖片應該變成另一張圖片。請幫助我,謝謝!這是我的代碼。簡單的GUI程序
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawPanelThree extends JPanel
{
private JButton button;
public DrawPanelThree()
{
button = new JButton();
setLayout(new BorderLayout());
add(button, BorderLayout.SOUTH);
button.setText("Start");
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawRect(90, 40, 100, 50);
g.setColor(Color.RED);
g.fillRect(10, 10, 10, 10);
g.fillRect(260, 10, 10, 10);
g.fillRect(10, 120, 10, 10);
g.fillRect(260, 120, 10, 10);
g.setColor(new Color(255, 215, 0));
g.fillOval(120, 45, 40, 40);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("Rectangle");
frame.setSize(new Dimension(300, 200));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawPanelThree panel = new DrawPanelThree();
frame.add(panel);
panel.setBackground(Color.CYAN);
frame.setVisible(true);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
}
}