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.
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.
Java beginner here.
I am trying to generate Labels based on user input(take input for the number of labels to generate between 0 to 50) in a JPanel inside a JScrollPane.
The labels are generating correctly but the problem is the panel cant be scrolled down to view all the Labels.
Is it because I am using absolute layout for the panel? If yes then what might be the solution? Please guide.
Note: I made the labels using an array of 50 JLabels in a for loop. Terrible programming practice maybe but works.
Here's the code snippet
frame = new JFrame();
frame.setSize(800, 800);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(103, 37, 439, 350);
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(null);
JButton btnGenerateLabels = new JButton("Generate Labels");
btnGenerateLabels.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel[] lab = new JLabel[50];
int y = 50;
for(int i=0; i<50; i++)
{
lab[i] = new JLabel();
lab[i].setText("Label "+(i+1));
panel.add(lab[i]);
lab[i].setBounds(180, y, 97, 25);
y += 30;
}
}
});
btnGenerateLabels.setBounds(129, 23, 152, 25);
panel.add(btnGenerateLabels);
Is it because I am using absolute layout for the panel?
Yes. Don't use a null layout. Swing was designed to be used with layout managers.
The solution is to use a layout manager, probably the GridLayout as was suggested.
After all the components have been added to the panel you then need to invoke revalidate() and repaint() on the panel. This will invoke the layout manager and each component will be given a size/location.
Scrollbars will then appear as required.
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
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.
I have a Swing UI which contains 6 text fields and labels for the input and 1 button and texfield to show the output. now I want to make a border around these two.
I have read some materials regarding Titled borders but I think its only for single elements. Please suggest.
You could make a JPanel with a titled border, then put however many components you wanted in the JPanel using the content manager of your choice.
An example:
JPanel myPanel = new JPanel();
myPanel.setBorder(new TitledBorder(null, "My Title", TitledBorder.LEADING, TitledBorder.TOP, null, null));
myPanel.setLayout(new GridLayout(1, 0, 0, 0));
JButton button = new JButton("New button");
myPanel.add(button);
JLabel label = new JLabel("New label");
myPanel.add(label);
You can add that last 2 components to a JPanel and then add that panel to main frame. Now you can give border to JPanel and it will around 2 components inside it.
To give border to jPanel you can use following:
JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createLineBorder(Color.black));
If you want titled border then you can use following:
pane.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createMatteBorder(5, 5, 5, 5, Color.blue), "Title",
TitledBorder.LEFT, TitledBorder.TOP));
Reference: http://download.oracle.com/javase/tutorial/uiswing/components/border.html