Hello,How can i add two panels in one frame? [duplicate] - java

This question already has answers here:
How to add JTable in JPanel with null layout?
(11 answers)
Closed 6 years ago.
Can any one help me?
Hello,How can i add two panels in one frame?
public class test{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(5, 5, 300, 300);
JPanel panel2 = new JPanel();
panel2.setLayout(null);
panel2.setBounds(1,200,300,300);
JLabel label2 = new JLabel("asddas");
label2.setBounds(30,30,20,20);
panel2.add(label2);
JLabel label[] = new JLabel[10];
int count = 1;
for(int i = 0; i < 10; i++){
label[i] = new JLabel("ds");
label[i].setBounds(1,count,20,20);
count +=20;
panel.add(label[i]);
}
frame.add(panel,panel2);
frame.setVisible(true);
}
}

You can think of the JPanel as one big panel that contains the all of the other elements. So you can have a main JPanel and then put others inside it. You should set a layout that fits your needs to the main panel. A good introduction to layouts can be found here http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
Also see this answer
How to layout multiple panels on a jFrame? (java)

Related

Moving labels in nested panels [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I've made two grids and added them to a window using nested panels. The only issue is that I can't move the grid in the center and can't get the labels under their respective grids. Tried using setBounds but that's not working. Any advice? I've added an image of the current state. I want to display the player and opponent label under the first and second grid respectively.
public static void main(String[] args) {
window = new JFrame();
window.setTitle("Battleship.exe");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setPreferredSize(new Dimension(800, 800));
P1_container = new JPanel(new GridLayout(10,10));
P1_container.setPreferredSize(new Dimension(400, 400));
P1_container.setBorder(BorderFactory.createLineBorder(Color.black, 5));
compContainer = new JPanel(new GridLayout(10,10));
compContainer.setPreferredSize(new Dimension(400, 400));
compContainer.setBorder(BorderFactory.createLineBorder(Color.black, 5));
grid = new JPanel[10][10];
for (int i =0; i< 10; i++) {
for (int j =0; j< 10; j++) {
grid[i][j] = new JPanel();
grid[i][j].setBackground(Color.white);
grid[i][j].setBorder(BorderFactory.createLineBorder(Color.blue, 2));
grid[i][j].setPreferredSize(new Dimension(35,35));
P1_container.add(grid[i][j]);
}
}
enemyGrid = new JPanel[10][10];
for (int i =0; i< 10; i++) {
for (int j =0; j< 10; j++) {
enemyGrid[i][j] = new JPanel();
enemyGrid[i][j].setBackground(Color.lightGray);
enemyGrid[i][j].setBorder(BorderFactory.createLineBorder(Color.red, 2));
enemyGrid[i][j].setPreferredSize(new Dimension(35, 35));
compContainer.add(enemyGrid[i][j]);
}
}
GridLayout layout = new GridLayout(1, 2);
layout.setHgap(150);
mainPanel = new JPanel(layout);
mainPanel.add(P1_container);
mainPanel.add(compContainer);
player = new JLabel("PLAYER");
player.setBounds(100, 410, 5, 5);
opponent = new JLabel("OPPONENT");
opponent.setBounds(100, 410, 5, 5);
panel = new JPanel();
panel.setPreferredSize(new Dimension(100, 100));
panel.add(mainPanel, BorderLayout.CENTER);
panel.add(player, BorderLayout.WEST);
panel.add(opponent, BorderLayout.WEST);
window.add(panel, BorderLayout.CENTER);
window.pack();
window.setVisible(true);
}
Please mark your previous questions as solved if they are. People have taken the time to help you out and it's the least you can do. Read here for more information on What should I do when someone answers my question?
You are STILL calling setPreferredSize you should instead override getPreferredSize and ONLY where necessary. If your grid JPanels are sized via getPreferredSize there is no need to call setPreferredSize on their respective containers or the JFrame also you are still not creating your Swing components on the EDT.
As others have mentioned, you cannot use setBounds when using a LayoutManager. To achieve what you want, you need to nest layouts, as you have been told before.
So you probably want to create two JPanels with a BorderLayout. These two new containers will hold each grid and its label respectively:
JPanel p1Container = new JPanel(new BorderLayout());
p1Container.add(P1_container, BorderLayout.CENTER);
p1Container.add(player, BorderLayout.SOUTH);
JPanel opponentContainer = new JPanel(new BorderLayout());
opponentContainer.add(compContainer, BorderLayout.CENTER);
opponentContainer.add(opponent, BorderLayout.SOUTH);
...
panel.add(p1Container);
panel.add(opponentContainer);
Also the below code makes no sense:
panel = new JPanel();
panel.setPreferredSize(new Dimension(100, 100));
panel.add(mainPanel, BorderLayout.CENTER);
panel.add(player, BorderLayout.WEST);
panel.add(opponent, BorderLayout.WEST);
By default, a JPanel uses FlowLayout so BorderLayout.XXX means nothing here.
Again take the time to read A Visual Guide to Layout Managers but the code I showed corrects this by not passing in any extra parameters to add()

How can I display a JTable on a JFrame

I've spent hours online and I'm afraid I can't quite figure out how to make my JTable show up next to my JButton on a JFrame, if anyone has a simple or comprehensive way to explain why and how to fix the problem Id really appreciate it.
Extensive online research including downloading samples and applying various suggestions; also reached out to my teacher for help but she doesn't have much experience with java.
public class canvas extends JTable{
static void displayJFrame(){
//set variables
int i = 0;
String[][] strArray = new String[3][i];
String[] labelArray = new String[3];
labelArray[0] = "Name: ";
labelArray[1] = "Subject: ";
labelArray[2] = "Average: ";
//create JFrame
JFrame f = new JFrame("Test Average");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setBackground(Color.WHITE);
f.setPreferredSize(new Dimension(600, 600));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
//create JButton
JButton button=new JButton("Enter New Grade");
button.setBounds(450,15,140, 40);
button.addActionListener(e -> average(strArray, i));//gets info from user and adds to strArray
button.addActionListener(e -> count(i));//increases counter
f.add(button);
//create JTable
JTable j = new JTable(strArray, labelArray);
JScrollPane scrollPane = new JScrollPane(j);
j.setBounds(30, 40, 200, 300);
j.setSize(500, 200);
j.setVisible(true);
}
}
all of my code runs as expected except there is no table, I've also tried so many things that didn't work so this basically where I started so that its not crowded by tons of incorrect stuff
You have several problems. First, by default a JFrame uses a BorderLayout. Using the default add(component) method places that component in the center. Using the same add() method again just replaces the new component at the center.
Second, do not pack or set the frame visible until AFTER you have created the entire GUI.
You would do better to create a JPanel and add the components to that panel, then add the panel to the frame. The default layout manager for JPanel is a FlowLayout.

BoxLayout can't be shared error, trying to fit two JPanels in a JFrame [duplicate]

This question already has answers here:
error upon assigning Layout: BoxLayout can't be shared
(4 answers)
Closed 4 years ago.
Trying to have 2 panels in a frame, one small one at the top and one filling up the rest of the frame. This code returns a "BoxLayout can't be shared" error though.
JFrame frame = new JFrame("Clients");
frame.setSize(1000,900);
JPanel sorters = new JPanel();
sorters.setSize(1000, 100);
frame.getContentPane().add(sorters);
JPanel rowPane = new JPanel();
JScrollPane scrPane = new JScrollPane(rowPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrPane.setSize(1000, 800);
frame.getContentPane().add(scrPane);
frame.getContentPane().setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
A BoxLayout must have as first parameter the actual container it is applied to, in your case the container is the contentPane of the frame :
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

How can i add JButtons to a panel in vertical way? [duplicate]

This question already has answers here:
Java: vertical alignment within JPanel
(3 answers)
Closed 4 years ago.
the task i am trying to do is simple. I want to add JButtons to a panel in a vertical way, but using a loop to adding it, i tried to do it using .setBounds() and .setLocation() mehtod, but i dont have any results.
In a simple way, i want to do this but adding the buttons vertically and keeping the JScroll bar...:
public class NewMain {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setLayout(null);
for (int i = 0; i < 10; i++) {
JButton asd=new JButton("HOLA "+i);
asd.setLocation(i+20, i+20);
panel.add(asd);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(50, 30, 300, 50);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500, 400));
contentPane.add(scrollPane);
frame.setContentPane(contentPane);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
Give the JPanel that holds the JButtons an appropriate layout manager that adds components in a vertical manner. A GridLayout(0, 1) would work, the parameters referring to 0 rows -- meaning variable number of rows, and 1 column. This will add the JButtons into a vertical grid, column of one
Other possible solutions include BoxLayout and GridBagLayout, both of which are a little more complex than the GridLayout.
Also avoid using null layouts as you're doing as this leads to inflexible GUI's painful debugging and changes.

using JScrollPane with JFrame, JPanel and GridLayout [duplicate]

This question already has answers here:
Java: Can't apply Gridlayout to a Jscrollpane. Get Get java.lang.ClassCastException
(1 answer)
ScrollPane adding to grid layout
(3 answers)
Closed 5 years ago.
I have a GridLayout (of JTextFields : 57*57) in a JPanel in a JFrame (resolution 800*800) and the result is really small and that's why I would like to have a JScrollPane (that would have a 800*800 resolution and the GridLayout resolution would be 3000*3000 for example).
I've tried many things but it was not working. Can someone please help me ?
Here is the concerned part of the code :
Thanks.
GridLayout grid = new GridLayout(thumbnail.getHeight(), thumbnail.getWidth(),0,0);
final_frame = new JFrame();
final_frame.setPreferredSize(new Dimension(800, 800));
JPanel myPanel = new JPanel();
myPanel.setLayout(grid);
//fill grid with colors and denomination
for (int yy=0;yy<thumbnail.getHeight();yy++){
for (int xx=0;xx<thumbnail.getWidth();xx++){
for (int kk=0;kk<colors.length;kk++){
if (thumbnail.getRGB(xx,yy)==colors[kk].getRGB()){
//System.out.println("Color : "+colors[kk]+" libel : H"+libel[kk]);
JTextField tf = new JTextField("H"+libel[kk]);
tf.setBackground(colors[kk]);
tf.setSize(10, 10); //do nothing ?
myPanel.add(tf);
}
}
}
}
final_frame.add(myPanel);
final_frame.pack();
final_frame.setVisible(true);
final_frame.setLocationRelativeTo(null);
final_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Categories