📄 example9_4.java
字号:
import java.awt.*;
import java.awt.event.*;
public class Example9_4 extends WindowAdapter implements ActionListener,ItemListener,
KeyListener,MouseListener
{
Frame f;
Button clearButton,printButton;
Label scratchPadLabel,penWidthLabel;
TextArea scratchPad;
Choice shapeChoice;
List colorList;
Canvas c;
TextArea ct;
Checkbox filledCheckbox;
Panel leftPanel;
Panel rightPanel;
Panel buttonPanel;
Panel penWidthPanel;
Scrollbar penWidthSlider;
TextField penWidthDisplay;
int shapeTag=0;
int colorTag=0;
int filledTag=1;
Color MyColor;
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: Add your code here
Example9_4 be=new Example9_4();
be.go();
}
class MyCanvas extends Canvas
{
public void paint(Graphics g)
{
int npoints=3;
int [] xpoints={0,50,80};
int [] ypoints={0,80,50};
if(filledTag==1)
switch(shapeTag){
case 0:g.fillRect(0,0,96,96);
break;
case 1:g.fillOval(0,0,96,96);
break;
case 2:g.fillPolygon(xpoints,ypoints,npoints);
break;
}
else
switch(shapeTag){
case 0:g.drawRect(0,0,96,96);
break;
case 1:g.drawOval(0,0,96,96);
break;
case 2:g.drawPolygon(xpoints,ypoints,npoints);
break;
}
}
}
public void go()
{
f=new Frame("MyFrame");
f.setLayout(new FlowLayout());
f.setSize(600,600);
leftPanel=new Panel();
leftPanel.setLayout(new FlowLayout());
rightPanel=new Panel();
rightPanel.setLayout(new BorderLayout());
buttonPanel=new Panel();
buttonPanel.setLayout(new GridLayout(4,1));
penWidthPanel.setLayout(new BorderLayout());
colorList=new List(8,false);
colorList.add("Red");
colorList.add("Orange");
colorList.add("Yellow");
colorList.add("Green");
colorList.add("Blue");
colorList.add("Purple");
colorList.add("Black");
colorList.add("White");
colorList.addActionListener(this);
colorList.addItemListener(this);
leftPanel.add("west",colorList);
c=new MyCanvas();
c.setSize(100,100);
leftPanel.add(c,"Center");
clearButton=new Button("Clear");
clearButton.addActionListener(this);
printButton=new Button("print");
shapeChoice=new Choice();
shapeChoice.add("Square");
shapeChoice.add("Circle");
shapeChoice.add("Triangle");
shapeChoice.addItemListener(this);
filledCheckbox.addItemListener(this);
buttonPanel.add(clearButton);
buttonPanel.add(printButton);
buttonPanel.add(shapeChoice);
buttonPanel.add(filledCheckbox);
leftPanel.add("east",buttonPanel);
penWidthLabel=new Label("pen width");
penWidthSlider=new Scrollbar(Scrollbar.HORIZONTAL,1,1,1,10);
penWidthDisplay=new TextField("1",8);
penWidthPanel.add("North",penWidthLabel);
penWidthPanel.add("Center",penWidthSlider);
penWidthPanel.add("East",penWidthDisplay);
leftPanel.add("South",penWidthPanel);
scratchPadLabel=new Label("Scratch pad");
rightPanel.add("North",scratchPadLabel);
scratchPad=new TextArea("this is noe a scratch and sniff pad",8,30);
rightPanel.add("South",scratchPad);
f.add(leftPanel);
f.add(rightPanel);
f.addWindowListener(this);
f.setVisible(true);
}
/**
* Method actionPerformed
*
*
*/
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
if(e.getSource()==clearButton)
{
System.exit(0);
}
}
//implement the ItemListener interface intemStateChanged()
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()instanceof List)
{
colorTag=((List)e.getSource()).getSelectedIndex();
}
if(e.getSource()instanceof Checkbox)
{
if(filledTag==1)
filledTag=0;
else filledTag=1;
}
if(e.getSource()instanceof Choice)
{
shapeTag=((Choice)e.getSource()).getSelectedIndex();
c.repaint();
}
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void windowClosing(WindowEvent w){
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -