将图形添加到子JPANEL和按钮到其他

时间:2014-01-21 14:48:43

标签: java swing graphics jpanel jbutton

我有一个代码,需要按钮和文本字段来显示上面的图形,但不明白JPANELS在图形隐藏theese选项后,我将使用if语句,将采取季节和绘制图像

import java.awt.*;
import java.applet.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class Weather extends JApplet implements ActionListener
    {

    JButton Enter , Exit;
    JTextField location;
    JLabel city;
    JRadioButton time;
    JComboBox Seasons;

      Random rand = new Random();
      int P = rand.nextInt(100) + 1;//Random Precipitation
       int  H = rand.nextInt(50) + 1;//Random Heat



    public void init ()
    {
             getContentPane() .setLayout (new FlowLayout ());

        Enter =new JButton ("Enter");
        Exit =new JButton ("exit");



        city = new JLabel ("What city?");
        location = new JTextField (20); //location entry field


        Seasons  = new JComboBox ();
        Seasons.addItem ("Summer");
        Seasons.addItem ("Fall");
        Seasons.addItem ("Winter");
        Seasons.addItem ("Spring");


        time = new JRadioButton ("check if night?");


       getContentPane().add (city);
       getContentPane().add (location);
       getContentPane().add (Seasons);
       getContentPane().add (time);

   }
    public void paint (Graphics g)
    { super.paintComponent(g);
       painter.paint(g);
    //Precipitation Bar
    g.setColor (Color.black);
        g.drawRect (40, 170, 100, 20);//outline of bar
        g.setColor (Color.blue);
        g.fillRect (40+1, 170+4, P, 14 ); //indicator bar (+4 puts space beetween outline bar)

        //Temparature Bar
    g.setColor (Color.red);
        g.fillRect (170+4 ,50,14,100);//Covers in
        g.setColor (Color.black);
        g.drawRect (170, 50, 20, 100);//outline of bar
        g.setColor (Color.white);
        g.fillRect (170 +4, 50+1, 16, 100 - H); //indicator bar (+4 puts space beetween outline bar)


        }


    public void actionPerformed (ActionEvent ae)
    {

        }
}

0 个答案:

没有答案