This is very basic code but i just need some help to understand..
i have a button and i want to place it on the EAST side of the Panel.. any suggestions..
public class ButtonText {
public static void main(String[] args) {
Frame frame=new Frame("Button Frame");
Button button = new Button("Submit");
frame.setLayout(new FlowLayout());
frame.add(button, BorderLayout.EAST);
frame.setSize(200,100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
which looks like this http://imgur.com/0GYso
any help would be greatly appreciated thank you!
To lay your user interface out with a border layout manager, you need to set your frame's layout to an instance of BorderLayout. A default FlowLayout sets components out from left to right and onto next lines if necessary.
frame.setLayout(new BorderLayout());
frame.add(button, BorderLayout.EAST);
You can use a Layout that positions the elements in the frame. Take a look here. You are using a FlowLayout, but you need to understand each layout and the rules positioning elements.
FlowLayout cant use BorderLayout constraints...
Try using BorderLayoutinstead of FlowLayout and it should work just fine.
Flowlayout doesn't let you do that, you should set the
frame.setLayout(new BorderLayout());
frame.add(button, BorderLayout.EAST);
or
frame.setLayout(new BorderLayout());
frame.add(button, BorderLayout.LINE_START);
to make it work,
The Flowlayout only positions things like a horizontal stack.
you may want to try something like this
Frame frame=new Frame("Button Frame");
Button button = new Button("Submit");
HorizontalPanel hp = new HorizontalPanel();
hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
hp.setWidth(200);
hp.add(button);
frame.add(hp);
frame.setSize(200,100);
frame.setVisible(true);
Related
I am confused about how JFrame extends the JButton vertically to match JFrame min size but not horizontally. I would like for it to not extend in either direction and know why it extends or doesn't.
import javax.swing.*;
import java.awt.*;
public class somestring {
public static void main(String []args){
JFrame frame = new JFrame();
Container content = frame.getContentPane();
content.setLayout(new BorderLayout());
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension max = toolkit.getScreenSize();
frame.setMinimumSize(new Dimension(1000,1000));
frame.setSize(max);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton s = new JButton("generate somestring");
s.setPreferredSize(new Dimension(400,400));
content.add(s, BorderLayout.WEST);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
}
I am confused about how JFrame extends the JButton vertically to match JFrame min size but not horizontally.
content.setLayout(new BorderLayout());
...
content.add(s, BorderLayout.WEST);
The default layout manager of the content pane of the frame is the BorderLayout, so the above is unnecessary.
In any case, the reason it extend vertically is because that is the rule of the BorderLayout when you add a component to the WEST.
Read the section from the Swing tutorial on Layout Managers. The section on the BorderLayout will explain the rules for adding components to the WEST.
I would like for it to not extend in either direction
Then you need to use a different layout manager.
For example you could use a FlowLayout. The button will be centered at the top of the frame.
Or you could use a GridBagLayout (with the default GridBagConstraints). The button will be centered both horizontally and vertically.
frame.pack();
frame.setResizable(false);
You should set the resizable property BEFORE invoking pack().
frame.setMinimumSize(new Dimension(1000,1000));
frame.setSize(max);
Not sure why you are setting the minimum size and invoking setSize(). If you want the button to be displayed at its preferred size, then just invoke pack().
I am working on a GUI in Java and I have the following code:
public class MainWindow extends JFrame {
public MainWindow () {
setUpWindow();
JPanel upperPanel = new JPanel();
JPanel lowerPanel = new JPanel();
upperPanel.setBorder(new LineBorder(Color.GRAY, 1));
lowerPanel.setBorder(new LineBorder(Color.GRAY, 1));
getContentPane().add(upperPanel, BorderLayout.NORTH);
getContentPane().add(lowerPanel, BorderLayout.SOUTH);
}
private void setUpWindow () {
setSize(600, 450);
setTitle("Subjects");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(new BorderLayout());
getRootPane().setBorder(BorderFactory.createMatteBorder(4,4,4,4,Color.LIGHT_GRAY));
setVisible(true);
}
}
If I compile this, the output I get is:
As you can see, I have one JPanel at NORHT and another one at SOUTH.
I want to add text in the border of a JPanel so I can get as an output something similar as this:
I have tried multiple things but I don't know how. Is there a JPanel method that does this? Should I create a JLabel and place it there somehow?
Thanks in advance.
What you're looking for is the BorderFactory. It can be applied like so:
upperPanel.setBorder(BorderFactory.createTitledBorder("Favorite subjects"));
For a detailed explanation of JPanels, Borders and what's possible (with examples), see the Oracle article How to Use Borders.
I am trying to make a GUI for a game. I am very new to Java, especially GUI. The code below is a snippet which is supposed to make a JFrame with nested panels for organization. It works until I add buttons to the button panel. They end up on the boardBckg panel. If I manage to place them on the correct panel the JTextField disappears or it takes up the entire screen. I have been working on this part of the code for the past two days and I could really use GUI tips.
private void makeWindow()
{
boardPanel = new JPanel();
boardBckg = new JPanel();
menuPanel = new JPanel();
save = new JButton("Save");
save.setSize(Buttons);
load = new JButton("Load");
load.setSize(Buttons);
replay = new JButton ("Replay");
replay.setSize(Buttons);
words = new JTextField();
frame = new JFrame(title);
boardPanel.setSize(PANEL);
boardPanel.setMaximumSize(MAX);
boardPanel.setMinimumSize(MIN);
boardPanel.setLayout(new GridLayout(m,n));
boardBckg.setSize(1000, 1000);
boardBckg.setBackground(Color.cyan);
boardBckg.add(boardPanel, BorderLayout.CENTER);
frame.setSize(1500, 1000);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoxLayout vertical = new BoxLayout(menuPanel, BoxLayout.Y_AXIS);
menuPanel.setSize(500, 1000);
menuPanel.setBackground(Color.blue);
menuPanel.setLayout(vertical);
frame.add(boardBckg);
frame.add(menuPanel);
JPanel iGiveUp = new JPanel();
iGiveUp.setBackground(Color.black);
JPanel buttons = new JPanel();
buttons.setBackground(Color.darkGray);
buttons.add(save);
buttons.add(load);
buttons.add(replay);
menuPanel.add(iGiveUp);
menuPanel.add(buttons);
iGiveUp.add(words);
boardBckg.add(boardPanel, BorderLayout.CENTER);
The default layout of a JPanel is the FlowLayout. You can't just specify a BorderLayout constraint when you add the component to the panel.
frame.add(boardBckg);
frame.add(menuPanel);
The default layout for (the content pane of) the frame is a BorderLayout. If you don't specify a constraint, then the component is added to the BorderLayout.CENTER. Problem is only one component can be added to the CENTER so you only see the last comoponent added.
frame.setVisible(true);
Component should be added to the frame BEFORE the frame is packed and made visible. So the above statement should be the last statement in your constructor.
I have no ideas what your desired layout is but you need to start with something simple and take advantage of the default BorderLayout of the frame.
So your basic logic might be something like:
JPanel menuPanel = new JPanel()
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));
menuPanel.add(...);
menuPanel.add(...);
JPanel center = new JPanel();
center.setLayout(...);
center.setBackground( Color.BLUE );
center.add(...);
frame.add(menuPanel, BorderLayout.LINE_START);
frame.add(center, BorderLayout.CENTER);
frame.pack();
frame.setVisible( true );
The main point is to break the panels down logically and add them to the frame one at a time. So first get the menu and its child components added to the frame is the correct position. Then you can add the CENTER panel and its child components.
I have problem with this. The button is taking up the entire JFrame. I've tried changing the dimensions of the JFrame and the JButton but with no changed. It's completely hiding a JTable underneath. Could someone please tell me what I'm doing wrong.
JFrame FRAME = new JFrame();
JButton BUTTON = new JButton("OK");
FRAME.add(new JScrollPane(TableName));
BUTTON.setPreferredSize(new Dimension(20,30));
FRAME.add(BUTTON);
FRAME.setSize(700, 600);
FRAME.setVisible(true);
FRAME.setLocationRelativeTo(null);
The default layout manager for a a JFrame is a BorderLayout. You are attempting to add two component the CENTER which is not allowed. Only the last one added will be displayed.
You need to specify constraints when adding components to a BorderLayout. Your code should be something like:
frame.add(new JScrollPane(TableName), BorderLayout.CENTER);
button.setPreferredSize(new Dimension(20,30));
frame.add(button, BorderLayout.PAGE_START);
Also, variables names should NOT be upper cased. Follow Java convention.
Read the section from the Swing tutorial on How to Use BorderLayout for more information and working examples. The tutorial code will also show you how to better structure your program so you follow Swing coding conventions.
Don't add the button straight to the frame, create a JPanel first, add the button to the panel then add the panel to the frame.
JFrame frame = new JFrame();
// frame.setBounds(x axis, y axis, weight, height)
frame.setBounds(10,10,304,214);
frame.getContentPane().setLayout(null);
JButton button = new JButton("Button");
button.setBounds(98, 75, 126, 39);
frame.getContentPane().add(button);
frame.setVisible(true);
Is it possible to have some extra space around the edges of a JFrame that uses AbsoluteLayout? When I have a button as the downwardsmost component on the JFrame, it gets positioned right up against the bottom edge of the JFrame window, and it looks bad. I would like to know if there's a way to add a little extra space between components and the edge of the JFrame while using AbsoluteLayout.
Suggestions:
When you add a component to a JFrame, you're actually adding it to the JFrame's contentPane. To give the contentPane a "buffer" border, consider giving it an EmptyBorder(...) with the parameters being int constants for the amount of border desired around the component.
Avoid using "absolute" layouts for anything, and especially for placing components at easy to place locations for the layout managers, such as at the bottom of the GUI.
For example, note in the GUI created in the code below how the center and bottom JPanel's don't go out to the edge of the GUI because of the empty border:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
public class ButtonAtBottom {
private static void createAndShowGui() {
JPanel bottomPanel = new JPanel();
bottomPanel.add(new JButton("Bottom Button"));
bottomPanel.setBorder(BorderFactory.createTitledBorder("Bottom Panel"));
JPanel centerPanel = new JPanel();
centerPanel.setBorder(BorderFactory.createTitledBorder("Center Panel"));
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(bottomPanel, BorderLayout.PAGE_END);
// **** here I add the border to the mainPanel which I'll
// make into the contentPane
int eb = 25;
mainPanel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
// don't set the preferredSize per Kleopatra, but am doing it
// here simply to make code shorter for this sscce
mainPanel.setPreferredSize(new Dimension(500, 400));
JFrame frame = new JFrame("ButtonAtBottom");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
You can use Box.createRigidArea(dimensions) to create an empty space that you can add below the button.
Set an empty border on your content panel where SIZE is the amount of padding you want.
JFrame frame = new JFrame();
JPanel panel = new JPanel(null);
panel.setBorder(BorderFactory.createEmptyBorder(SIZE,SIZE,SIZE,SIZE);
frame.setContentPane(panel);
//The rest
The arguments are for top, left, bottom and right padding so if you want different paddings on each edge, you can set it accordingly.