1

このアプレットのレイアウトに問題があります。ボーダー レイアウトの Canvas の横に List があります。実行すると、リストが非常に狭く、幅が 51 ピクセルになることがあります。それ以外の場合は、コードを少し変更するだけで問題なく動作し、リストはエントリを表示するのに十分な幅があります。リストは配列から取り込まれ、配列から 1 つの項目をコメントアウトするだけで動作を変更できます。正確には、この行にコメントすると問題が解決します

{"---- Lissajous curves ","t","t","1","1"}, // commenting this line fixes problem

これはバグだと思いますが、再現可能かどうか、または私が何かばかげたことをしているのかどうかを知っていただければ幸いです。また、バグを送信する適切な場所がよくわかりません。Mac OS X 10.6.8 で標準の Java を使用しています。

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.List;

import javax.swing.SwingUtilities;

public class Zap extends Applet {

    private static final long serialVersionUID = 330L;

    String[][] equations = new String[][]{
//                  {"Line ----------------------","3 t-1","t+1"},
                    {"Line","3 t-1","t+1"},
                    {"Circle","cos(t)","sin(t)","-pi","pi"},
                    {"Sq Circle","cos(t)","2 sin(t)","-pi","pi"},
                    {"Ellipse","2 cos(t)","sin(t)","-pi","pi"},
                    {"Cycloid","t-sin(t)","1-cos(t)","-20","20"},
                    {"---- Hypocycloids ----"},
                    {"Deltoid","2 cos(t)+cos(2t)","2 sin(t)-sin(2t)","-pi","pi"},
                    {"Astroid","cos(t)^3","sin(t)^3","-pi","pi"},
                    {"Hypocycloids 5","cos(t) + cos(4 t)/4","sin(t) - sin(4 t)/4","-pi","pi" },
                    {"---- Epicycloids ----"},
                    {"Cardioid",
                            "cos(t) - cos(2 t)/2",
                            "sin(t) - sin(2 t)/2","-pi","pi" },
                    {"Nephroid",
                                            "cos(t) - cos(3 t)/3",
                                            "sin(t) - sin(3 t)/3","-pi","pi" },
                    {"Epicycloid 3",
                                                    "cos(t) - cos(4 t)/4",
                                                    "sin(t) - sin(4 t)/4","-pi","pi" },
                    {"Epicycloid 4",
                                                            "cos(t) - cos(5 t)/5",
                                                            "sin(t) - sin(5 t)/5","-pi","pi" },
                    {"Epicycloid 4",
                                                            "cos(t) - cos(5 t)/5",
                                                            "sin(t) - sin(5 t)/5","-pi","pi" },
                    {"Epicycloid 5",
                                                                    "cos(t) - cos(6 t)/6",
                                                                    "sin(t) - sin(6 t)/6","-pi","pi" },

                    {"---- Lissajous curves ","t","t","1","1"}, // commenting this line
                    {"Lissajous 1,2","sin(t)","sin(2 t)","-pi","pi"},
                    {"Lissajous 1,3","sin(t-pi/2)","sin(3 t)","-pi","pi"},
                    {"Lissajous 1,4","sin(t)","sin(4 t)","-pi","pi"},//             {"a Lissajousxxxxxxxxxxb",""},
                    {"Lissajous 2,1","sin(2t)","sin(t)","-pi","pi"},
                    {"Lissajous 2,2","sin(2t-pi/4)","sin(2t)","-pi","pi"},
                    {"Lissajous 2,3","sin(2t-pi/3)","sin(3t)","-pi","pi"},
                    {"Lissajous 3,4","sin(3t)","sin(4t)","-pi","pi"},
    };



    /** The canvas for plotting the graph */
    private Canvas graphCanvas;

    /** List of equations */
    List list = new List();

    /**
     * Initializes the applet FunctionPlotter
     */
    @Override
    public void init ()  {
        try {
            initComponents();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * Sets the layout of the applet window to BorderLayout, creates all
     * the components and associates them with event listeners if necessary.
     * @param j
     * @throws JepException
     */
    private void initComponents ()  {
        setLayout(new BorderLayout());

//        GridBagConstraints gbc1 = new GridBagConstraints(0, 0, 1, 1,0.9, 1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0 , 0) ;
//        GridBagConstraints gbc2 = new GridBagConstraints(1, 0, 1, 1, 0.1, 1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
//        setBackground (java.awt.Color.white);

//        list.addItemListener(this);
        list.setMinimumSize(new Dimension(220, 200));
        list.setSize(new Dimension(220, 200));
        list.setPreferredSize(new Dimension(220, 200));
        System.out.println("list "+list.getBounds());
        System.out.println("list ps"+list.getPreferredSize());
        //add(list,gbc2);
        add("East",list);
        for(String[] eles:equations) {
            list.add(eles[0]);
    }
        // create the graph canvas and add it
        graphCanvas = createGraphCanvas();
        //add (graphCanvas,gbc1);
        add("Center",graphCanvas);
        System.out.println("list ps before validate"+list.getPreferredSize());
        list.validate();
        this.validate();
        System.out.println("list ps after validate"+list.getPreferredSize());
        System.out.println(((Object)list).toString());
        System.out.println("\n\n");
    }


    protected Canvas createGraphCanvas()  {
          Canvas gc = new Canvas();
        gc.setPreferredSize(new Dimension(300,300));
        gc.setSize(new Dimension(300,300));
        System.out.println("PC size "+gc.size());
        return gc;
    }

    public static void main(String argv[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    protected static void createAndShowGUI() {
            Frame f = new Frame();
            f.setSize(600, 400);
            Zap pp = new Zap();
            f.add(pp);
            pp.init();
            System.out.println("pack");
        //f.pack();
            System.out.println("setVis");
            f.setVisible(true);
            System.out.println("setVis done");
    }
}
4

1 に答える 1

3

リストのこの (Swing ベースの) バリアントを試してください。

ザップ

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;

public class Zap extends JPanel {

    private static final long serialVersionUID = 330L;

    String[][] equations = new String[][]{
            //                  {"Line ----------------------","3 t-1","t+1"},
            {"Line","3 t-1","t+1"},
            {"Circle","cos(t)","sin(t)","-pi","pi"},
            {"Sq Circle","cos(t)","2 sin(t)","-pi","pi"},
            {"Ellipse","2 cos(t)","sin(t)","-pi","pi"},
            {"Cycloid","t-sin(t)","1-cos(t)","-20","20"},
            {"---- Hypocycloids ----"},
            {"Deltoid","2 cos(t)+cos(2t)","2 sin(t)-sin(2t)","-pi","pi"},
            {"Astroid","cos(t)^3","sin(t)^3","-pi","pi"},
            {"Hypocycloids 5","cos(t) + cos(4 t)/4","sin(t) - sin(4 t)/4","-pi","pi" },
            {"---- Epicycloids ----"},
            {"Cardioid",
                "cos(t) - cos(2 t)/2",
                "sin(t) - sin(2 t)/2","-pi","pi" },
                {"Nephroid",
                    "cos(t) - cos(3 t)/3",
                    "sin(t) - sin(3 t)/3","-pi","pi" },
                    {"Epicycloid 3",
                        "cos(t) - cos(4 t)/4",
                        "sin(t) - sin(4 t)/4","-pi","pi" },
                        {"Epicycloid 4",
                            "cos(t) - cos(5 t)/5",
                            "sin(t) - sin(5 t)/5","-pi","pi" },
                            {"Epicycloid 4",
                                "cos(t) - cos(5 t)/5",
                                "sin(t) - sin(5 t)/5","-pi","pi" },
                                {"Epicycloid 5",
                                    "cos(t) - cos(6 t)/6",
                                    "sin(t) - sin(6 t)/6","-pi","pi" },

                                    {"---- Lissajous curves ----","t","t","1","1"}, // commenting this line
                                    {"Lissajous 1,2","sin(t)","sin(2 t)","-pi","pi"},
                                    {"Lissajous 1,3","sin(t-pi/2)","sin(3 t)","-pi","pi"},
                                    {"Lissajous 1,4","sin(t)","sin(4 t)","-pi","pi"},//             {"a Lissajousxxxxxxxxxxb",""},
                                    {"Lissajous 2,1","sin(2t)","sin(t)","-pi","pi"},
                                    {"Lissajous 2,2","sin(2t-pi/4)","sin(2t)","-pi","pi"},
                                    {"Lissajous 2,3","sin(2t-pi/3)","sin(3t)","-pi","pi"},
                                    {"Lissajous 3,4","sin(3t)","sin(4t)","-pi","pi"},
    };

    /** The canvas for plotting the graph */
    private JPanel graphCanvas;

    DefaultListModel model = new DefaultListModel(); 
    /** List of equations */
    JList list = new JList(model);

    /**
     * Initializes the FunctionPlotter
     */
    public void init ()  {
        try {
            initComponents();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Sets the layout of the applet window to BorderLayout, creates all
     * the components and associates them with event listeners if necessary.
     * @param j
     * @throws JepException
     */
    private void initComponents ()  {
        setLayout(new BorderLayout());
        System.out.println("list "+list.getBounds());
        System.out.println("list ps"+list.getPreferredSize());
        list.setVisibleRowCount(8);
        JScrollPane scroll = new JScrollPane(
                list, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
                );
        add(BorderLayout.LINE_END,scroll);

        for(String[] eles:equations) {
            model.addElement(eles[0]);
        }

        // create the graph canvas and add it
        graphCanvas = createGraphCanvas();
        //add (graphCanvas,gbc1);
        add("Center",graphCanvas);
    }

    protected JPanel createGraphCanvas()  {
        JPanel gc = new JPanel() {

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.GREEN.darker());
                g.fillRect(0, 0, getWidth(), getHeight());
            }
        };

        gc.setPreferredSize(new Dimension(300,10));
        return gc;
    }

    public static void main(String argv[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    protected static void createAndShowGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        Zap pp = new Zap();
        f.add(pp);
        pp.init();
        System.out.println("pack");
        f.pack();
        System.out.println("setVis");
        f.setVisible(true);
        System.out.println("setVis done");
    }
}
于 2012-11-19T10:56:37.863 に答える