I'm creating an applet and having problems with the positioning as size of my buttons. I've added two buttons, but the "OK" button seems to position and size itself correctly, but the "CLEAR" button fills the entire applet area behind the "OK" button. Any suggestions as to what is the problem?
#Override
public void init()
{
super.init();
setSize(J_WIDTH, J_HEIGHT);
setLayout(new BorderLayout());
btn_OK = new Button("OK");
btn_CLEAR = new Button("CLEAR");
btn_OK.setBounds(50, 450, 75, 50);
btn_CLEAR.setBounds(125, 50, 75, 50);
add(btn_OK);
add(btn_CLEAR);
btn_OK.addActionListener(this);
btn_CLEAR.addActionListener(this);
}
When using a BorderLayout, you should specify a location where you want to place the component. If you don't, the default is BorderLayout.CENTER. Also, each position can only contain one component. So when you call add(btn_OK), the OK button is added to the center of the panel. But then you replace it with the Clear button by calling add(btn_CLEAR);.
In addition, each position in the BorderLayout takes up a certain amount of space. The component at that position will stretch to fill that space. In particular, the CENTER takes up all remaining space not used by the other positions.
I think that BorderLayout is not what you want here. Check out the Visual Guide to Layout Managers for more information on each LayoutManager. You can also follow the rest of the tutorial trail for details about how to implement each of them.
You should also bookmark and familiarize yourself with the Java API docs. These are an essential tool for every Java programmer and will help you answer many questions on your own.
Related
In my code I have a JFrame that opens up a main menu, in that menu you can click options, which will take you to the options menu. On that options menu I want to create a back button that will position itself somewhere in the top left corner of the screen. When I run the program and go to the options menu the JButton does not appear. Something must be wrong with my code. Any help? Below is where I first declare the JButton.
static JButton optionsBackButton = new JButton("<html><font size = 5
color = green>Back</font></html>");
Here is the other part of the code that has to do with the JButton.
//Options Menu
JPanel optionsPanel = new JPanel();
JLabel optionsOptionsTitle = new JLabel("<html><font size = 7 color = blue>Options</font></html>");
JPanel optionsOptionsTitlePanel = new JPanel();
JPanel optionsBackButtonPanel = new JPanel();
optionsPanel.setLayout(null);
optionsBackButton.setBounds(100,100,50,50);
optionsBackButtonPanel.add(optionsBackButton);
optionsOptionsTitlePanel.add(optionsOptionsTitle);
optionsPanel.add(optionsOptionsTitlePanel);
optionsPanel.add(optionsBackButtonPanel);
optionsBackButton.addActionListener(this);
//Add panels to the card
panel.add("Home Screen", homePanel);
panel.add("Options Menu", optionsPanel);
//card.add("Game screen", gamePanel);
cardLayout.show(panel, "HomeScreen");
contentPane.add(panel);
Your problem is stemming from your use of null layouts. Note:
JPanel optionsBackButtonPanel = new JPanel(); // holds the back button
// but never given a size
optionsPanel.setLayout(null); // uh oh,.... bad news
optionsBackButton.setBounds(100,100,50,50); // yes you set the bounds of the button
optionsBackButtonPanel.add(optionsBackButton);
optionsOptionsTitlePanel.add(optionsOptionsTitle);
optionsPanel.add(optionsOptionsTitlePanel);
optionsPanel.add(optionsBackButtonPanel); // again, optionsBackButtonPanel
// has been never given a size or location/bounds
optionsBackButton.addActionListener(this);
So you set the bounds of the button and add it to a JPanel that uses the default FlowLayout, and so the bounds are meaningless. Then you add that JPanel, whose bounds you never set, to a JPanel that uses null layout. So the optionsBackButtonPanel will never show.
Solution: Don't use null layouts, but instead learn and use the layout managers.
While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
Start here: Laying Out Components in a Container Tutorial
Im new in Java Swing, and want to make my layout, but can't do this
Look Now :
Look I want :
Code Now :
JPanel MainPanel = new JPanel(new GridBagLayout());
JLabel MoneyLabel = new JLabel(MoneyIcon);
MoneyLabel.setHorizontalTextPosition(JLabel.CENTER);
MoneyLabel.setVerticalTextPosition(JLabel.BOTTOM);
MoneyLabel.setText("Money:" + CarMain.Money);
JLabel MoneyClicksLabel = new JLabel();
MoneyClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
MoneyClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
MoneyClicksLabel.setText("Money Clicks: " + CarMain.MoneyClicks);
JLabel BoxesLabel = new JLabel(BoxLv9_10Icon);
BoxesLabel.setHorizontalTextPosition(JLabel.CENTER);
BoxesLabel.setVerticalTextPosition(JLabel.BOTTOM);
BoxesLabel.setText("Boxes: " + CarMain.Boxes);
JLabel BoxesClicksLabel = new JLabel();
BoxesClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
BoxesClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
BoxesClicksLabel.setText("Boxes Clicks: " + CarMain.BoxesClicks);
MainPanel.add(MoneyLabel);
MainPanel.add(MoneyClicksLabel);
MainPanel.add(jbtnMoney);
MainPanel.add(BoxesLabel);
MainPanel.add(BoxesClicksLabel);
MainPanel.add(jbtnBoxes);
This is simple example of, what i want, becouse i'm building ingame shop, with 13 labels like these, in each tabbedpane window. How can i make it look, like in second picture, what I want?
Im new in Java Swing, and want to make my layout, but can't do this
Probably no single layout can suit everyone's needs. But combining several layouts can usually handle most scenarios.
From the image you showed in the question. There is no need to write your own layout. You can always use sub panels to hold your components and set a specific layout for each sub panel to handle what you need for those individual areas.
The reason for the alignment in your first attached image is because:
JPanel uses FlowLayout as its default layout. Hence all the components added will appear in a linear fashion and tries to fill up the row as much as possible the panel's width can hold. Once exceeded the panel's width, the components will be pushed to the next row.
If you want to achieve the alignment in the second attached image:
You may create a main panel to contain several sub-panels (see image below).
The red box is your main panel and you may continue to use the default FlowLayout.
Then add your components into sub-panels (orange boxes) before adding it to the main. You may then use BoxLayout, FlowLayout or even GridBagLayout for the sub panels (orange boxes).
Artis Uljanovs, at night after work i will give a look at this to help you.
I recommend you already to read the following: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
You need some foundations on Java Layouts.
I have created a GUI in Java which looks as shown below -
'panel_mid' is the white panel in the middle. I have added it to a scrollpane called 'panel_mid_scrollpane'.
Apart from 'panel_mid' there are more panels -
panel_left (containing 'back' button)
panel_right (visible on right hand side)
Revelant code for this gui is -
panel_mid.setBorder(grayborder);
panel_mid.setBounds(0, 0, 1100, 1060);
panel_mid.setBackground(Color.white);
panel_mid.add(obj.create_test_add_section);
panel_mid_scrollpane = new JScrollPane(panel_mid);
panel_mid_scrollpane.setLocation(150, 20);
panel_mid_scrollpane.setSize(1000, 660);
panel_mid_scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
The Add Section button shown in panel_mid, adds a section to the middle panel, every time it is clicked. When this button is clicked multiple times, the gui looks like -
As you could see, the scrollbar does not appear automatically as panels are added, the last panel is thus only half visible. What could be causing this problem ?
Thanks !
Scrollbars appear automatically when the preferred size of the component added to the scrollpane is greater than the size of the scroll pane.
You appear to be using a null layout.
//panel_mid.setBounds(0, 0, 1100, 1060);
panel_mid.setBackground(Color.white);
panel_mid.add(obj.create_test_add_section);
panel_mid_scrollpane = new JScrollPane(panel_mid);
//panel_mid_scrollpane.setLocation(150, 20);
//panel_mid_scrollpane.setSize(1000, 660);
Don't use a null layout with setSize() and setLocation. Swing was designed to be used with layout managers. If you use layout managers then the scrollbar will work automatically and the size and location will be calculated automatically for you.
Read the Swing tutorial on Layout Mangers.
You must tell the GUI to refresh, so that the containers are laid out again. This will show the container that it also has to show a scrollbar.
So in the ActionListener or whatever you use to add a section, add code like:
container_with_sections.validate();
container_with_sections.repaint();
where container_with_sections is the container (JContainer) which contains the JScrollPane, or a container which contains a container which contains the JScrollPane, and so on.
I'm making a simple JFrame with the GUI editor in netbeans with a background image set as an icon in a label as suggested by the netbeans site, with a label and a button centered. I was having a very hard time centering them without using the null layout and setting the pixels to center them. I have an 800X600 image as the background, and I don't want the window to be resizeable. So I unchecked resizeable in the properties, and on the code tab I have designer size set to 800, 600, generate size is checked, and the form size automatically sets to 816, 638. This then gives me a border around the right and bottom sides of a few pixels. If I change the Form Size to 800, 600, then the background image is cut off by a few pixels. One other thing that I set that may impact that is in the properties=>bounds set to 800, 600, 800, 600.
Any advice on how to get rid of the border without allowing the window to be resizeable as well as any on whether a different layout can help with centering would be greatly appreciated. I did find some information that Grid Bag layout would help, but I wasn't quite able to get it working correctly. I suppose that writing out the code instead of using the GUI editor may also be a better alternative, but I'm pretty new so any advice on that would be great as well.
Don't use null layout when you can center components quite easily if you use the correct layout or combination of layouts. For instance if you want a JLabel next to a JButton and have them centered in a JPanel, put the JLabel and JButton into their own JPanel first (make sure to have this JPanel's opaque property set to false) and then have the containing JPanel use GridBagLayout. If you add one component (the inner JPanel) without GridBagConstraints, the component is centered automatically, even if the containing JPanel is resized. It's almost idiot-proof, whereas null layout is a recipe for difficult hard to maintain code.
I need to make a button that will display different things depending on the state if the app. So for example if nothing has been opened its title will be "Lesson Plans" and if project B is open it will be "project B Lesson Plan", I am using the java.awt.Button class for this. My question, is there a way to determine how big the button should be to fit the given text. For example can I somehow get the width of each character in the given font used by the button then multiply this by the characters in the title I want to use? How would I get this character width or is there some better way? Thanks!
Measuring widths of individual characters in particular fonts (not all characters are the same width) gets complicated, especially if you want to take into account the spaces between characters, and add some margin either side so they don't butt up against the edges of the button. (N.B. Others have provided a few ways of doing this more simply than I realized was possible!)
Much better/more usual solution is to let Java do the work for you. If you're using using a java.awt.Button (or javax.swing.JButton), then it will usually automatically size to the text on it. If your particular button is behaving strangely, then you probably have a layout problem. Check the section of the Java tutorial on Layout Managers for more information.
As an example (in Swing, rather than AWT - sorry!), these two buttons end up being the right size for the text on them:
import javax.swing.*;
public class ButtonTest {
public static void main(String[] args) {
JButton button1 = new JButton("Some label");
JButton button2 = new JButton("Some much longer label");
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(button1);
panel.add(button2);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
If you really do want the size, rather than just letting Java do its best, then I would recommend calling button1.getPreferredSize() and button2.getPreferredSize() after the call to frame.pack().
Just a quick glance at the API Docs shows:
public Rectangle2D getStringBounds(String str, FontRenderContext frc)
in java.awt.Font
Returns the logical bounds of the specified String in the specified FontRenderContext. The
logical bounds contains the origin, ascent, advance, and height, which includes the leading.
So that should help you.
On the awt Button you can call getFontMetrics and then ask for the getStringBounds. This you can then set the preferred size of the button based on this. It won't take into account the extra spacing you'll need for the borders though (which varies depending on platform).
I'd try setting each to the strings in turn, getting the preferred sizes and then just set the preferred size for the largest size.
Have you tried using either getMinimumSize() or getPreferredSize() on the button? I believe one of them will tell you the minimum size the button needs to be to display its label.