Java - Painting - java

Hello im trying to create a grid in java and paint each cell but I dont kown what im doing wrong. Each cell is a JPanel and I add each cell to the to the mazePanel. After that the mazePanel is added to the frame. But only get the result shown in the next image:
public void printFrame() {
JFrame frame;
JPanel mazePanel = new JPanel();
frame = new JFrame("The Maze");
frame.setSize(600, 600);
mazePanel.setLayout(new GridLayout(2, 2));
mazePanel.setBackground(Color.cyan);
JPanel cell = new JPanel();
cell.setBackground(Color.gray);
mazePanel.add(cell);
cell.setBackground(Color.BLACK);
mazePanel.add(cell);
cell.setBackground(Color.red);
mazePanel.add(cell);
cell.setBackground(Color.GREEN);
mazePanel.add(cell);
frame.add(mazePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

A Component cannot be added to a Container more than once (eg cannot have multiple parents). In this case, what you see is the result of the last addition of the variable 'cell' to its parent mazePanel (green - together with the background color of mazePanel (cyan) due to the layout). If you wish to add multiple 'cells', then create new JPanel's with the appropriate background color and add them to the mazePanel

JFrame frame;
JPanel mazePanel = new JPanel();
frame = new JFrame("The Maze");
frame.setSize(600, 600);
mazePanel.setLayout(new GridLayout(2, 2));
mazePanel.setBackground(Color.cyan);
JPanel cell = new JPanel();
JPanel cell2 = new JPanel();
JPanel cell3 = new JPanel();
JPanel cell4 = new JPanel();
cell.setBackground(Color.gray);
mazePanel.add(cell);
cell2.setBackground(Color.BLACK);
mazePanel.add(cell2);
cell3.setBackground(Color.red);
mazePanel.add(cell3);
cell4.setBackground(Color.GREEN);
mazePanel.add(cell4);
frame.add(mazePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
You have to create separate cell objects like mentioned above. I believe this gives you what you are looking for.

Related

JPanel won't show in JFrame

I'm trying to create a JPanel (non-resizable) showing a grid of buttons but when I try to add the JPanel to a JFrame it won't show.
JFrame frame = new JFrame("frame");
JPanel panel = new JPanel();
frame.setSize(681,920);
frame.setResizable(true);
JLabel label = new JLabel();
label.setLayout(new FlowLayout(FlowLayout.LEADING,0,0));
JButton btn = new JButton();
btn.setContentAreaFilled( false );
btn.setBorder( null );
btn.setBounds(214,210,0,0);
label.add(btn);
panel.add(label);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
The output should be a resizable frame with inside a 3x4 grid of buttons.
If I don't use the panel and I put the line frame.setResizable(false) it works as expected but I need to add more stuff to the frame so I need to put the buttons safe in a panel.
Both panel and label are added to your frame, to make sure they are added write
JLabel label = new JLabel("JLABEL");
and
panel.setBackground(Color.BLUE);

How to for loop jPanel in jFrame?

May I know why my jPanel does not appear in the jFrame? I want to make 5 blue jPanel appear in the jFrame but why only 1 blue jPanel appear in my jFrame? Thanks for helping!
public class NewJFrame2 extends javax.swing.JFrame {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
/**
* Creates new form NewJFrame2
*/
public NewJFrame2() {
initComponents();
JPanel[] panelArray = new JPanel[5];
JButton btnArray[] = new JButton[5];
for(int i = 0; i<5;i++)
{
panelArray[i] = new JPanel();
//panelArray[i].setVisible(true);
System.out.println(panelArray[i]);
javax.swing.border.Border border = BorderFactory.createLineBorder(Color.BLUE, 5);
panelArray[i].setBackground(Color.YELLOW);
panelArray[i].setBorder(border);
frame.getContentPane().add(panelArray[i]);
}
frame.setSize(new Dimension(500, 400));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("A Simple JFrame");
frame.setVisible(true);
}
As mentioned in the comments you want a LayoutManager.
The current issue is that you are adding all five panels to the exact same space on your frame. To solve this issue you need to provide a structure for the frame to associate different coordinates with different areas.
This answer contains a good jumping off point for you to start to play with layouts in Java.
Using a container JPanel with a BoxLayout -- see comments below for further info :
initComponents();
JPanel[] panelArray = new JPanel[5];
JButton btnArray[] = new JButton[5];
JPanel container = new JPanel(); // Container JPanel
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS)); // With a BoxLayout
for (int i = 0; i < 5; i++) {
panelArray[i] = new JPanel();
//panelArray[i].setVisible(true);
System.out.println(panelArray[i]);
javax.swing.border.Border border = BorderFactory.createLineBorder(Color.BLUE, 5);
panelArray[i].setBackground(Color.YELLOW);
panelArray[i].setBorder(border);
container.add(panelArray[i]); // Adding 5 JPanels to container JPanel
}
frame.getContentPane().add(container); // Adding container JPanel to JFrame
frame.setSize(new Dimension(500, 400));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("A Simple JFrame");
frame.setVisible(true);

Java GUI Logic - Not understanding adding to JFrame

I have a pretty straight forward question. Can some please please explain to me why the following JFrame is not showing Hello (100,100 pixels on the left side of the screen and World (100,100 pixels) on the right side of the screen since I am using border layout.
I created a JFrame
Assigned it a layout of borderlayout
Created 2 panels with 2 labels and assigned the panels to be aligned left and right.
added the panels to the JFrame
Displayed the JFrame
What am I missing?
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(100,100));
panel1.setBackground(Color.BLUE);
JLabel label1 = new JLabel("Hello");
label1.setBackground(Color.YELLOW);
label1.setForeground(Color.WHITE);
panel1.add(label1,BorderLayout.LINE_START);
frame.add(panel1);
JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(100,100));
panel2.setBackground(Color.RED);
JLabel label2 = new JLabel("World");
label2.setBackground(Color.CYAN);
label2.setForeground(Color.WHITE);
panel2.add(label2,BorderLayout.LINE_END);
frame.add(panel2);
You are setting the Layout-Constraints on the wrong panel.
Instead of panel2.add(label2,BorderLayout.LINE_END); it should be panel2.add(label2) and instead of frame.add(panel2); it should be frame.add(panel2, BorderLayout.LINE_END);.
Same for panel1.

JButtons only appear on JFrame if in BorderLayout.CENTER, not SOUTH or NORTH

So I'm trying to create a gui, I've tinkered with gui's before in java but I'm still new to them. So my issued here is that my JLabels (butLabel & cbLabel) are filled with buttons and checkboxes. Sadly my JFrame will only show whichever is set to the BorderLayout.CENTER. NORTH & SOUTH don't ever show, even if I only set the butLabel to SOUTH and don't even use the cbLabel. What am I overlooking?? It's much appreciated, thanks!
public class mainWindow
{
JFrame frame = new JFrame("Main Window");
JLabel butLabel = new JLabel();
JLabel cbLabel = new JLabel();
JButton showBut = new JButton("Show");
JButton exitBut = new JButton("Exit");
JButton addBut = new JButton("Add");
JButton remBut = new JButton("Remove");
JCheckBox aCB = new JCheckBox("Airplane");
JCheckBox bCB = new JCheckBox("Boat");
JCheckBox cCB = new JCheckBox("Clock");
public mainWindow()
{
frame.setLayout(new BorderLayout()); //I know this is set by default to BorderLayout but I just did it when I was out of options to try.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(360, 480));
butLabel.setLayout(new GridLayout(1,4));
cbLabel.setLayout(new GridLayout(2, 2));
butLabel.add(showBut);
butLabel.add(exitBut);
butLabel.add(addBut);
butLabel.add(remBut);
cbLabel.add(aCB);
cbLabel.add(bCB);
cbLabel.add(cCB);
frame.add(butLabel, BorderLayout.CENTER);
frame.add(cbLabel, BorderLayout.NORTH);
}
public void setVisible()
{
butLabel.setVisible(true);//Didn't think I needed butLabel.setVisible or the cbLabel.setVisible but
cbLabel.setVisible(true);//again I was trying things that I thought might make sense.
frame.setVisible(true);
}
}
do not use Label for grouping elements, use JPanel instead
I have tried replace all
Label
with
Panel
it works

How to center a component on a row containing multiple components with MiGLayout

I started using MiGLayout about a month and half ago and everything is simple and works great. There's only one issue I still have that I haven't been able to fix.
Let's say I want to have a row that has two buttons on the right-most side and a centered title, the title doesn't actually get centered when I do it this way:
("this" is a JPanel)
this.add(labelTitle, "split, span, center");
this.add(closeButton, "east");
this.add(mainMenuButton, "east");
What happens is that "labelTitle" is centered in the remaining space available after the buttons are placed, but I actually want it to be centered relative to the whole JPanel, not just the remaining space.
What parameters could I use to get the desired effect? I know I could use absolute positioning, but I don't want to do that because it defeats the purpose of using MiGLayout in the first place in my case.
Can it be something like this you are looking for?
Cheers!
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel(new MigLayout("debug"));
panel.add(new JLabel("Label Title"), "x2 min(b1.x - unrel, (container.w+pref)/2)");
panel.add(new JButton("Close Button"), "id b1, pushx, alignx right");
panel.add(new JButton("Main Menu Button"), "alignx right");
frame.add(panel);
frame.setSize(800, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
You can use the JXLayer and put the buttons in the glasspane.
JButton closeButton = new JButton("Close");
JButton mainMenuButton = new JButton("Menu");
JLabel labelTitle = new JLabel("Application");
JPanel panel = new JPanel();
panel.setLayout(new MigLayout(new LC().fillX()));
panel.add(labelTitle, new CC().alignX("center").spanX());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new MigLayout(new LC().fillX()));
buttonPanel.add(closeButton, new CC().alignX("right").split());
buttonPanel.add(mainMenuButton, new CC().alignX("right"));
buttonPanel.setOpaque(false);
JXLayer<JPanel> mainPanel = new JXLayer<JPanel>();
mainPanel.setView(panel);
mainPanel.setGlassPane(buttonPanel);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(mainPanel);
frame.setSize(400, 600);
frame.setVisible(true);
When creating your JPanel, use the following MigLayout initializer:
new MigLayout("","[]push[center]push[]","")
If you don't know about constraints, check here: MigLayout Whitepaper
This is assuming you don't have anything else in this JPanel...

Categories