can't make Jframe in center using setLocationRelativeTo(null); - java

I have a jframe when i use
setLocationRelativeTo(null);
the JFrame does not appear at the middle of window why it is so
my full code-
JFrame ca = new JFrame("Start");
ca.setUndecorated(true);
ca.setLocationRelativeTo(null);
JLabel lab = new JLabel(" Space Invaders");
JButton bu = new JButton("START");
JButton bu2 = new JButton("QUIT");
lab.setFont(new Font("Serif Italic", Font.BOLD, 60));
lab.setForeground(Color.RED);
bu.setForeground(Color.WHITE);
bu.setBackground(Color.BLACK);
bu.setFont(new Font("serif", Font.BOLD, 50));
bu.setBorderPainted(false);
bu2.setForeground(Color.WHITE);
bu2.setBackground(Color.BLACK);
bu2.setFont(new Font("serif", Font.BOLD, 50));
bu2.setBorderPainted(false);
My Output is this-

setLocationRelativeTo(null);
Must be invoked AFTER you have added components to the frame and invoked pack() on the frame, otherwise the frame has a size of (0, 0) so it can't be centered properly.
Edit:
JFrame frame = new JFrame(...);
frame.add(someComponent);
frame.add(anotherComponent);
frame.pack(); // now the frame width/height is greater than 0.
frame.setLocationRelativeTo( null );
frame.setVisible( true );

Related

Java swing scrollpane doesn't show the scroll bar

I was working on a java swing gui project for my course. When I was doing that, I found that I had too much information in a panel and it was not able to show everything. As a result, I wanted to add a Jscrollpane and I did some research about how to use this function but it didn't seem to work for my project, and I had no reason why even after I tried almost everything I could find on google.
Here is my code:
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(10, 1100));
musiclist.setBounds(0, 0, 190, 1000);
musiclist.setLayout(null);
listpanel.setLayout(null);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
listpanel.add(selectButton);
c.add(musiclist, BorderLayout.WEST);
frame.setVisible(true);
musiclist.setVisible(true);
The problem now is that the scroll bar never shows up even I set the vertical to always show. I am just new to java, so any help will be helpful and thank you all for your time!
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(100, 1100));
musiclist.setBounds(0, 0, 190, 1000);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
JList mlist=new JList();
DefaultListModel lmodel=new DefaultListModel();
lmodel.addElement("Song 1");
lmodel.addElement("Song 2");
lmodel.addElement("Song 3");
mlist.setModel(lmodel);
listpanel.setLayout(new BorderLayout());
listpanel.add(mlist, BorderLayout.CENTER);
listpanel.add(selectButton, BorderLayout.SOUTH);
c.add(musiclist, BorderLayout.WEST);
musiclist.setVisible(true);
frame.setVisible(true);

JPanel not displaying JLabel

I am trying to make an application, which displays a JLabel and a button, which if clicked switches to another jPanel. For some reason my JLabel is not displaying at all in either case. I would appreciate an expert eye to look over my code and see what I am doing wrong. Thanks in advance.
HomeScreenUI(){
//frame
JFrame frame = new JFrame("Opisa");
//panels, one before button click and one after
JPanel panel = new JPanel();
JPanel panelAfterButtonClick = new JPanel();
panel.setLayout(null);
panelAfterButtonClick.setLayout(null);
//jlabel that isnt displaying + dimensions
JLabel label = new JLabel("Opisa");
Dimension size = label.getPreferredSize();
label.setBounds(100, 100, size.width, size.height);
label.setFont(new Font("Helvetica", Font.PLAIN, 70));
//second jlabel that isn't displaying
JLabel label2 = new JLabel("Opisa");
Dimension size4 = label2.getPreferredSize();
label2.setBounds(100, 100, size4.width, size4.height);
label2.setFont(new Font("Helvetica", Font.PLAIN, 70));
//adding the labels to the panels
panel.add(label);
panelAfterButtonClick.add(label2);
//button that is displaying both before and after
JButton button = new JButton("Click Me..");
JButton buttonAfterClick = new JButton("Clicked Me..");
//dimensions
Dimension size2 = button.getPreferredSize();
button.setBounds(100, 100, size2.width, size2.height);
Dimension size3 = button.getPreferredSize();
buttonAfterClick.setBounds(100, 100, size3.width, size3.height);
//adding the buttons to the jpanel
panel.add(button);
panelAfterButtonClick.add(buttonAfterClick);
//function that changes the panel after the button is clicked
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
frame.setContentPane(panelAfterButtonClick);
frame.invalidate();
frame.validate();
}
});
//adding the panel to the frame and setting the size
frame.add(panel);
frame.setSize(1000,800);
frame.setVisible(true);
}
Check the label bounds, more specifically its size... try setting some fixed values (great enough to show its content like setBounds(100, 100, 250, 80)).
The preferred size is being retrieved before changing the font size, so it is not big enough to show that big characters. Try changing the font first.

Why cant I change color of my JFRame background when I use setLayout()?

I try to set background color to red for JFrame while keeping white color to JPanel.
But it doesnt work with setLayout somehow
private void buildGraphics(){
JFrame frame = new JFrame();
setTitle("Application");
setBounds(100, 100, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
frame.setBackground(Color.red);
JPanel panel = new JPanel();
panel.setBounds(50, 50, 500, 70);
panel.setBackground(Color.white);
panel.setBorder(BorderFactory.createTitledBorder("Click to choose..."));
panel.add(button1);
panel.add(button2);
panel.add(button3);
getContentPane().add(panel);
}
You already have a frame (your class is extending JFrame, so it IS a JFrame) and buildGraphics() has to build the existing frame you don't have to create a new one:
private void buildGraphics() {
this.setTitle("Application");
this.setBounds(100, 100, 600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
this.setBackground(Color.red);
JPanel panel = new JPanel();
panel.setBounds(50, 50, 500, 70);
panel.setBackground(Color.white);
panel.setBorder(BorderFactory.createTitledBorder("Click to choose..."));
panel.add(button1);
panel.add(button2);
panel.add(button3);
this.getContentPane().add(panel);
}
Ok, I now created another JPanel and put my first one inside to control background color
JFrame frame = new JFrame();
setTitle(" Application");
setBounds(100, 100, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JPanel panelMain = new JPanel();
panelMain.setBounds(5, 5, 595, 395);
panelMain.setBackground(Color.red);
getContentPane().add(panelMain);
JPanel panel = new JPanel();
panel.setBounds(50, 50, 500, 70);
panel.setBackground(Color.white);
panel.setBorder(BorderFactory.createTitledBorder("What would you like to do?"));
panel.add(button1);
panel.add(button2);
panel.add(button3);
panelMain.add(panel);

MigLayout: Only window displayed when trying fullscreen

Here is code:
ScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height,
ScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
JFrame MainFrame = new JFrame();
MainFrame.setSize(ScreenWidth, ScreenHeight);
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
MainFrame.setVisible(true);
/* When set to false, all buttons and boxes are displayed,
otherwise only the main window appears */
MainFrame.setUndecorated(true);
Container Pane = Frame.getContentPane();
Pane.setLayout(new MigLayout());
initLoginPanel(Pane);
The function that lays out the controls:
private void initLoginPanel(Container Obj)
{
JPanel LoginContainer = new JPanel();
LoginContainer.setLayout(new MigLayout());
Obj.add(LoginContainer, "pos 0.5al 0.5al");
JLabel uNameLabel = new JLabel("Username");
JTextField uNameBox = new JTextField();
JLabel uPassLabel = new JLabel("Password");
JTextField uPassBox = new JTextField();
JButton LoginButton = new JButton("Login", 90, 26);
LoginContainer.add(uNameLabel, "wrap");
LoginContainer.add(uNameBox, "span");
LoginContainer.add(uPassLabel, "wrap");
LoginContainer.add(uPassBox, "span");
LoginContainer.add(LoginButton, "");
}
If, in the above code, MainFrame.setUndecorated(false) is used, it works fine but no full screen. That is the title bar, close, minimize and maximize buttons are displayed.
Question:
1. How can I get the components working in fullscreen mode.
There are two problems:
The position of setVisible
Showing the frame must be the last step;
first you must setup your frame and add his content.
The call to initLoginPanel
Your code is doing incorrect things. Why don't you add the components directly to the frame? i.e.
initLoginPanel( YourJFrame );
Fixed, simplified code:
JFrame frame = new JFrame("Main window");
frame.setSize( Toolkit.getDefaultToolkit().getScreenSize() );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(false);
initLoginPanel(frame);
frame.setVisible(true); //FINALLY show the JFrame!

How to move components in a jframe to a certain spot

so im having trouble moving components to a certain part of the jframe, ive tried using BorderLayout and setBounds() but both of those don't seem to work.
// sets the start button
start = new JButton(imgStart);
start.setPreferredSize(new Dimension(150, 55));
start.setFocusable(false);
start.addActionListener(this);
// sets the controls button
controls = new JButton(imgControl);
controls.setPreferredSize(new Dimension(150, 55));
controls.setFocusable(false);
controls.addActionListener(this);
controls.setBounds(151, 56, 0, 0);
JFrame frame = new JFrame();
frame.setContentPane(this);
frame.add(start);
frame.add(controls, BorderLayout.WEST);
frame.setTitle("Freedom Planet");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(this);
frame.setResizable(false);
frame.setSize(imgBackground.getIconWidth(), imgBackground.getIconHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
I also have the some problem with a panel im using in a different part of my program
// creates a panel called Info and and adds components to them
JPanel Info = new JPanel();
//Info.setBorder(BorderFactory.createLineBorder(Color.black, 5));
Info.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 10));
//Info.setBounds(290, 180, -10, 10);
Info.setOpaque(false);
Info.add(lblTime);
Info.add(lblTimer);
Info.add(lblScore);
Info.add(lblShowScore);
addKeyListener(this);
setFocusable(true);
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setContentPane(this);
frame.add(Info, BorderLayout.SOUTH);
frame.add(lblLevel);
frame.setTitle("Freedom Planet");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(this);
frame.setResizable(false);
frame.setSize(imgBackground[0].getIconWidth(), imgBackground[0].getIconHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
I'm trying to move the components horizontally
Then maybe you can use a Swing Border. An EmptyBorder will allow you to add extra space around the components.

Categories

Resources