如何在actionPerformed中调用paintScreen方法



所以我的任务是添加3个按钮,分别是完全打开百叶窗、完全关闭百叶窗和关闭按钮。在actionPerformed部分,我尝试调用paintScreen方法,以便将矩形的颜色设置为青色或灰色,但我发现这很难。你能帮助并引导我理解我该如何做到这一点吗?

/**
* A basic Java Swing application: a slider can be adjusted to open and close a "blind".
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class WindowBlind extends JFrame 
implements ChangeListener, ActionListener {
/**
* A slider to adjust a "blind"
*/
private JSlider slider;
/**
* To record the current slider/blind setting
*/
private int sliderValue = 0;
/**
* For drawing on
*/
private JPanel panel;    

private JButton closeButton=new JButton("Close Program");  


private JButton openBlindCompletely=new JButton("Open Blinds");  

private JButton closeBlindCompletely=new JButton("Close Blinds");  
/**
* The main method launches the application
* @param args the command line arguments
*/
public static void main(String[] args) {
WindowBlind applic = new WindowBlind();
applic.setLocation(100,100);
applic.setVisible(true);

}
/**
* Constructor - executed at instantiation of the class
* Sets up the application's window
*/
public WindowBlind() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("WindowBlind");
setSize(300,300);

add(closeButton);
add(openBlindCompletely);
add(closeBlindCompletely);
Container window = getContentPane();
window.setLayout(new FlowLayout());     // The default is that JFrame uses BorderLayout
panel = new JPanel() {
// This paintComponent overrides the default one in JPanel (which does nothing).
// paintComponent is called automatically when a screen refresh is needed.
// The screen (Graphics parameter g) has already been cleared before paintComponent is called
public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint the panel's background
paintScreen(g);          // Now do our custom drawing
} // paintComponent
};
panel.setPreferredSize(new Dimension(200, 200));
panel.setBackground(Color.white);
window.add(panel);
slider = new JSlider(JSlider.VERTICAL,0,100,0);
slider.setInverted(true);               // 0 will be at top, not bottom, of vertical slider
window.add(slider);
slider.addChangeListener(this);         // Register for slider event
} // constructor
/**
* Re-draws the window image: a "blind" covering a "window" through which the blue sky can be seen, referring to the global variable sliderValue to determine the blind size
* @param g
*/
public void paintScreen(Graphics g) {
g.setColor(Color.cyan);
g.fillRect(70, 40, 60, 100);         // The blue sky
g.setColor(Color.lightGray);
g.fillRect(70, 40, 60, sliderValue); // The blind, partially closed
g.setColor(Color.black);
g.drawRect(70, 40, 60, 100);   // The window frame

} // paintScreen
/**
* When the slider is adjusted, this method is called automatically
* @param e
*/
public void stateChanged(ChangeEvent e) {
sliderValue = slider.getValue();  // Fetch the slider's current setting
repaint();                        // Force a screen refresh (paintComponent is called indirectly)
} // stateChanged
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == closeButton)
{

}

if (e.getSource() == openBlindCompletely)
{

}

if (e.getSource() == closeBlindCompletely)
{
}       
}        
}

为按钮添加监听器,类似于:

openBlindCompletely.addActionListener(this);

更改滑块值,类似于:

if (e.getSource() == openBlindCompletely) {
slider.setValue(slider.getMinimum());
}

最新更新