publicclassWayextendsJPanel{JButtonred;publicWay(){setBackground(Color.yellow);setPreferredSize(newDimension(320,260));JPanelbtnPanel=newJPanel();btnPanel.setLayout(newFl...
public class Way extends JPanel
{
JButton red;
public Way(){
setBackground(Color.yellow);
setPreferredSize(new Dimension(320,260));
JPanel btnPanel=new JPanel();
btnPanel.setLayout(new FlowLayout());
red.setLayout(new FlowLayout());// 将单选按钮加入按钮面板
red.add(red);
add(red);
}
private void lightRed(Graphics g)
{
g.setColor(Color.red);
g.fillOval(getWidth() / 2, 30, 15, 15);
g.setColor(Color.black);
g.drawOval(getWidth() / 2, 50, 15, 15);
g.drawOval(getWidth() / 2, 70, 15, 15);
}
private void lightGreen(Graphics g)
{
g.setColor(Color.green);
g.fillOval(getWidth() / 2, 50, 15, 15);
g.setColor(Color.black);
g.drawOval(getWidth() / 2, 30, 15, 15);
g.drawOval(getWidth() / 2, 70, 15, 15);
}
private void lightYellow(Graphics g)
{
g.setColor(Color.yellow);
g.fillOval(getWidth() / 2, 70, 15, 15);
g.setColor(Color.black);
g.drawOval(getWidth() / 2, 30, 15, 15);
g.drawOval(getWidth() / 2, 50, 15, 15);
}
protected void paintComponent(Graphics g)
{
super.paintComponents(g);
Way a=new Way();
g.clearRect(0, 0, getWidth(), getHeight());
g.drawRect(getWidth() / 2, 20, 30, 80);
red.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Graphics g=getGraphics();
int f1=0;
f1++;
switch(f1)
{
case 0:
a.lightRed(g);
break;
case 1:
a.lightGreen(g);
break;
case 2:
a.lightYellow(g);
break;
}
};
public static void main(String[] args)
{
JFrame fr =new JFrame("邮件界面模拟");
fr.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE); // 点击x结束程序
Container contentPane=fr.getContentPane();
//得到窗口内容面板
contentPane.add(new Way());
fr.pack();
fr.setVisible(true); //设置窗口可见
}
});
}}
展开