Okay, so I'm having some trouble with my Programming Exercise today.
The Exercise text goes like this:
(Use the FlowLayout manager) Write a program that meets the following requirements:
Create a frame and set its layout to FlowLayout
Create two panels and add them to the frame
Each panel contains three buttons. The panel uses FlowLayout
The buttons should be named "Button 1", "Button 2" and so on.
I think I'm having some trouble with adding the panels to the frame because when i run the program, it shows an empty frame.
Here is the code i have.
import javax.swing.*;
import java.awt.*;
public class Exercise12_1 extends JFrame {
public Exercise12_1() {
setLayout(new FlowLayout());
JFrame frame = new JFrame(" Exercise 12_1 ");
frame.setLayout(new FlowLayout());
// Create two panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout());
// Add three buttons to each panel
panel1.add(new JButton(" Button 1 "));
panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));
// Add panels to frame
frame.add(panel1);
frame.add(panel2);
}
public static void main(String[] args) {
Exercise12_1 frame = new Exercise12_1();
frame.setTitle(" Exercise 12_1 ");
frame.setSize(600, 100);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I would greatly appreciate it if some of you took your time to help me out here.
Thanks.
Your main method creates a frame:
Exercise12_1 frame = new Exercise12_1();
and then makes it visible.
And the constructor of this 'Exercise12_1' frame creates another frame, and adds panel to this other frame:
JFrame frame = new JFrame(" Exercise 12_1 ");
frame.setLayout(new FlowLayout());
The constructor shouldn't create another frame. It should add the panels to this: the frame being constructed, and that will then be made visible.
Also, you should not use setSize(), but pack(), to make the frame have the most appropriate size based on the preferred size of all the components it contains.
Check this:
import javax.swing.*;
import java.awt.*;
public class Exercise12_1 extends JFrame {
public Exercise12_1() {
setLayout(new FlowLayout());
//JFrame frame = new JFrame(" Exercise 12_1 ");
this.setLayout(new FlowLayout());
// Create two panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout());
// Add three buttons to each panel
panel1.add(new JButton(" Button 1 "));
panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));
// Add panels to frame
this.add(panel1);
this.add(panel2);
}
public static void main(String[] args) {
Exercise12_1 frame = new Exercise12_1();
frame.setTitle(" Exercise 12_1 ");
frame.setSize(600, 100);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
You use use such code like this.getContentPane().add(panel1); to add Panel.
Change
frame.add(panel1);
frame.add(panel2);
To
this.getContentPane().add(panel1);
this.getContentPane().add(panel2);
and it will be working then.
Related
I am trying to write a Title for the main menu of my program, by using a JLabel, but it doesn't seem to appear on my screen.
import javax.swing.*;
public class GUI {
public GUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Title");
frame.pack();
frame.setSize(854,560);
frame.setVisible(true);
JLabel title = new JLabel();
title.setText("Title");
//title.setSize();
title.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
What am I doing wrong and how could I change the position of the Text if I manage to make it visible?
And I also want to add a button to go to the next page so if you could tell me how to do that too that would be great.
I would quickly and untested say that you are adding the label after you set the frame visible.
Do it before. Else you would have to revalidate and repaint the frame
As I can see in your code you are not adding title in panel. As a quick solution put panel.add(title); after title.setVisible(true); line in your code, it will display the label.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Title");
frame.pack();
frame.setSize(854,560);
frame.setVisible(true);
JLabel title = new JLabel();
title.setText("Title");
//title.setSize();
title.setVisible(true);
panel.add(title); //<---- this one line will diaplay label in GUI
I have JFrame that uses FlowLayout for buttons and BoxLayout for the JFrame and looks like this:
I need it to look like this:
For some reason the JPanel of the buttons (green) takes up too much space, while the labels on the red panel are all on the same row, instead of each on a different row.
My code is as follows:
import javax.swing.*;
import java.awt.*;
public class ButtonsTest extends JFrame {
private JButton button1 = new JButton("Button1");
private JButton button2 = new JButton("Button2");
private JButton button3 = new JButton("Button3");
private JButton button4 = new JButton("Button4");
private JPanel panel = new JPanel(new FlowLayout());
private JPanel otherPanel = new JPanel();
public ButtonsTest() {
setPreferredSize(new Dimension(200, 200));
setMinimumSize(new Dimension(200, 200));
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.setBackground(Color.GREEN);
add(panel);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
otherPanel.add(new Label("1"));
otherPanel.add(new Label("2"));
otherPanel.add(new Label("3"));
otherPanel.setBackground(Color.RED);
add(otherPanel);
pack();
}
public static void main(String[] args) {
ButtonsTest test = new ButtonsTest();
}
}
What is my mistake?
For some reason the JPanel of the buttons (green) takes up too much space
When using a BoxLayout, the components will grow up to the maximum size when extra space is available. So extra space is allocated to both the red and green panels.
Don't set the layout of the content pane to use a BoxLayout.
while the labels on the red panel are all on the same row, instead of each on a different row.
By default a JPanel uses a Flow layout.
The solution is to use the default BorderLayout of the JFrame.
Then you add the green panel to the frame using:
add(panel, BorderLayout.PAGE_START);
Then for the "otherPanel" you can use the BoxLayout:
otherPanel.setLayout( new BoxLayout(otherPanel, BoxLayout.Y_AXIS) );
Then you add the "otherPanel" to the frame using:
add(otherPanel, BorderLayout.CENTER);
Also, components should be added to the frame BEFORE the frame is visible. So the setVisible(...) statement should be the last statement in the constructor.
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.
My objective here is to display a message to the console, showing which button I have pressed (the buttons go from 1-6). This is the furthest I have gotten.
CODE:
public class excercise5_1 extends JFrame {
public excercise5_1() {
setLayout(new FlowLayout());
// Create two panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
// Add three buttons to each panel
panel1.add(new JButton(" Button 1 "));
panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));
// Add panels to frame
add(panel1);
add(panel2);
}
public static void main(String[] args) {
excercise5_1 frame = new excercise5_1();
frame.setTitle(" Exercise 12_1 ");
frame.setSize(600,75);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I just don't know what to do to make it display "Button 2" if I press it.
Don't create your JButton's inline -- in the add method parameter. So not panel1.add(new JButton(...)),
Instead create your JButton on its own line: but rather JButton myButton = new JButton(...); and then panel1.add(myButton);
Use a for loop to simplify things.
Add an ActionListener to your button via its addActioListener(...) method, and have the ActionListener print the ActionEvent's getActionCommand() String. The ActionEvent is the parameter passed into the actionPerformed(ActionEvent e) method.
Read the JButton tutorials as it's all spelled out for you there. It appears as if you've read nothing about JButtons yet.
Problem : i'm trying to add a 2nd Jpanel in my frame but when i add this latter, it overwrites the previous one. The purpose is to have 2 components (Jpanels) in the same frame but it seem to accept only one but not both. The order of appearence should be in one column and two rows:
1: Enter name:
2: TextField
import javax.swing.*;
import java.awt.*;
public class Money2 extends JFrame {
public Money2() {
// setLayout(new GridLayout(2,2));
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(new JLabel("Enter name:"));
// -------------------------------------------------------------------------
// p2.setLayout(new FlowLayout(FlowLayout.CENTER));
JPanel p2 = new JPanel(new FlowLayout());
p2.add(new JTextField(8));
add(p1); // add to Jframe
add(p2);
}
/** Main method */
public static void main(String[] args) {
Money2 frame = new Money2();
frame.setTitle("Money Converter App");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 400);
frame.setVisible(true);
}
}
Both panels cannot occupy the same location in a BorderLayout. You could place panel p1 at a different location:
add(p1, BorderLayout.PAGE_START);