Trying to add multiple panel objects to a different class that creates a basic JFrame
I am creating a class that acts as a container which holds a label and buttons. I want the buttons to only appear when I mouseover the label on a new layer, and be cl开发者_StackOverflow中文版ickable, but this is not my question (I have discovered OyverLay Layout and JLayeredPane for this purpose, and I will figure that out later). Like I said I plan to use JLayeredPane vs regular JPanel (because of it's multi layer abilities) most likely, but my first question is this the correct type of container to use if this class will instantiate objects onto another classes JFrame container? I don't think it is because as a panel it creates a window with a title bar and buttons, and I don't want that. I just want this class to instantiate a box with my label and buttons and add it to a JFrame to be in a grid with other buttons???
My main issue is, I want this container that holds the label and buttons to be added to a JFrame that is apart of a class who's main job is to hold the containers from my first class and have the user be able to add more and remove via a UI.
Essentially I want a form with text boxes and a button, and the button adds a new ListItem object to the JFrame of another class and the new objects label text is built form the text boxes, instantiating a new object on that JFrame. So, if I hit the button 5 times I'll see 5 boxes with 5 labels in them with unique words one right on top of the other.
I have looked into using the root container, using JInternalFrames, and tried every other magic tricks but can't seem to get it to work right.
Here is the code for the class I have built.
*above this in my class is the importing of the objects I need, and the creation of various strings, buttons and labels used here. what I have pasted here is the meat of my code, the object which is created and displayed when instanciated***
public ListItem(String nameC, String timeC, String recordNum)
{
JLayeredPane panel1 = new JLayeredPane();
name = nameC; time = timeC; recordN = recordNum;
//fullItemString is a String object
fullItemString = "Help " + name + " at " + time + " regarding " + "Record # " + recordNum;
//item is a label
item.setText(fullItemString);
item.setFont(verdana);
minusButton.putClientProperty("JButton.buttonType", "square");
exportButton.putClientProperty("JButton.buttonType", "square");
editButton.putClientProperty("JButton.buttonType", "square");
// setSize(425, 50);
//setDefaultCloseOperation(EXIT_ON_CLOSE);
minusButton.setBounds(175, -1, 35, 30);
editButton.setBounds(210, -1, 35, 30);
exportButton.setBounds(130, -1, 35, 30);
panel1.add(minusButton);//, new Integer(2));
panel1.add(top);//, new Integer(1));
panel1.add(editButton);//, new Integer(3));
panel1.add(exportButton, new Integer(4));
minusButton.addActionListener(this);
item.addMouseListener(this);
exportButton.setVisible(false);
minusButton.setVisible(false);
editButton.setVisible(false);
lp.setVisible(true);
setVisible(true);
}
Multiple panel adding in JFrame using CardLayout. try it...
精彩评论