添加面板

时间:2017-07-07 11:42:43

标签: java swing netbeans layout-manager null-layout-manager

我创建了一个具有“自由设计”布局的JFrame(带有自动生成的代码)。在JFrame中,我有一个带有'null'布局的背景面板。后台面板包含另一个名为cardLayoutPanel的面板,该面板具有“cardlayout”设计。在卡片布局面板中,我有一个jScrollPane,它有一个名为firstNoteView的面板,它具有“自由设计”布局。在firstNoteView中,另一个名为NewNotePanel的面板具有“null”布局。

当我运行文件来运行程序时,jScrollPane(在卡布局面板中)工作,但是当我将自己的面板(使用我自己的代码)添加到firstNoteView时 - 在jScrollPane中,jScrollPane不起作用

下面是代码:

package interfaces;

import java.awt.Dimension;
import javax.swing.JPanel;

public class ScrollablePanel extends javax.swing.JFrame
{

 Notes notes = new Notes ();

int numberOfNotes = 9;
int currentNoteNumber = 0;


//co-ordinates for first text area + used to calculate the rest of the      text area locations 
int x = 40;
int y = 100;

// panel sizes
int length = 250;
int breadth = 220;

int xgap = 310;
int ygap = 310;


// method to declare text areas for notes
public void createPanels () {

Dimension preferredSize = jScrollPane1.getPreferredSize();
JPanel[] panel = new JPanel [numberOfNotes];

System.out.println(numberOfNotes);

for (int construct = 0; construct < numberOfNotes; construct++)
    panel [construct] = new JPanel ();


for (int setLocation = 1, xCordinate = 1, pHeight = 1; setLocation < numberOfNotes; setLocation++, xCordinate++) {

            if ((setLocation < 4) && (setLocation != 0))
                panel[setLocation].setBounds(x + (xgap * (setLocation)), y, length, breadth);

            if ((setLocation % 4 == 0) && (setLocation != 0)) {
                y = y + ygap;

                x = 40;
                xCordinate = 0;

                firstNoteView.setBounds(200, 50, 1250, 850+(ygap*pHeight));

                pHeight++;

            }


            if (setLocation >= 4)

                panel[setLocation].setBounds(x + (xgap * (xCordinate)), y, length, breadth);

        }


        for (int add = 0; add < numberOfNotes; add++) 
                firstNoteView.add(panel[add]);

        firstNoteView.repaint();


    }

// method to hide neccessary components when for when the program initialises
public void setScreen ()
{
// setting components to be hidden, alphabetical order

firstNoteView.setVisible(true);

// remove all panels in the cardlayout
cardLayoutPanel.removeAll();

// add the firstNoteView Panel to set the default home screen
cardLayoutPanel.add(firstNoteView);
cardLayoutPanel.repaint();

}


public ScrollablePanel()
{
initComponents();

setScreen ();
setNumberOfNotes () ;
createPanels ();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents()
{

BackgroundPanel = new javax.swing.JPanel();
cardLayoutPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
firstNoteView = new javax.swing.JPanel();
NewNotePanel = new javax.swing.JPanel();
lblNewNote = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBounds(new java.awt.Rectangle(0, 23, 1440, 900));
setMinimumSize(new java.awt.Dimension(1440, 900));
setPreferredSize(new java.awt.Dimension(1440, 900));

BackgroundPanel.setBackground(new java.awt.Color(255, 255, 255));
BackgroundPanel.setLayout(null);

cardLayoutPanel.setLayout(new java.awt.CardLayout());

jScrollPane1.setBounds(new java.awt.Rectangle(0, 0, 1090, 0));

firstNoteView.setBackground(new java.awt.Color(0, 153, 204));

NewNotePanel.setLayout(null);

lblNewNote.setText("<html><center>+<br>New Note...</center></html>");
NewNotePanel.add(lblNewNote);
lblNewNote.setBounds(90, 90, 73, 32);

javax.swing.GroupLayout firstNoteViewLayout = new javax.swing.GroupLayout(firstNoteView);
firstNoteView.setLayout(firstNoteViewLayout);
firstNoteViewLayout.setHorizontalGroup(
    firstNoteViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(firstNoteViewLayout.createSequentialGroup()
        .addGap(40, 40, 40)
        .addComponent(NewNotePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(951, Short.MAX_VALUE))
);
firstNoteViewLayout.setVerticalGroup(
    firstNoteViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(firstNoteViewLayout.createSequentialGroup()
        .addGap(100, 100, 100)
        .addComponent(NewNotePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(792, Short.MAX_VALUE))
);

jScrollPane1.setViewportView(firstNoteView);

cardLayoutPanel.add(jScrollPane1, "card2");

BackgroundPanel.add(cardLayoutPanel);
cardLayoutPanel.setBounds(200, 60, 1240, 840);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(BackgroundPanel, 

javax.swing.GroupLayout.DEFAULT_SIZE, 1440, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(BackgroundPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        


    public static void main(String args[])
    {

        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(ScrollablePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex)
        {


    java.util.logging.Logger.getLogger(ScrollablePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(ScrollablePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(ScrollablePanel.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 ScrollablePanel().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JPanel BackgroundPanel;
    private javax.swing.JPanel NewNotePanel;
    private javax.swing.JPanel cardLayoutPanel;
    private javax.swing.JPanel firstNoteView;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNewNote;
    // End of variables declaration                   
}

0 个答案:

没有答案