relive(六道轮回,无想转生)
再给指导指导。

解决方案 »

  1.   

    构造方法中第一个下面的语句改为:
    constraints.fill = GridBagConstraints.BOTH;
    即可。
      

  2.   

    /* 
     *@(#)Traffic5.java  1.5  2003/03/28
     *
     * Copyright (c) Since 2003 WisdomLeague  
     * JRT 1.4.1
     */
    /**
     *
     *@version      1.5   28 March 2003
     *@author   Fan
     */ import javax.swing.*;
     import java.awt.*;
     import java.awt.event.*; public class Traffic5 extends JFrame {
         public int num = 0;      
         Traffic5() {
             super("Traffic");
             setSize(100,410);
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     GridBagLayout gridbag = new GridBagLayout();
     GridBagConstraints constraints = new GridBagConstraints();
     JPanel pane = new JPanel();
     pane.setLayout(gridbag);
             setContentPane(pane);         buildConstraints(constraints, 0, 1, 1, 1, 100,75);
     constraints.fill = GridBagConstraints.NONE;
     constraints.anchor = GridBagConstraints.CENTER;
     TrafficLights tl = new TrafficLights();
             tl.setPreferredSize(new Dimension(100,300));
             gridbag.setConstraints(tl, constraints);
     pane.add(tl);
             
             buildConstraints(constraints, 0, 2, 1, 1, 100, 0);
     constraints.fill = GridBagConstraints.NONE;
     constraints.anchor = GridBagConstraints.CENTER;
     TrafficButtons tb = new TrafficButtons();
     gridbag.setConstraints(tb, constraints);         
             pane.add(tb);             
        }    void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
    gbc.gridx = gx;
    gbc.gridy = gy;
    gbc.gridwidth = gw;
    gbc.gridheight = gh;
    gbc.weightx = wx;
    gbc.weighty = wy;
        }
         public static void main (String[] arg) {
             Traffic5 tr = new Traffic5();
             tr.show();
         }
     //}     class TrafficLights extends JPanel implements Runnable {
             //public int num = 0;
             Thread runner;         public TrafficLights () {
                 
                 if (runner == null) {
                    runner = new Thread(this);
                    runner.start();
                 }
             }
     
             public void run() {
                 while (true) {
                     repaint();
                     try {
                         Thread.sleep(100);
                     } catch (InterruptedException e) { }
                 }
             }         public void paintComponent(Graphics comp) {
                 Graphics2D comp2D = (Graphics2D)comp;
                 comp2D.setColor(Color.black);
                 comp2D.fillRect(0, 0, 300, 300);
                 if (num == 1)
                     comp2D.setColor(Color.green);
                 else
                     comp2D.setColor(Color.black);
                     comp2D.fillOval(0, 0, 100, 100);
                 if (num == 2)
                     comp2D.setColor(Color.yellow);
                 else
                     comp2D.setColor(Color.black);
                     comp2D.fillOval(0, 100, 100, 100);
                 if (num == 3)
                     comp2D.setColor(Color.red);
                 else
                     comp2D.setColor(Color.black);
                     comp2D.fillOval(0, 200, 100, 100);
             }
         }
          class TrafficButtons extends JPanel implements ActionListener {
             //public int num = 0;         JButton green = new JButton("Green");
             JButton yellow = new JButton("Yellow");
             JButton red = new JButton("Red");         TrafficButtons () {
                 green.addActionListener(this);
                 yellow.addActionListener(this);
                 red.addActionListener(this);             GridLayout family = new GridLayout(3,1,1,1);
                 setLayout(family);             add(green);
                 add(yellow);
                 add(red);
             }         public void actionPerformed(ActionEvent evt) {
                 Object src = evt.getSource();             if ( src == green )
                     num = 1;
                 else if ( src == yellow )
                     num = 2;
                 else if ( src == red )
                     num = 3;
             }
         }
     }
     /*好了
     */