超级油漆组件没有出现(我已经尝试了一切)

时间:2014-11-28 22:10:42

标签: java swing jframe jpanel preferredsize

大家好,我目前在使用我的油漆组件制作任何东西时遇到问题。我尝试了很多我在网上看过的东西,并尝试了很多不同的东西,但我只是无法展示任何东西。我试图让我的绘画组件显示一系列条形图,这些条形图使用swing进行排序,但我甚至无法显示任何内容。任何帮助将不胜感激。

package proj2;
import java.awt.*;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;


public class project2 extends JFrame
{
    private JButton button1 = new JButton("Insertion");         
    private UpdateTextFieldThread currentThread;   
    private int[] array = new int[15];  
    private Display display; 

    private class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            Object src = e.getSource();
            if (src == button1){
                insertionSort(array);
                (new UpdateTextFieldThread()).execute();
            }
        }
    }

    public project2()
    {
        setTitle("Sorting Charts");
        setSize(400,250);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        for(int i=0; i<array.length;i++)    
            array[i]=(int) (Math.random()*100);
        display=new Display();

        ButtonHandler bh = new ButtonHandler();
        button1.addActionListener(bh);  

        JPanel container = new JPanel();
        container.setLayout(new GridLayout(2, 2));
        container.add(button1);

        JPanel masterPanel=new JPanel();
        masterPanel.setLayout(new BorderLayout());
        masterPanel.add(display, BorderLayout.SOUTH);
        masterPanel.add(container, BorderLayout.NORTH);

        setContentPane(container);
        setVisible(true);
        display.repaint();
    }

    private class Display extends JPanel    {
        private Color color = Color.RED;

        //@Override
        public void paintComponent(Graphics g) {
            super.paintComponents(g);
            g.setColor(Color.RED);
            Dimension d = getSize();
            int clientWidth = d.width;
            int clientHeight = d.height;
            int barWidth = clientWidth / array.length;
            for(int i=0;i<array.length;i++){
                int x=0;
                int y=15;
                int linethickness=5;
                int linelength=10;
                g.setColor(Color.red);
                g.fillRect(x, clientHeight, linelength*array[i] , linethickness);
                g.setColor(Color.black);
                g.drawRect(x,clientHeight, linelength*array[i], linethickness);
                x+=15;
            }
        }
    }

    private class UpdateTextFieldThread extends SwingWorker<Void, Integer>
    {
        protected Void doInBackground()
        {
            display.repaint();
            return null;
            //should have repaint method in here somewhere

            /*for (int i = 0; i < 50; i++) {
                publish(i);
                try {
                    Thread.sleep(THREAD_DELAY);
                } catch (InterruptedException e) { }
            }
            return null;*/
        }

        // The parameter here is a list of all published data
        //  since the last call to process.  We are interested
        //  in displaying only the latest one on the GUI.
        protected void process(java.util.List<Integer> list)
        {
        //    textfield.setText("" + list.get(list.size() - 1));
        }
    }

    public static <T extends Comparable<T>> void insertionSort(int[] hue2)
    {
        for (int i = 1; i < hue2.length; i++) {
            int thingToInsert = hue2[i];

            int j = i - 1;
            while (j >= 0 && thingToInsert<hue2[j]) {
                hue2[j+1] = hue2[j];
                j--;
            }

            hue2[j+1] = thingToInsert;
         }
     }

     public static void main(String[]args)  {
         new project2();
     }
}

1 个答案:

答案 0 :(得分:3)

masterPanel.add(display, BorderLayout.SOUTH);

您正在将“显示”面板添加到BorderLayout的SOUTH。 SOUTH约束将重新生成组件的首选高度。您的组件的高度为0,无法显示任何内容。

每当您进行自定义绘制时,您还需要覆盖组件的getPreferredSize()方法以提供适当的组件大小,以便布局管理器可以完成这项工作。