java - 是否可以不从JPanel继承到内部类?

标签 java swing events awt java-7

早上好。我想编写一个程序,在 JSlider 的帮助下放大填充图形。当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();
             }
         });
    }
}

最佳答案

您永远不会将任何组件添加到 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); 
}

关于java - 是否可以不从JPanel继承到内部类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19888668/

相关文章:

java - UnsuppotedOperationException.getCause 返回自身

java - 验证 SSL 认证 Java/Android

Java线程更新jProgressBar在再次调用线程时不会更新

c# - 计时器 Tick 事件突然不起作用

c# - 如何捕捉键盘输入的 "@"字符

java - 在 LibGDX 中在 Sprite 像素级别而不是屏幕像素级别旋转 Sprite

java - 与其他应用程序共享图像的通用方法

java - JFrame - 当 JFrame 最小化时,系统托盘上不显示图标

java - 无布局的 TransferHandler

c# - 事件、委托(delegate)与 Action<T>