So I have the following screen:
And this is my code:
setLayout(new BorderLayout());
JLabel lblTitulo = new JLabel("Sistema Generador de Examenes");
lblTitulo.setFont(new Font("Tahoma", Font.BOLD, 18));
JPanel panel1 = new JPanel();
panel1.setBackground(Color.white);
panel1.add(lblTitulo);
add(panel1, BorderLayout.NORTH);
JButton btnCrear = new JButton("Crear Examen");
JButton btnRendir = new JButton("Rendir Examen");
JButton btnCorregir = new JButton("Corregir Examen");
JButton btnVerCorrecciones = new JButton("Ver Correcciones");
btnCrear.setBounds(15, 100, 450, 35);
btnRendir.setBounds(15, 150, 450, 35);
btnCorregir.setBounds(15, 200, 450, 35);
btnVerCorrecciones.setBounds(15, 250, 450, 35);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.setLayout(null);
panel2.add(btnCrear);
panel2.add(btnRendir);
panel2.add(btnCorregir);
panel2.add(btnVerCorrecciones);
add(panel2, BorderLayout.CENTER);
1 - I'm using the BorderLayout. Do I need to have 2 JPanels to separate components (JLabel and JButtons) if I want to have the JLabel in the North and the JButtons in the Center? Or is there any way to use just one JPanel?
2 - I want to take out the setBounds used in my JButtons and use some Layout in order to have my JButtons like that in the middle of the screen. How could I do that?
I'm using the BorderLayout. Do I need to have 2 JPanels to separate components (JLabel and JButtons) if I want to have the JLabel in the North and the JButtons in the Center? Or is there any way to use just one JPanel?
Yes, you could use one JPanel and a GridBagLayout with a single column and some Insets to space the buttons from the label.
However, the nested layouts will keep the buttons in the center no matter how you resize the JFrame.
I want to take out the setBounds used in my JButtons and use some Layout in order to have my JButtons like that in the middle of the screen. How could I do that?
The GridBagLayout will space out the buttons with insets.
See this article, Sudoku Solver Swing GUI, for a couple of examples of dialogs that use the GridbagLayout.
Related
I am attempting to design a panel with MiGFormat that has a label at the top, and two buttons at the bottom - a yes/no prompt.
I achieve this closely, but the label yesOrNoText (text is "TEST") is not fully centered:
I initialize the panel containing this prompt like so:
private JPanel createYesNoPrompt() {
JPanel panel = new JPanel(new MigLayout());
panel.setBorder(BorderFactory.createLineBorder(Color.red));
JButton yesButton = new JButton("Yes");
JButton noButton = new JButton("No");
yesOrNoText = new JLabel();
yesOrNoText.setText("TEST");
yesOrNoText.setFont(panel.getFont().deriveFont(Font.BOLD, 30f));
yesOrNoText.setHorizontalAlignment(SwingConstants.CENTER);
Dimension dimension = new Dimension(500, 125);
Font font = panel.getFont().deriveFont(Font.BOLD, 20f);
yesButton.setFont(font);
yesButton.setBackground(new Color(35, 138, 35));
yesButton.setPreferredSize(dimension);
noButton.setFont(font);
noButton.setBackground(new Color(183, 19, 19));
noButton.setPreferredSize(dimension);
yesButton.addActionListener(e -> isYes = true);
noButton.addActionListener(e -> isYes = false);
panel.add(yesOrNoText, "wrap, dock center");
panel.add(yesButton);
panel.add(noButton);
return panel;
}
Then, I add it to gamePanel, then gamePanel to mainPanel, then mainPanel to the frame.
gamePanel.add(YesOrNoPanel, "align center");
mainPanel.add(gamePanel);
add(mainPanel);
I'm unsure of what would be causing yesOrNoText to not become fully centered within the YesNoPanel. Please let me know if I need to clarify anything!
Thank you.
I needed to make the add call for the yesNo label span 2 cells. By adding one component in the first row, then adding two in the next, I essentially created a 2x2 grid.
panel.add(yesOrNoText, "wrap, align center, span 2 1");
panel.add(yesButton);
panel.add(noButton);
Notice that on the first component I add yesOrNoText I use span to tell MiGFormat to take up two cells for this component. I can then center that with the remaining two components because it becomes the only component in the row.
I am making a copy of the apple calendar application, and I am having trouble aligning the month name and year name with the center of my screen, while aligning the left and right buttons with the left and right sides of the screen. Here is my code:
final JPanel months = new JPanel();
months.setLayout(new BoxLayout(months,BoxLayout.X_AXIS));
months.add(back, BorderLayout.WEST); //back is a JButton
JLabel monthName = new JLabel(this.monthNames[this.month]+" ", SwingConstants.CENTER);
JLabel year = new JLabel("" + this.year, SwingConstants.CENTER);
monthName.setFont(new Font("Helvetica", 0, 24));
year.setFont(new Font("Helvetica", 0, 24));
monthName.setHorizontalAlignment(JLabel.CENTER);
months.add(monthName, BorderLayout.CENTER);
months.add(year, BorderLayout.CENTER);
months.add(front, BorderLayout.EAST);
add(months);
Yet it shows up like this:
months.setLayout(new BoxLayout(months,BoxLayout.X_AXIS));
You are using a BoxLayout. A BoxLayout just adds the components horizontally to the panel. The WEST, CENTER, EAST constrains are only used by a BorderLayout so they are ignored by the BoxLayout.
months.add(monthName, BorderLayout.CENTER);
months.add(year, BorderLayout.CENTER);
When using a BorderLayout you can only add a single component to a region of the layout. So if you want to add two components to the CENTER you need to first create a panel and add the components to the panel.
So your basic code might be something like:
JPanel centerPanel = new JPanel();
centerPanel.add(month);
centerPanel.add(year);
JPanel mainPanel = new JPanel( new BorderLayout() );
mainPanel.add(westButton, BorderLayout.WEST);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(eastButton, BorderLayout.EAST);
When i add another JPanel into frame previous will dissapper (or probably overlaps the previous one). How can i stop this overlapping?
public Attack() {
JFrame frame = new JFrame("Oracle Padding Attack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.setLayout(null);
JLabel label1 = new JLabel("Inicialization vector:");
createTextField(10, 40, arrayIV, panel1, "HH", true, false);
label1.setBounds(10, 0, 120, 50);
panel1.add(label1);
panel2.setLayout(null);
JLabel label2 = new JLabel("Encrypted text:");
createTextField(400, 40, encryptedTextArray, panel2, "00", true, false);
label2.setBounds(400, 0, 120, 50);
panel2.add(label2);
frame.add(panel1);
frame.add(panel2);
frame.setSize(900, 400);
frame.setVisible(true);
}
It depends on how you want your two panels placed inside your frame. You will need a layout for your frame.
If you want to be able to "jump" from one to the other, cardLayout is your answer.
Otherwise if you want both side to side for example, you will have to use a layout inside your frame. I like the MigLayout, but GridLayout will do the job just fine.
http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html
All build in layout handlers can be found here: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Also it's not recommended to use null layout at all since it will break the look if the window is resized.
This is my code:
frame2 = new JFrame("Confirmation");
frame2.setLayout(new BorderLayout());
JRadioButton y,n,c;
panel = new JPanel();
ButtonGroup buttonGroup = new ButtonGroup();
y = new JRadioButton("Add");
buttonGroup.add(y);
panel.add(y);
n = new JRadioButton("Update");
buttonGroup.add(n);
panel.add(n);
c = new JRadioButton("Delete");
buttonGroup.add(c);
panel.add(c);
y.setSelected(true);
b1=new JButton();
b1.setBounds(300,100,2,2);
b1.setIcon(new ImageIcon(searchresult.class.getResource("/images/yes.png")));
b2=new JButton();
b2.setBounds(100,10,2,2);
b2.setIcon(new ImageIcon(searchresult.class.getResource("/images/no.png")));
panel.add(b1);
panel.add(b2);
frame2.add(panel);
frame2.setSize(182,150);
frame2.setVisible(true);
Right now this gives me the following output
whereas I want this
with an increased width but I am not able to do it..Could anyone provide me with further details that could help me
JPanel uses a FlowLayout by default, which, as the name suggests, layouts out components one after the after, in a flow...
Two choices. Use a compound layout, using BorderLayout as the base, create JPanel that uses a GridLayout for the radio buttons (using 0 rows and 1 column), add this to the CENTER position of the base panel.
Create a second JPanel using a FlowLayout and your buttons to it. Add this to the SOUTH position of the base pane.
Second choice is to use a GridBagLayout
Take a look at Laying out Components within a Container for more details
I'm trying to add a JLabel to a JPanel to a JFrame. I set the border for the JPanel, but all I see on the JFrame is a small black square in the center of my frame. Whatever I do I can't change the size or location of it. Please help.
Start main = new Start();
Random random = new Random();
JFrame mainFrame = new JFrame("MainFrame");
JPanel mainPanel = new JPanel();
JLabel welcomeLabel = new JLabel();
mainFrame.add(main);
mainFrame.setLayout(new GridBagLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setTitle(names[random.nextInt(names.length)]);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setSize(mainFrameX, mainFrameY);
mainFrame.setResizable(false);
mainFrame.setLocationRelativeTo(null);
mainFrame.add(mainPanel);
mainPanel.add(welcomeLabel);
mainPanel.setBorder(new LineBorder(Color.BLACK));
mainPanel.setSize(new Dimension(200, 200));
welcomeLabel.setFont(new Font("Verdana", 1, 20));
welcomeLabel.setLocation(100, 100);
main.start();
Suggestions:
You will want to read the tutorial, Laying out Components, as it will explain how to code with the Swing layout managers, and this information is essential to solve your current problem.
One caveat: I urge you to avoid the temptation to use the null layout as use of it will lead to creation of code that is very hard to maintain or upgrade.
Your JLabel, welcomeLabel, will of course need some text to be visible.
Don't set it's location via setLocation(...) but again use the layout managers to do the dirty work of placing and sizing your components.
You will also want to call pack() and setVisible(true) on your JFrame after adding all initial components.
Hovercraft is right (+1), make sure you understand how the layout managers are working.
The order in which you do things are important, especially when dealing with the top level containers...
Start main = new Start();
Random random = new Random();
JFrame mainFrame = new JFrame("MainFrame");
JPanel mainPanel = new JPanel();
JLabel welcomeLabel = new JLabel();
welcomeLabel.setFont(new Font("Verdana", 1, 20));
mainPanel.add(welcomeLabel);
mainPanel.setBorder(new LineBorder(Color.BLACK));
// Do this first
mainFrame.setLayout(new GridBagLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setTitle(names[random.nextInt(names.length)]);
// Add your components
mainFrame.add(main);
mainFrame.add(mainPanel);
// Prepare the window for showing, now you have some content.
mainFrame.setResizable(false);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setLocationRelativeTo(null);
main.start();
This will still only produce a small black square in the window, because the JLabel has no content and therefore it's preferred size is going to be (something like) 2x2 (because of the border).
Try adding some text to...
welcomeLabel.setText("Welcome");
And then see the difference