Hello So i have created a JFrame with a JSpinner inside (as you can see in the picture). Right now, the BorderLabel is showing in the Jspinner (as it should) but the arrows on the JSpinner are there as a part of the entire thing instead of just the JSpinner field. I would like help to find out how to put the JSpinner arrows on the bar. Thank you.
For you who asked for code,
Also I miss stated JLabel Earlier. I meant TitledBorder
import java.awt.ComponentOrientation;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class ex extends JFrame{
public static void main(String[] args){
new ex();
}
ex(){
super("test");
setSize(200,100);
SpinnerModel sm = new SpinnerNumberModel(3,1,25,1);
JSpinner shiftIn = new JSpinner(sm);
JPanel p = new JPanel();
shiftIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
shiftIn.setBorder(new CompoundBorder(new TitledBorder(new EmptyBorder(0,
0, 0, 0), "Shift Key"), shiftIn
.getBorder()));
p.add(shiftIn);
add(p);
setVisible(true);
}
}
shiftIn.setBorder(new CompoundBorder(
new TitledBorder(new EmptyBorder(0, 0, 0, 0), "Shift Key"),
shiftIn.getBorder()));
This is not the sort of thing that a titled border was made for!
Use a JLabel and put it in the PAGE_START of a BorderLayout, put the JSpinner in the PAGE_END. Add that container (the panel) where the spinner is currently added. (Then add a mnemonic for the label and make it the 'label for' the spinner.)
This is how to use that idea inside another layout (GridLayout in this example)..
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class ex extends JFrame {
public static void main(String[] args) {
new ex();
}
ex() {
super("test");
// imagine this is actually using GridBagLayout
JPanel ui = new JPanel(new GridLayout(0, 3,4,4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
SpinnerModel sm = new SpinnerNumberModel(3, 1, 25, 1);
JSpinner shiftIn = new JSpinner(sm);
JLabel l = new JLabel("Shift Key");
JPanel p = new JPanel(new BorderLayout());
p.add(l, BorderLayout.PAGE_START);
p.add(shiftIn, BorderLayout.PAGE_END);
add(ui);
for (int ii=0; ii<9; ii++) {
if (ii==4) {
ui.add(p);
} else {
ui.add(new JButton("Button"));
}
}
pack();
setVisible(true);
}
}
You appear to be running into risk by messing with the JSpinner's border. Myself, I would wrap my JSpinner in a JPanel and then give that wrapper JPanel the desired border. For example:
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
#SuppressWarnings("serial")
public class Ex extends JFrame {
public static void main(String[] args) {
new Ex();
}
Ex() {
super("test");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
SpinnerModel sm = new SpinnerNumberModel(3, 1, 25, 1);
JSpinner shiftIn = new JSpinner(sm);
JPanel spinnerWrapper = new JPanel(new BorderLayout());
spinnerWrapper.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEmptyBorder(0, 0, 0, 0), "Shift Key"));
spinnerWrapper.add(shiftIn);
shiftIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
add(spinnerWrapper);
pack();
setVisible(true);
}
}
Related
Can anyone see the code ? I want to make a page that has a banner and a pannel in which cards will change on the requirement. I added the Banner in JFrame (That is working fine) but The problem is that " CardLayout Panel is not adding in the JFrame".
Actually, I need this.
When button is pressed only card1 change to card2 but banner will remain same.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class gui extends JFrame{
private static final long serialVersionUID = 1L;
JPanel
basic_panel,
card_Layout_panel,
banner_panel,
welcome_authenticaion_panel_card1;
CardLayout basic2;
JLabel
logo_label,
name_label;
public gui(){
server_login_gui();
add(basic_panel);
standard_gui();
}
public void server_login_gui(){
basic_panel = new JPanel();
basic_panel.setLayout(null);
basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
banner_panel = new JPanel();
banner_panel.setLayout(null);
banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
banner_panel.setSize(680, 200);//(400,100,400,100);
//////Banner inner things//////////////////////////////////////////////////
logo_label = new JLabel("Logo");
logo_label.setBounds(30,40,100,100);
logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(logo_label);
name_label = new JLabel(" Name..... ");
name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC,25));
name_label.setBounds(200,80,400,50);
name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(name_label);
////////////////////////////////////////////////////////////////////////
// basic_panel.add(banner_panel,BorderLayout.NORTH);
///////// Card Layout//////////////
basic2 = new CardLayout();
card_Layout_panel = new JPanel(basic2);
card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
basic_panel.add(card_Layout_panel,BorderLayout.CENTER);
welcome_authenticaion_panel_card1 = new JPanel();
welcome_authenticaion_panel_card1.setLayout(null);
welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
welcome_authenticaion_panel_card1.setBounds(0,200,680,460);
card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");
basic_panel.add(card_Layout_panel,BorderLayout.CENTER);
/////////////////////////////////////////////////////////////////////////
}
public void standard_gui(){
setSize(700,700);
setTitle("System");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
I want to make a page that has a banner and a pannel in which cards
will change on the requirement.
your component aren't focusable, there is required some event (JButton, Swing Timer) for switching the view by using CardLayout
for more info about CardLayout to read Oracle tutorial, for working code exampes, tons code examples are here
you code works without NullLayout (by set BorderLayout to parent JPanel), default LayoutManager for Jpanel is FlowLayout (accepts only getPreferredSize, childs aren't resizable with its parent/s)
my question is for why reason is there code line basic_panel.add(card_Layout_panel, BorderLayout.CENTER); twice, and another ...
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Gui extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel basic_panel, card_Layout_panel,
banner_panel, welcome_authenticaion_panel_card1;
private CardLayout basic2;
private JLabel logo_label, name_label;
public Gui() {
server_login_gui();
add(basic_panel);
standard_gui();
}
public void server_login_gui() {
basic_panel = new JPanel();
basic_panel.setLayout(new BorderLayout(10, 10));
basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
banner_panel = new JPanel();
//banner_panel.setLayout(null);
banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
banner_panel.setSize(680, 200);//(400,100,400,100);
//////Banner inner things//////////////////////////////////////////////////
logo_label = new JLabel("Logo");
//logo_label.setBounds(30, 40, 100, 100);
logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(logo_label);
name_label = new JLabel(" Name..... ");
name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC, 25));
//name_label.setBounds(200, 80, 400, 50);
name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(name_label);
////////////////////////////////////////////////////////////////////////
basic_panel.add(banner_panel, BorderLayout.NORTH);
///////// Card Layout//////////////
basic2 = new CardLayout();
card_Layout_panel = new JPanel(basic2);
card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
welcome_authenticaion_panel_card1 = new JPanel();
welcome_authenticaion_panel_card1.setLayout(null);
welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
//welcome_authenticaion_panel_card1.setBounds(0, 200, 680, 460);
card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");
basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
/////////////////////////////////////////////////////////////////////////
}
public void standard_gui() {
setSize(700, 700);
setTitle("System");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Gui();
}
});
}
}
you're doing this basic_panel.add(card_Layout_panel,BorderLayout.CENTER); twice, hence the error. ( check before and after the welcome_authentication_panel_card )
There is a frame. There a panel in that frame with BoxLayout. In that panel there is a ScrollPane. There is another panel in ScrollPane with SpringLayout. There is a label in that inner panel.
Here is a code:
package test;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
class MainPanel extends JPanel {
MainPanel() {
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxLayout);
panel.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel innerPanel = new JPanel();
SpringLayout springLayout = new SpringLayout();
innerPanel.setLayout(springLayout);
JLabel label = new JLabel("test");
innerPanel.add(label);
springLayout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, innerPanel);
springLayout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, innerPanel);
//innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
innerPanel.setPreferredSize(new Dimension(300, 100));
panel.add(innerPanel);
JScrollPane scrollPane = new JScrollPane(panel);
this.add(scrollPane);
}
}
class MainFrame extends JFrame {
MainFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new MainPanel());
this.pack();
this.setVisible(true);
}
}
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MainFrame();
}
});
}
}
Label "test" is visible as expected. But when I add a border to inner panel (uncomment the line, see below) the label is disappeare.
//innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
Can someone explane why?
Try to change
innerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
to
panel.setBorder(BorderFactory.createLineBorder(Color.black));
I hope that's what you meant to do & it fixed your problem :)
I would like to keep a panel I have created using an absolute layout in the center of my window even when the window is resized (if possible). I've come across a couple of suggestions here and [here][2] but no dice! Below is my sample code, any ideas or suggestions? I have no problems centered a single component like a JLable but I want to center a panel with many components!
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.JLabel;
public class TestPanel extends JFrame {
private JLabel lblSetupTitle;
private Border compoundBorder, outlineColorBorder, outlineBorder;
private JTextArea txtrManageData;
private JPanel childPanel;
public TestPanel()
{
setBackground(Color.white);
outlineColorBorder = BorderFactory.createLineBorder(Color.gray);
outlineBorder = BorderFactory.createEmptyBorder(20, 20, 20, 20);
compoundBorder = BorderFactory.createCompoundBorder(outlineColorBorder, outlineBorder);
lblSetupTitle = new JLabel("Setup");
lblSetupTitle.setBounds(443, 288, 44, 23);
txtrManageData = new JTextArea("Text Area Text");
txtrManageData.setBounds(393, 322, 142, 61);
childPanel = new JPanel();
childPanel.setLocation(89, 38);
childPanel.setSize(921, 452);
childPanel.setBorder(compoundBorder);
setupGUIElements();
setupPanel();
}
private void setupGUIElements()
{
txtrManageData.setBackground(null);
txtrManageData.setLineWrap(true);
txtrManageData.setWrapStyleWord(true);
}
private void setupPanel()
{
getContentPane().setLayout(new GridBagLayout()); // set layout of parent panel to GridBagLayout
childPanel.setLayout(null); // set layout of child panel to AbsoluteLayout
childPanel.add(lblSetupTitle);
childPanel.add(txtrManageData);
getContentPane().add(childPanel, new GridBagConstraints());
this.setSize(1020, 500);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestPanel ex = new TestPanel();
ex.setVisible(true);
}
});
}
}
EDIT: Any tips, links, guidance on creating something like this
I'd nest layouts.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class ThreeButtonTextFieldCombo {
private JPanel ui = null;
ThreeButtonTextFieldCombo() {
initUI();
}
public final void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridBagLayout());
ui.setBorder(new TitledBorder("Parent Panel"));
JPanel controls = new JPanel(new GridLayout(1,0,10,10));
ui.add(controls);
controls.setBackground(Color.RED);
controls.setBorder(new TitledBorder("Child Panel"));
for (int ii=1; ii<4; ii++) {
addLabelAndField(controls, "String " + ii);
}
}
public JComponent getUI() {
return ui;
}
private void addLabelAndField(JPanel panel, String text) {
JPanel controls = new JPanel(new BorderLayout(3, 3));
controls.setBorder(new EmptyBorder(20,20,20,20));
JLabel l = new JLabel(text);
controls.add(l, BorderLayout.PAGE_START);
JTextArea ta = new JTextArea(text, 2, 8);
controls.add(new JScrollPane(ta));
panel.add(controls);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
JFrame f = new JFrame("Three Button/Text Field Combo");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
ThreeButtonTextFieldCombo tbtfc =
new ThreeButtonTextFieldCombo();
f.setContentPane(tbtfc.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
The first problem with your code is that you are adding your child panel using an empty instantiation of GridBagConstraints. I have never seen it used like that before.
getContentPane().add(childPanel, new GridBagConstraints());
Do not set any layout to content pane and just add it like this :
getContentPane().add(childPanel);
Now if you run it you will get the two components in the middle, where you defined them using the setBounds(..) method.
Like almost everyone commenting on your question, you should not use null layout, and use some other layout instead. I would use a GridBagLayout to organise the three buttons and three textfields in your diagram. You could then setBounds(..) on your child panel.
If you really must use absolute layout then you will have to do a bit of maths.
If your first label is like this :
labell1.setBounds(443, 288, 44, 23);
then your second label should be something like this :
labell2.setBounds(443 + someXDisplacement, 288, 44, 23);
..and third :
labell3.setBounds(443 + (someXDisplacement x 2), 288, 44, 23);
You get the picture.
I am creating a JFrame object with some JPanels next to each other side by side.
I want the JPanels to have a 15px margin, etched border, and 15px padding. At first I thought that this would be something really intuitive just like the HTML box model, so I tried to create CompoundBorder inside a CompoundBorder but that wouldn't work.
Here's my code:
import java.awt.Dimension;
import javax.swing.*;
public class StackOverFlowExample extends JFrame {
public static void main() {
stackOverFlowExample window = new stackOverFlowExample();
window.setVisible(true);
}
public StackOverFlowExample() {
// create buttons
JButton foo = new JButton("foo");
JButton bar = new JButton("bar");
JButton foo2 = new JButton("foo2");
JButton bar2 = new JButton("bar2");
// create panels and add buttons to them
JPanel left = new JPanel();
left.setBorder(BorderFactory.createEtchedBorder());
left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
left.add(foo);
left.add(bar);
JPanel right = new JPanel();
right.setBorder(BorderFactory.createEtchedBorder());
right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
right.add(foo2);
right.add(bar2);
// add panels to frame
this.getContentPane().setLayout(new BoxLayout(
getContentPane(), BoxLayout.LINE_AXIS));
this.getContentPane().add(left);
this.getContentPane().add(right);
// finalize layout
this.setPreferredSize(new Dimension(150,150));
this.pack();
this.setVisible(true);
}
}
I'm aware that I could have just used GridBagConstraints or JButton.setMargin() to create the padding, and then use CompoundBorder to create the etched border with an empty border. What if I don't want to make my code look messy with those techniques though?
I'm not sure what problems you might be having, as you've not supplied an example of what you've tried, but the basic process would be to...
Create the inner border requirements (EtchedBorder wrapping a EmptyBorder), for example, new CompoundBorder(emptyBorder, etchedBorder)
Create the outer border requirements (EmptyBorder wrapping the inner compound border), for example, new CompoundBorder(inner, emptyBorder);
Apply this outer border to the component...
As an example...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
public class Test1 {
public static void main(String[] args) {
new Test1();
}
public Test1() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
EmptyBorder emptyBorder = new EmptyBorder(15, 15, 15, 15);
EtchedBorder etchedBorder = new EtchedBorder();
CompoundBorder inner = new CompoundBorder(emptyBorder, etchedBorder);
CompoundBorder outter = new CompoundBorder(inner, emptyBorder);
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(outter);
panel.add(new JButton("Hello"));
add(panel);
}
}
}
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;
public class ThreePartBorder {
public static void main(String[] args) {
final BufferedImage bi = new BufferedImage(
400, 100, BufferedImage.TYPE_INT_RGB);
Runnable r = new Runnable() {
#Override
public void run() {
JLabel l = new JLabel(new ImageIcon(bi));
Border twoPartBorder = new CompoundBorder(
new EmptyBorder(15, 15, 15, 15),
new EtchedBorder());
Border threePartBorder = new CompoundBorder(
twoPartBorder,
new EmptyBorder(15, 15, 15, 15));
l.setBorder(threePartBorder);
JFrame f = new JFrame("Three Part Border");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setContentPane(l);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
I came back and just realized I asked a dumb question haha. Both answers above are very helpful and helped me solve the problem so I accepted one of them. Here's my solution after reading the two answers...
left.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(10,10,10,10), // margin
BorderFactory.createEtchedBorder() // border
),
BorderFactory.createEmptyBorder(50,50,50,50) // padding
));
right.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(10,10,10,10), // margin
BorderFactory.createCompoundBorder(
BorderFactory.createEtchedBorder(), // border
BorderFactory.createEmptyBorder(50,50,50,50) // padding
)
));
I have frame with a button and a JPanel as I named panel, I want after I clicked the button add an inner panel to my panel. But this but there is a problem with this! because after adding second panel it didn't add any other panel.
Code
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import javax.swing.JTextField;
public class DrawImages extends JFrame{
int i;
public DrawImages() {
JButton btnNewButton = new JButton("New button");
i = 0;
getContentPane().add(btnNewButton, BorderLayout.SOUTH);
setMinimumSize(new Dimension(1000,150));
final JPanel panel = new JPanel();
panel.setSize(995, 145);
getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
final JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(0, 0, 46, 14);
panel.add(lblNewLabel);
btnNewButton.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent paramActionEvent) {
JPanel panel_1 = new JPanel();
//getContentPane().add(panel_1, BorderLayout.NORTH);
panel_1.setLayout(null);
JLabel imagelable = new JLabel(new ImageIcon("c:\\good.jpg"));
imagelable.setBounds(70, 5, 105, 65);
panel_1.add(imagelable);
JLabel lblNewLabel_4 = new JLabel("Up Label");
lblNewLabel_4.setBounds(5, 5, 65, 35);
panel_1.add(lblNewLabel_4);
JLabel lblNewLabel_2 = new JLabel("Down Label");
lblNewLabel_2.setBounds(5, 25, 65, 65);
panel_1.add(lblNewLabel_2);
lblNewLabel.setText(""+i);
panel_1.setBounds(5+170*i, 5, 170+170*i, 70);
panel.add(panel_1);
i++;
}
});
panel.setMinimumSize(new Dimension(995,150));
}
public static void main(String[]args)
{
DrawImages drawImages = new DrawImages();
drawImages.setVisible(true);
}
}
The problem is with the statement:
panel_1.setLayout(null);
panel_1 doesn't have any preferred size so will not appear (or will appear as a tiny dot).
Swing was designed to use layout managers. You could use GridLayout in this particular case.
Read: Doing Without a Layout Manager
I agree with Reimeus. Just to test your code, I used
panel_1.setLayout(new FlowLayout());
And I could see the panels being added without calling repaint() on the parent panel.
Thanks for your answers but my problem is I added third arguman in every circle:
panel_1.setBounds(5+170*i, 5, 170+170*i, 70);
so my panel get bigger and bigger (event biger than my monitor) so the correct is:
panel_1.setBounds(5+170*i, 5, 170, 70);