I am attempting to make a Chess game and while trying to work on the GUI, I encountered this issue:
I cannot seem to be able to vertically center my chess board on my JFrame. The JPanel is horizontally centered, but it is off-center, stuck to the top, vertically.
Code where the panel using GridLayout is added to its container and the frame is initialized:
public class ChessGUI extends JFrame
{
private static final long serialVersionUID = 1L;
private static Dimension appDimention = new Dimension(1000, 600);
public static JFrame frame = new JFrame("Chess");
public static JPanel background = new JPanel();
public static BoardGUI board = new BoardGUI();
public static int width;
public static int height;
public static void createFrame()
{
JFrame.setDefaultLookAndFeelDecorated(true);
//Set stuff that JFrame needs
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
//Set stuff that JPanel needs
background.setPreferredSize(appDimention);
frame.getContentPane().add(background);
frame.pack();
//This 'board' is my Chess Board JPanel which I can't seem to centre
//'background' is a JPanel which is, as the name suggests, the background
background.add(board);
//Set the location of the JFrame and set it visible
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
BoardGUI
#SuppressWarnings("serial")
public class BoardGUI extends JPanel
{
GridLayout chessBoard = new GridLayout(8, 8);
Dimension boardDims = new Dimension(500, 500);
public BoardGUI()
{
this.setLayout(chessBoard);
this.setBackground(Color.BLACK);
this.setPreferredSize(boardDims);
}
}
I'm not really doing anything in the code above to center the BoardGUI object, but I did tried the following two ways with negative results:
background.add(board, JPanel.CENTER_ALLIGNMENT)
background.add(board, BorderLayout.CENTER)
The result I'm getting at the moment:
As you can see it is not vertically centered, and my desired behavior is for it to be both horizontally and vertically centered on the frame.
Any help or insights on any mistakes I might be making would be very welcome! Thanks!
background is a JPanel which has a default layout of FlowLayout, which is where your problem is coming from.
I would change
public static JPanel background = new JPanel();
to
public static JPanel background = new JPanel(new GridBagLayout());
Suggestions...
Okay, so suggestions.
Avoid static, especially if all you want to do is access information from one class in another - there are better ways to achieve this that won't tightly couple your code
Avoid setPreferredSize - it's not a recommend way of defining custom sizing hints, override getPreferredSize instead, this prevents other people from changing it.
Instead of setting the preferredSize of the background panel, I would simple make use of either an EmptyBorder or the margins/inserts support of GridBagLayout
Related
I tried using BoxLayout because that's the layout that supports the centered vertical stacking of panels that I'm trying to achieve.
The problem is that when I don't set the maximum size, the panel just fills the entire parent, and when I do set it, well, it can't be bigger than the size set.
I want the size of the panels inside the BoxLayout to be dependent on the parent panel's size, and to keep their aspect ratio. Something like this example that I made.
How can I do that?
This is my code, it's really simple.
import java.awt.*;
import javax.swing.*;
public class PrezentacijaTab extends JPanel {
private final JPanel panelZaLabel;
private final JPanel panelZaSlajdove=new JPanel();
private final JScrollPane scrollPaneZaSlajdove;
public PrezentacijaTab(String labelText) {
setLayout(new BorderLayout());
panelZaLabel = new JPanel(new BorderLayout());
panelZaLabel.add(new JLabel(" "+labelText),BorderLayout.WEST);
add(panelZaLabel, BorderLayout.NORTH);
panelZaSlajdove.setLayout(new BoxLayout(panelZaSlajdove,BoxLayout.Y_AXIS));
scrollPaneZaSlajdove=new JScrollPane(panelZaSlajdove);
this.add(scrollPaneZaSlajdove,BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.add(new PrezentacijaTab("Label 1"));
frame.pack();
frame.setVisible(true);
}
}
panelZaSLajdove is where I'm storing all of my new JPanels.
User should be able to add as many panels as he wants. Think of it like PowerPoint. The panels added are just like slides.
When I run this program it appears as an empty window until you fullscreen, then it can be resized as you like, why is it doing this/how do I stop it?
the program is very basic just a menubar and two panels split.
public class SplitPane {
public static void main(String[] args) {
window view = new window();
}
private static class window extends JFrame {
public window() {
this.setSize(1000, 750);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//menubar is here, must lower code quantity for stack
//panels
//graph half
JPanel graphRep = new JPanel();
//Background colour - graphRep.setBackground(Color.RED);
graphRep.setVisible(true);
String graphTitle = "Textual Representation.";
Border graphBorder = BorderFactory.createTitledBorder(graphTitle);
graphRep.setBorder(graphBorder);
//text half
JPanel textRep = new JPanel();
textRep.setVisible(true);
String textTitle = "Graphical Representation.";
Border textBorder = BorderFactory.createTitledBorder(textTitle);
textRep.setBorder(textBorder);
//splitpane
JSplitPane splitPane = new JSplitPane();
splitPane.setSize(600, 750);
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerSize(10);
splitPane.setDividerLocation(250);
splitPane.setLeftComponent(graphRep);
splitPane.setRightComponent(textRep);
this.add(splitPane);
}
}
this.setVisible(true);
You are making the frame visible BEFORE you add components to the frame. The layout manager is never invoked so the size of all the components remains (0, 0) so there is nothing to paint.
The frame should be made visible AFTER all the components have been added to the frame.
And the code should be:
frame.pack();
frame.setVisible();
So each component is displayed at its proper size. Don't hardcode the size() because you don't know what the size of a users screen might be.
I am working on a Java desktop application. It uses MySQL database to store all data etc. I use swing for the GUI.
The GUI of this application is layed out as follows:
Main Window taking the entire screen size (with image in the
background)
Internal Window 800 x 600 centered within the Main
Window (that holds current content that can be switched between
using menu and/or event within the application.
LoginPanel.java:
import javax.swing.*;
import java.awt.*;
public class LoginPanel {
private JPanel loginPanel;
public void loginForm()
{
JButton loginSubmit = new JButton("Login");
loginPanel = new JPanel();
loginPanel.add(loginSubmit);
loginPanel.setSize(800, 600);
}
public JComponent getGUI()
{
return loginPanel;
}
public static void main(String[] args)
{
}
}
Main.java:
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args)
{
JFrame mainFrame;
mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setTitle("Caledonian Library System");
LoginPanel loginObj = new LoginPanel();
mainFrame.add(loginObj.getGUI());
mainFrame.pack();
mainFrame.setVisible(true);
}
}
Should I maybe use box layout? any suggestions?
Ok, I have just run a test program, and have achieved the result you are looking for. I have used a GridBagLayout which defaults to centre into the Container it is added to. It will not show up with the borders or other buttons built into a JFrame (though you can add a border if you wish later).
JFrame mainframe = new JFrame();
JPanel mainPanel = new JPanel();
GridBagLayout gridLayout = new GridBagLayout();
mainPanel.setLayout(gridLayout);
//GridBagConstraints allow you to set various features of the way the components appear
//in the grid. You can set this up as you wish, but defaults are fine for this example
GridBagConstraints gridConstraints = new GridBagConstraints();
//Just using FlowLayout as a test for now
JPanel centerPanel = new JPanel(new FlowLayout());
centerPanel.add(new JLabel("Hello"));
centerPanel.add(new JLabel("Centered"));
mainPanel.add(centerPanel, gridConstraints);
mainFrame.add(mainPanel);
If you found that the space around the side of your centered panel wasn't being used, and you wanted it to be use, you could try nesting mainPanel inside another panel that is using a BorderLayout, making sure that it is in BorderLayout.CENTER.
In the example I didn't bother changing GridBagConstraints from the default, as it was ok for this demonstration. However you can edit it as you wish, and then apply to each component you add to the GridBagLayout, making sure to include the GridBagConstraints object in each mainPanel.add(). Check the GridBagLayout tutorials for some good information.
Of course, if you would like more components in the centre other than the main window, you can then simply add them to the mainPanel (making sure to change the position in the GridLayout). There are going to be numerous ways of achieving what you want, but it really depends on what you feel looks good. The Layout Managers will do all of the resizing work for you.
I've been having trouble recreating this GUI:
We've been told to use the BorderLayout with grids inside each section. I've been trying to head the header to work (the top square of the GUI with the class name and person name), but I can't seem to get anything to show up. This is what I have so far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Display extends JFrame implements ActionListener {
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 350;
private static final int FRAME_X_ORIGIN = 100;
private static final int FRAME_Y_ORIGIN = 75;
public static void main(String[] args) {
Display frame = new Display();
frame.setVisible(true);
}
public Display() {
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setLayout(null);
setTitle("CSCE155A Course Offering Viewer");
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// header
JPanel header = new JPanel();
header.setLayout(new GridLayout(2, 1));
header.setSize(380, 50);
header.setLocation(0, 0);
header.setBorder(BorderFactory.createLineBorder(Color.BLACK));
header.add(new JLabel("CSCE155A Course Offering Viewer"));
header.add(new JLabel("First Last"));
}
public void actionPerformed(ActionEvent event) {
}
}
The only thing that shows up is the window with nothing inside it.
We've been told to use the BorderLayout with grids inside each section
setLayout(null);
So why are you using a null layout on the frame?
Where do you add the panel to the frame?
You where given a link yesterday in your question: JPanels and GridLayouts to the Swing tutorial on How to Use a Border Layout. You where also given example code that showed you how to add the panel to the frame.
Read the tutorial, download the working example and then customize the example for your needs.
Don't keep repeating questions in the forum when you don't listen to previous advice!
I think you need to create a container-object where you can put your panels. Here I put two panels within a BorderLayout
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputPanel, BorderLayout.EAST);
contentPane.add(rightPanel, BorderLayout.CENTER);
I am trying to place a JPanel on top of another JPanel which contains a JTextArea and a button and i want to the upper apnel to be transparent. I have tried it by making the setOpaque(false) of the upper panel. but it is not working. Can anyone help me to get through this? Thanks in advance!
public class JpanelTest extends JPanel
{
public JpanelTest()
{
super();
onInit();
}
private void onInit()
{
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JTextArea(100,100),BorderLayout.CENTER);
panel.add(new JButton("submit"),BorderLayout.SOUTH);
JPanel glass = new JPanel();
glass.setOpaque(false);
add(panel,BorderLayout.CENTER);
add(glass,BorderLayout.CENTER);
setVisible(true);
}
public static void main(String args[])
{
new JpanelTest();
}
}
Indeed, it would be useful to tell the reason why you want panels one over another.
Starting with your code, and changing it a lot, I got it to work, but it might not do what you expect...
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
public Test()
{
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 200);
onInit();
setVisible(true);
}
private void onInit()
{
JLayeredPane lp = getLayeredPane();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JTextArea(), BorderLayout.CENTER);
panel.add(new JButton("Submit"), BorderLayout.SOUTH);
panel.setSize(300, 150); // Size is needed here, as there is no layout in lp
JPanel glass = new JPanel();
glass.setOpaque(false); // Set to true to see it
glass.setBackground(Color.GREEN);
glass.setSize(300, 150);
glass.setLocation(10, 10);
lp.add(panel, Integer.valueOf(1));
lp.add(glass, Integer.valueOf(2));
}
public static void main(String args[])
{
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Test();
}
});
}
}
If totally transparent, well, it is like it isn't here! When opaque, it just covers some of the GUI, but doesn't prevent mouse clicks, for example.
1) there are a few ways, there no issue to put JPanel, with covering full JFrames/JPanel area or only part of Rectangle / Dimension that returns JFrames/JPanel
use JLayer(Java7) based on JXLayer (Java6)
use GlassPane
use JViewport
use OverlayLayout
use transucent JDialog / JWindow
2) everything depends of if you want to protect against mouse and key events from the top layer to bottom, or not (to avoiding redispatch events from - to and vice versa)
Check out this tutorial on using Swing Root Panes.
The glass pane is useful when you want to be able to catch events or paint over an area that already contains one or more components. For example, you can deactivate mouse events for a multi-component region by having the glass pane intercept the events. Or you can display an image over multiple components using the glass pane.