JPanel在特定情况下不会绘制组件

时间:2017-09-22 05:13:08

标签: java swing user-interface

我正在创建一个程序,它将创建一个带有相当简单但多组件GUI的窗口。我通过在一个框架中创建一个仅包含面板的样本来测试JPanel逻辑,但它工作正常,但是在我的真实GUI中创建JPanel时,只显示背景。

BoardPanel代码:

package groupchatclient;
import java.awt.*;
import javax.swing.*;

public class BoardPanel extends JPanel
{
    int xOff = 20,yOff = 20,height = 20,width = 50,size = 30;
    char orientation = 'V';
    public BoardPanel()
    {
        setBorder(BorderFactory.createLineBorder(Color.black));
    }
    public BoardPanel(int height, char orient, int size, int width)
    {
        this.height = height;
        orientation = orient;
        this.size = size;
        this.width = width;
        setBorder(BorderFactory.createLineBorder(Color.black));
    }
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        g.setColor(Color.BLACK);

        int xOffset,yOffset;
        if(Character.toUpperCase(orientation) == 'V')
        {   

            for(int h = 0; h<height; h++)
            {

                if(h%2 == 0)
                {


                    for(int w = 0; w<width; w++)
                    {

                        //find the top point
                        xOffset = xOff + w*(int)(Math.sqrt(3)*size);
                        yOffset = yOff + (h*3*size/2);
                        Point hexPoint = new Point(xOffset,yOffset);
                        //calculate the other points
                        VertHex hex = new VertHex(hexPoint,size);
                        //organize the points into a usable format
                        int[] xPoints = new int[6];
                        int[] yPoints = new int[6];
                        for(int i = 0; i<6; i++)
                        {
                            xPoints[i] = hex.getPoint(i).getX();
                            yPoints[i] = hex.getPoint(i).getY();
                        }
                        //print the hexagon
                        g.drawPolygon(xPoints,yPoints,6);
                    }
                }
                else
                {
                    for(int w = 0; w<width-1; w++)
                    {
                     //find the top point
                     xOffset = xOff + w*(int)(Math.sqrt(3)*size) + (int)(size*Math.sqrt(3)/2);
                     yOffset = yOff + (h*3*size/2);
                     Point hexPoint = new Point(xOffset,yOffset);
                     //calculate the other points
                     VertHex hex = new VertHex(hexPoint,size); 
                    //organize the points into a usable format
                     int[] xPoints = new int[6];
                     int[] yPoints = new int[6];
                     for(int i = 0; i<6; i++)
                     {
                         xPoints[i] = hex.getPoint(i).getX();
                         yPoints[i] = hex.getPoint(i).getY();
                     }
                     //print the hexagon
                     g.drawPolygon(xPoints,yPoints,6);
                    }
                } 

            }
        }
        else //FLAT ORIENTATION
        {
            for(int w = 0; w<width; w++)
            {

                if(w%2 == 0)
                {


                    for(int h = 0; h<height; h++)
                    {

                        //find the top left point
                       xOffset = xOff + (int)(w*3*size/2);
                       yOffset = yOff + (int)(h*Math.sqrt(3)*size);
                        Point hexPoint = new Point(xOffset,yOffset);
                        //calculate the other points
                        FlatHex hex = new FlatHex(hexPoint,size);
                        //organize the points into a usable format
                        int[] xPoints = new int[6];
                        int[] yPoints = new int[6];
                        for(int i = 0; i<6; i++)
                        {
                            xPoints[i] = hex.getPoint(i).getX();
                            yPoints[i] = hex.getPoint(i).getY();
                        }
                        //print the hexagon
                        g.drawPolygon(xPoints,yPoints,6);
                    }
                }
                else
                {
                    for(int h = 0; h<height-1; h++)
                    {
                     //find the top left point
                     xOffset = xOff + (int)(w*3*size/2);
                     yOffset = yOff + (int)(h*Math.sqrt(3)*size + Math.sqrt(3)*size/2);
                     Point hexPoint = new Point(xOffset,yOffset);
                     //calculate the other points
                     FlatHex hex = new FlatHex(hexPoint,size); 
                    //organize the points into a usable format
                     int[] xPoints = new int[6];
                     int[] yPoints = new int[6];
                     for(int i = 0; i<6; i++)
                     {
                         xPoints[i] = hex.getPoint(i).getX();
                         yPoints[i] = hex.getPoint(i).getY();
                     }
                     //print the hexagon
                     g.drawPolygon(xPoints,yPoints,6);
                    }
                } 

            }
        }
    }
    public void setOrientation(char c)
    {
        if(c == 'v' || c == 'V' || c == 'h' || c == 'H')
        {
            orientation = c;
        }
    }
}

BoardFrame代码:

package groupchatclient;
import java.awt.*;
import java.io.BufferedReader;
import java.io.PrintWriter;
import javax.swing.*;

public class BoardFrame extends JFrame
{

    public BoardFrame(BufferedReader in, PrintWriter out, int height, char orient, int size, int width)
    {
        super("Pentarius");
        initComponents();
        this.out = out;
        this.in = in;
        this.height = height;
        this.orient = orient;
        this.size = size;
        this.width = width;
    }

     private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        mainScrollPane = new javax.swing.JScrollPane();
        mainDisplayPane = new BoardPanel(height, orient, size, width);
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 10), new java.awt.Dimension(0, 10), new java.awt.Dimension(32767, 10));
        searchScrollPane = new javax.swing.JScrollPane();
        searchTextArea = new javax.swing.JTextArea();
        chatTextField = new javax.swing.JTextField();
        chatScrollPane = new javax.swing.JScrollPane();
        chatTextArea = new javax.swing.JTextArea();
        searchTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(mainDisplayPane);
        mainDisplayPane.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 817, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 622, Short.MAX_VALUE)
        );

        mainScrollPane.setViewportView(mainDisplayPane);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 809, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(filler1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 613, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        searchTextArea.setColumns(20);
        searchTextArea.setRows(5);
        searchScrollPane.setViewportView(searchTextArea);

        chatTextField.setText("");
        chatTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                out.println(chatTextField.getText());
                chatTextField.setText("");
            }
        });

        chatTextArea.setColumns(20);
        chatTextArea.setRows(5);
        chatScrollPane.setViewportView(chatTextArea);

        searchTextField.setText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(searchScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(chatScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(searchTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(chatTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 5, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chatScrollPane)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chatTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PentariusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PentariusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PentariusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PentariusFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PentariusFrame().setVisible(true);
            }
        });
    }
    public void setFiller(javax.swing.Box.Filler fill) {
        filler1 = fill;
    }
    public void setPanel1(javax.swing.JPanel panel) {
        jPanel1 = panel;
    }
    public void setMainDisplayPane(javax.swing.JPanel panel) {
        mainDisplayPane = panel;
    }
    public void setSearchScrollPane(javax.swing.JScrollPane scroll) {
        searchScrollPane = scroll;
    }
    public void setChatScrollPane(javax.swing.JScrollPane scroll) {
        chatScrollPane = scroll;
    }
    public void setMainScrollPane(javax.swing.JScrollPane scroll) {
        mainScrollPane = scroll;
    }
    public void setSearchTextArea(javax.swing.JTextArea area) {
        searchTextArea = area;
    }
    public void setChatTextArea(javax.swing.JTextArea area) {
        chatTextArea = area;
    }
    public void setChatTextField(javax.swing.JTextField field) {
        chatTextField = field;
    }
    public void setSearchTextField(javax.swing.JTextField field) {
        searchTextField = field;
    }

    public javax.swing.Box.Filler getFiller() {
        return filler1;
    }
    public javax.swing.JPanel getMainDisplayPane() {
        return mainDisplayPane;
    }
    public javax.swing.JPanel getJPanel1() {
        return jPanel1;
    }
    public javax.swing.JScrollPane getSearchScrollPane() {
        return searchScrollPane;
    }
    public javax.swing.JScrollPane getChatScrollPane() {
        return chatScrollPane;
    }
    public javax.swing.JScrollPane getMainScrollPane() {
        return mainScrollPane;
    }
    public javax.swing.JTextArea getSearchTextArea() {
        return searchTextArea;
    }
    public javax.swing.JTextArea getChatTextArea() {
        return chatTextArea;
    }
    public javax.swing.JTextField getSearchTextField() {
        return searchTextField;
    }
    public javax.swing.JTextField getChatTextField() {
        return chatTextField;
    }

    // Variables declaration - do not modify                     
    private javax.swing.Box.Filler filler1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel mainDisplayPane;
    private javax.swing.JScrollPane searchScrollPane;
    private javax.swing.JScrollPane chatScrollPane;
    private javax.swing.JScrollPane mainScrollPane;
    private javax.swing.JTextArea searchTextArea;
    private javax.swing.JTextArea chatTextArea;
    private javax.swing.JTextField chatTextField;
    private javax.swing.JTextField searchTextField;

    private final PrintWriter out;
    private final BufferedReader in;
    private final int height;
    private final int size;
    private final char orient;
    private final int width;
    // End of variables declaration                   
}

创建GUI的绘制方法代码:

public void draw(GroupChatClient client) throws Exception
    {
                frame.setVisible(false);
        BoardFrame bf = new BoardFrame(in,out,bp.height,bp.orientation,bp.size,bp.width);
        bf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        if(bp.orientation == 'V')
        {
            panelSizeX=2*(int)(bp.width*Math.sqrt(3)*bp.size/2);
            panelSizeY= 2*bp.yOff + 2*bp.size*bp.height;
        }
        else //orientation = FLAT
        {
            panelSizeX= 2*(bp.yOff)+ (bp.size*bp.width);
            panelSizeY= 2*(int)(bp.height*Math.sqrt(3)*bp.size/2);
        }
        System.out.println(panelSizeX + " " + panelSizeY);
        bf.getChatTextArea().setEditable(false);
        bf.getSearchTextArea().setEditable(false);

        DefaultCaret newCaret = (DefaultCaret)bf.getChatTextArea().getCaret();
        newCaret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        bf.getChatTextArea().setLineWrap(true);
        bf.getSearchTextArea().setLineWrap(true);

        //bf.getChatTextField().addActionListener(new ActionListener() {
        //    public void actionPerformed(ActionEvent e) {
        //        out.println(bf.getChatTextField().getText());
        //        bf.getChatTextField().setText("");
        //    }
        //});

        bf.pack();
        bf.setVisible(true);
        while(true)
        {
            String line = in.readLine();
            if (line.startsWith("MESSAGE")) 
            {
                bf.getChatTextArea().append(line.substring(8) + "\n");
            }
        }
            }

我一直在测试这个而不改变bp的任何特性,它被初始化为一个新的默认的boardpanel

0 个答案:

没有答案