有可能不从JPanel继承到内部类吗?

时间:2013-11-10 10:31:42

标签: java swing events awt java-7

早上好。我想写一个程序,在JSlider的帮助下扩展一个fillgraphic。当ActPanelContourLab4的内部类时,他们至少会在JFrame上展示自己。但是传递Radius并重新粉刷存在一个巨大的问题。我没有使用内部类,但它在屏幕上不再显示ActPanelContour

这是代码最重要的部分

class ActPanel extends JPanel implements ChangeListener {

    public Contour cont;

    public ActPanel(Contour contour) {
        super(new FlowLayout());
        cont = contour;

        JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 250, 50);
        slider.addChangeListener(this);

        JButton button = new JButton("display mark");
    }

    public void stateChanged(ChangeEvent e) {

        if (e.getSource() instanceof JSlider) {
            JSlider source = (JSlider)e.getSource();
            if (!source.getValueIsAdjusting()) {
                int R = (int)source.getValue();
                cont.setR(R);
                cont.repaint();
            }
        } 
    } 
}

class Contour extends JPanel {

    private static final int pointCount = 9;

    public void paintComponent(Graphics gfx) {
        super.paintComponent(gfx);
        Graphics2D g = (Graphics2D) gfx;

        GeneralPath fpolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, pointCount);
        fpolygon.moveTo(Float.valueOf((float)R+250), Float.valueOf(250.0f));
        fpolygon.lineTo(Float.valueOf(250.0f), Float.valueOf(250.0f));

        //some drawing
    }

    private int R;

    public void setR(int R) {
        this.R = R;
    }

    public int getR() {
        return R;
    }

}

public class Lab4 {

    private static void addComponentsToPane(Container pane) {

        if (!(pane.getLayout() instanceof BorderLayout)) {
             pane.add(new JLabel("Container doesn't use BorderLayout!"));
             return;
        }

        Contour cont = new Contour();

        ActPanel bottom = new ActPanel(cont);
        pane.add(bottom, BorderLayout.PAGE_END);

        cont.setPreferredSize(new Dimension(500,500));
        pane.add(cont, BorderLayout.LINE_END);

    }

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("Lab4");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        addComponentsToPane(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
   }

   public static void main(String [] arg) {

        SwingUtilities.invokeLater(
             new Runnable() {
                  public void run() {
                       createAndShowGUI();
                  }
             });
        }
   }

这是完整版

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;

class ActPanel extends JPanel implements ChangeListener {

    public Contour cont;

    public ActPanel(Contour contour) {
         super(new FlowLayout());

         cont = contour;

         JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 250, 50);
         slider.addChangeListener(this);

         JButton button = new JButton("display mark");
    }

    public void stateChanged(ChangeEvent e) {

         if (e.getSource() instanceof JSlider) {
              JSlider source = (JSlider)e.getSource();
              if (!source.getValueIsAdjusting()) {
                   int R = (int)source.getValue();
                   cont.setR(R);
                   cont.repaint();
              }
         } 
    } 
}

class Contour extends JPanel {

    private static final int pointCount = 9;

    public void paintComponent(Graphics gfx) {
         super.paintComponent(gfx);
         double kappa = 0.5522847498;
         Graphics2D g = (Graphics2D) gfx;
         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,    RenderingHints.VALUE_ANTIALIAS_ON);

         GeneralPath fpolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, pointCount);
         fpolygon.moveTo(Float.valueOf((float)R+250), Float.valueOf(250.0f));
         fpolygon.lineTo(Float.valueOf(250.0f), Float.valueOf(250.0f));
         fpolygon.lineTo(Float.valueOf(250.0f), Float.valueOf((float)250-R/2));
         fpolygon.curveTo(
             Float.valueOf((float)((-R/2)*kappa+250)),Float.valueOf((float)(-R/2)+250),
             Float.valueOf((float)(-R/2)+250),Float.valueOf((float)((-R/2)*kappa)+250),
             Float.valueOf((float)-R/2+250), Float.valueOf(250.0f));
             fpolygon.lineTo(Float.valueOf((float)-R+250), Float.valueOf(250.0f));
             fpolygon.lineTo(Float.valueOf((float)-R+250), Float.valueOf((float)R+250));
         fpolygon.lineTo(Float.valueOf(250.0f), Float.valueOf((float)R+250));
         fpolygon.lineTo(Float.valueOf(250.0f), Float.valueOf((float)R/2+250));
         fpolygon.lineTo(Float.valueOf((float)R+250), Float.valueOf(250.0f));
         fpolygon.closePath();

         g.setPaint(Color.red);
         g.fill(fpolygon);
         g.setPaint(Color.black);
         g.draw(fpolygon);
     }

     private int R;

     public void setR(int R) {
         this.R = R;
     }

     public int getR() {
         return R;
     }

 }

 public class Lab4 {

     private static void addComponentsToPane(Container pane) {

     if (!(pane.getLayout() instanceof BorderLayout)) {
          pane.add(new JLabel("Container doesn't use BorderLayout!"));
          return;
     }

     Contour cont = new Contour();

     ActPanel bottom = new ActPanel(cont);
     pane.add(bottom, BorderLayout.PAGE_END);

     JList<Integer> xList = new JList<Integer>(new Integer[] {4,2,5,1,-5,-1,-4});
     pane.add(xList, BorderLayout.LINE_START);

     JPanel yPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
     yPanel.add(new Label("x\\y"));
     yPanel.add(new JCheckBox("5"));
     yPanel.add(new JCheckBox("1"));
     yPanel.add(new JCheckBox("-5"));
     yPanel.add(new JCheckBox("2"));
     yPanel.add(new JCheckBox("-5"));
     yPanel.add(new JCheckBox("-1"));
     yPanel.add(new JCheckBox("4"));
     pane.add(yPanel, BorderLayout.PAGE_START);

     cont.setPreferredSize(new Dimension(500,500));
     pane.add(cont, BorderLayout.LINE_END);

 }

 private static void createAndShowGUI() {

    JFrame frame = new JFrame("Lab4");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addComponentsToPane(frame.getContentPane());

    frame.pack();
    frame.setVisible(true);
}

public static void main(String [] arg) {

    SwingUtilities.invokeLater(
         new Runnable() {
             public void run() {
                 createAndShowGUI();
             }
         });
    }
}

1 个答案:

答案 0 :(得分:1)

您永远不会将任何组件添加到ActPanel:

public ActPanel(Contour contour) {
   super(new FlowLayout());

   ...
   // you're missing the following lines:
   this.add(slider);
   this.add(button);

所以它在框架中,但是空的。

“轮廓”面板也在框架中。将这些行添加到它,你会看到它:

public Contour() { 
    setBackground(Color.BLUE); 
}