I am trying to use JFrame and BoxLayout to achieve a GUI similar to the one shown, but I am not sure how to center my Stop and Play buttons. Any suggestions?
Here is my code:
JFrame frame = new JFrame();
Box box = Box.createHorizontalBox();
box = Box.createHorizontalBox();
box.add(new JButton("Play"));
box.add(new JButton("Stop"));
box.add(Box.createHorizontalGlue());
frame.add(box, BorderLayout.SOUTH);
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
I have not yet coded in the text box and load button as I haven't yet been able to figure out centering.
Create a seperate panel for buttons. With horizontal glue you can center your buttons.
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(new JButton("Play"));
buttonPanel.add(new JButton("Stop"));
buttonPanel.add(Box.createHorizontalGlue());
frame.add(buttonPanel, BorderLayout.SOUTH);
Also you can do that with FlowLayout easily
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
buttonPanel.add(new JButton("Play"));
buttonPanel.add(new JButton("Stop"));
frame.add(buttonPanel, BorderLayout.SOUTH);
Related
I am trying to set the sizes of my JButtons in a JPanel with BoxLayout correctly, but the behavior is beyond weird.
It will take the height from JButton.setPreferredSize, but completely ignore the width. This also only works when all buttons are set to the same height. As soon as one is smaller, it will revert all of them to some random minimum size (which isn't even the same for all buttons)
My code is this:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500);
JPanel rightPanel = new JPanel();
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
JButton bBookmarks = new JButton("Bookmarks");
bBookmarks.setPreferredSize(new Dimension(200, 100));
//more buttons with same size
leftPanel.add(bBookmarks);
//more buttons
JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
mainPanel.setDividerLocation(200);
frame.add(mainPanel);
frame.setResizable(false);
frame.setVisible(true);
This creates this image.
The middle button is always wider than the rest as well. Using frame.pack() doesn't do anything except resizing the frame because the right panel is empty.
What am I doing wrong?
Edit: Should look like this:
Divide and conquer: break the design into small, easy to layout containers. In this case do not place the buttons directly in the left (BoxLayout) container but in a nested JPanel using GridLayout manager.
This ensures that all buttons have the same size:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//add all buttons to a panel using a GridLayout which shows all components having the same size
JPanel buttons = new JPanel(new GridLayout(0,1));
JButton bBookmarks = new JButton("Bookmarks"); buttons.add(bBookmarks);
JButton bPlotter = new JButton("Plotter"); buttons.add(bPlotter);
JButton bShips = new JButton("Ships"); buttons.add(bShips);
//add buttons and text area to a panel using BoxLayout
JPanel leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(100,400));
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
leftPanel.add(buttons);
leftPanel.add(new TextArea(10,30));
JPanel rightPanel = new JPanel();
rightPanel.setPreferredSize(new Dimension(600,400));
rightPanel.add(new JLabel("right pane"));
JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, leftPanel, rightPanel);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
I can't to fix some problem:
I have a program with JPanel that uses BoxLayout with alignment by X_AXIS and inside 2 JLabel (setAignmentX(Component.CENTER_ALIGNMENT)), 2 JTextArea and 1 JButton(setAlignmentX(Component.CENTER_ALIGNMENT)).
when I press button and button changes text's button changes alignment labels and button too(not size only). If it making often it's very nervous for eyes. setHorizontalAlignment and SwingConstants does not work.
bit GUI design:
frame = new JFrame("TestIOapp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBackground(Color.decode("#e2dad4"));
JLabel qLabel = new JLabel("Question");
JLabel aLabel = new JLabel("Answer");
qLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
aLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
button = new JButton("Next");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
button.setPreferredSize(new Dimension(100,40));
button.addActionListener(new NextButtonListener());
question = new JTextArea(10,20);
question.setWrapStyleWord(true);
question.setLineWrap(true);
answer = new JTextArea(10,20);
answer.setWrapStyleWord(true);
answer.setLineWrap(true);
JScrollPane qScroll = new JScrollPane(question);
qScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
qScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JScrollPane aScroll = new JScrollPane(answer);
aScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
aScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(qLabel);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(qScroll);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(aLabel);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(aScroll);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(button);
panel.add(Box.createRigidArea(new Dimension(0,30)));
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.setVisible(true);
frame.setSize(500,500);
i am trying to make screen as in the picture using swing.
My code snipet
Label l1,l2,l3;
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
l1=new Label("Employee:);
l2=new Label("Earning");
l3=new Label("Deductions");
p1.setLayout(new GridLayout(1,1));
p1.add(l1);
p2.setLayout(new GridLayout(1,1));
p2.add(l2);
p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK)));
p2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK)));
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.add(p1);
container.add(p2);
container.add(p3);
add(container,BorderLayout.NORTH);
setVisible(true);
setSize(600,600);
with Y_AXIS it doesn't allow me to have 2 panel side by side.
how to have panels both horizontally and vertically
I am not sure how I could create a panel like this. I could have the main panel as a borderlayout and set the login screen panel to the page_end but then the forums and faqs also have to be on the page_end..... somehow the login screen panel and the forums and faqs panel has to share the page_end together. Is there someway I could do this or maybe some BETTER way? This has been confusing me for about 2 hours and I don't understand how I would do this.
Right now I have 3 panels and 1 frame. 1 is the main panel that is added to the main frame. The 2 other panels are the loginscreen panel and the forums and faqs panel. Here is the code.
private void createView() {
//Created essential details for the frame
JFrame frame = new JFrame();
frame.setTitle("Name of the game");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Defining layouts and panels + giving them layouts
JPanel pMain = new JPanel();
frame.getContentPane().add(pMain);
pMain.setLayout(new BorderLayout());
JPanel pLogin = new JPanel(new GridBagLayout());
pMain.add(pLogin, BorderLayout.PAGE_END);
JPanel pInfo = new JPanel(new GridBagLayout());
pMain.add(pInfo, BorderLayout.PAGE_END);
frame.setVisible(true);
}
Here is the component layout
Source
JFrame frame = new JFrame();
frame.setTitle("Name of the game");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Defining layouts and panels + giving them layouts
JPanel pMain = new JPanel();
frame.getContentPane().add(pMain);
pMain.setLayout(new BorderLayout());
JPanel bottomComponentsPanel = new JPanel(new GridBagLayout());
JPanel pLogin = new JPanel();
pLogin.setBackground(Color.ORANGE);
pLogin.setPreferredSize(new Dimension(100, 100));
JPanel pInfo = new JPanel(new GridBagLayout());
pInfo.setBackground(Color.ORANGE);
pInfo.setPreferredSize(new Dimension(70, 70));
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.PAGE_END;
constraints.gridx = 0;
constraints.gridy = 0;
bottomComponentsPanel.add(pLogin, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
bottomComponentsPanel.add(pInfo, constraints);
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomPanel.add(bottomComponentsPanel);
pMain.add(bottomPanel, BorderLayout.SOUTH);
frame.setVisible(true);
Display
I am trying to design a layout which contains a form and couple of items. but I found it too hard to put items in right places.
In the following image, the right frame is what I am aiming to design and the left on is what I could made.
And this is the code for the right frame:
public class GUI extends JFrame{
public GUI(){
JFrame frame = new JFrame("frame");
frame.setSize(600, 600);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Title"), BorderLayout.NORTH);
JPanel formPanel = new JPanel(new GridLayout(1,2));
panel.add(formPanel);
TitledBorder formPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
formPanel.setBorder(formPanelTitle);
//LEFT PANEL
JPanel labelsPanel = new JPanel(new GridLayout(4,1));
TitledBorder labelsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
labelsPanel.setBorder(labelsPanelTitle);
labelsPanel.add(new JLabel("Label 1"));
labelsPanel.add(new JLabel("Label 2"));
labelsPanel.add(new JLabel("Label 3"));
labelsPanel.add(new JLabel("Label 4"));
formPanel.add(labelsPanel);
//RIGHT PANEL
JPanel fieldsPanel = new JPanel(new GridLayout(4,1));
TitledBorder fieldsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
fieldsPanel.setBorder(fieldsPanelTitle);
fieldsPanel.add(new JTextField("Label 1"));
fieldsPanel.add(new JTextField("Label 2"));
fieldsPanel.add(new JTextField("Label 3"));
fieldsPanel.add(new JTextField("Label 4"));
formPanel.add(fieldsPanel);
//BOTTOM PANEL
JPanel bottomPanel = new JPanel(new GridLayout(2,1));
TitledBorder BottomPanelTitle = BorderFactory.createTitledBorder("GridLayout(2,1)");
bottomPanel.setBorder(BottomPanelTitle);
panel.add(bottomPanel, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(new JButton("Browse"));
buttonPanel.add(new JLabel("Label"));
TitledBorder buttonPanelTitle = BorderFactory.createTitledBorder("FlowLayout()");
buttonPanel.setBorder(buttonPanelTitle);
bottomPanel.add(buttonPanel);
JPanel secondButtonPanel = new JPanel(new GridLayout(1,2));
secondButtonPanel.add(new JButton("Back"));
secondButtonPanel.add(new JButton("Next"));
TitledBorder secondButtonPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
secondButtonPanel.setBorder(secondButtonPanelTitle);
bottomPanel.add(secondButtonPanel);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
new GUI();
}
}
I am not sure if the code is really optimal, since there are a lot of inner panels and made it too complicated. Also I could not put items in the places I wanted to. Is there any suggestion or idea to make this layout look better?
Create a JPanel, using GridBagLayout and add your labels/fields to it, this forms the "center" portion of your layout.
Create a JPanel and add the Browse button a JLabel to it. Using GridBagConstraints#gridwidth set to REMAINDER, add this to your first panel
Create a JPanel, using BorderLayout, add the first panel to the CENTER position. Add the title Label to the NORTH position, you may need to adjust it's horizontalAlignment property
Create a JPanel using FlowLayout, aligned to the RIGHT and add your "Back" and "Next" buttons to it. Add this to the SOUTH position of the previous panel.
Check out Laying Out Components Within a Container for more details