layout-manager

Position Image in any Screen Resolution

拟墨画扇 提交于 2019-11-27 16:28:24
I have a problem with my program. Every time when I change my screen resolution, my image starts to move out of position. Any suggestions on how to make my image stay in the same location even though I change to any other resolution? p2 = new JPanel(); p2.setLayout(new FlowLayout()); ImageIcon img2 = new ImageIcon("C:\\Drum\\Invisible4.png"); jbtn2 = new JLabel(img2); p2.add(jbtn2); add(jbtn2); jbtn2.setSize(jbtn2.getPreferredSize()); jbtn2.setLocation(140, 380); MadProgrammer I prefer to always try and work within boundaries of the framework where I can, it makes life generally easier in the

Java, setting layout to null

孤人 提交于 2019-11-27 16:25:16
I have been using a null layout and I alot of people will say it shouldn't be done this way. Is there a better way? Some code as example: import javax.swing.*; public class Main{ public static void main(String args[]){ JFrame frame = new JFrame(); JPanel panel = new JPanel(); JButton button = new JButton("Click"); //JFrame, frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setSize(500, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); //JPanel, panel panel.setLayout(null); //<<---- Is this correct? frame.add(panel); //JButton, button

Put a JButton in the center of the screen with BorderLayout

孤街浪徒 提交于 2019-11-27 16:25:13
How to add a JButton into the center of a JFrame with BorderLayout() ? I tried using BorderLayout.CENTER , but instead of the center of the screen, it gave the top-center of the screen. Or do I have to use another layout manager? Andrew Thompson Put a JPanel in the CENTER and set the layout to GridBagLayout or BoxLayout as seen in this answer to Set component at center of the page . The GridBagLayout is used to center a label containing the yellow/red gradient image seen in the Nested Layout Example . It may take some time to learn, but SpringLayout is worth looking into. It will let you

Why doesn't setLocation() move my label?

倖福魔咒の 提交于 2019-11-27 16:18:26
问题 I have the following code where I try to place a JLabel in a custom location on a JFrame . public class GUI extends JFrame { /** * * @param args */ public static void main(String args[]) { new GUI(); } /** * */ public GUI() { JLabel addLbl = new JLabel("Add: "); add(addLbl); addLbl.setLocation(200, 300); this.setSize(400, 400); // pack(); setVisible(true); } } It doesn't seem to be moving to where I want it. 回答1: The problem is that the LayoutManager of the panel is setting the location of

Positioning components in Swing GUIs

我们两清 提交于 2019-11-27 14:14:09
I have some questions on positioning components and some questions on text fields and text areas (Java Swing). Any help is greatly appreciated. Right now I am trying to have two text fields beside each other with a different label above each describing what that text field does. To achieve this I have placed them in a GridLayout(2, 2) . Is this the best way? It is the only way I know to have a label directly over another component. Is there a better way? What about if there is just one label above one button. Is it sensible to position this through a GridLayout(2, 1) ? I am visually impaired

Why does this GridBagLayout not appear as planned?

一个人想着一个人 提交于 2019-11-27 09:47:20
I was trying to achieve the end result required in Setting an arbitrary width in GridBagLayout . For easy reference, here it is: This is the current result: Button number and row is shown in the form 1,1 , followed by the number of columns (2) declared for this cell. As you can see, it starts with buttons 1,1 (3) and below it 1,2 (4) being the same width, while declaring different numbers of columns. Can anyone determine how to correct the code? The current code: import java.awt.*; import javax.swing.*; import javax.swing.border.EmptyBorder; public class KeyBoardLayout { private JComponent ui

How to locate JLabels to an absolute position on Java GUI

[亡魂溺海] 提交于 2019-11-27 09:29:28
I have many JLabel s (which includes ImageIcon s) in a JPanel . And this JPanel is only a panel on the GUI; there are lots of other panels. I want to place labels to the exact pixel coordinates on their JPanel container. How can I do that without using GroupLayout ? See Doing Without a Layout Manager (Absolute Positioning) in the Java tutorials. Creating a container without a layout manager involves the following steps. Set the container's layout manager to null by calling setLayout(null) . Call the Component class's setbounds method for each of the container's children. Call the Component

How to right-justify icon in a JLabel?

假如想象 提交于 2019-11-27 09:29:01
For a JLabel with icon, if you setHorizontalTextPosition(SwingConstants.LEADING) , the icon is painted right after text, no matter how wide the label is. This is particularly bad for a list, as the icons would be all over the place depending on how long the text is for each item. I traced the code and it seems to be that in SwingUtilities#layoutCompoundLabelImpl , text width is simply set to SwingUtilities2.stringWidth(c, fm, text) , and icon x is set to follow text without considering label width. Here is the simplest case: import java.awt.*; import javax.swing.*; public class TestJLabelIcon

How to show different pages from the center element of JFrame (having set to BorderLayout)

久未见 提交于 2019-11-27 08:57:46
问题 import java.awt.BorderLayout; import java.awt.Component; import javax.swing.JFrame; import javax.swing.border.Border; public class GuiController extends JFrame { private CentreFrameController centreFrameController; private CustomerPage customerPage; private LoginPage loginPage; public GuiController(){ centreFrameController=new CentreFrameController(this); setLayout(new BorderLayout()); add(centreFrameController,BorderLayout.CENTER); setTitle("Courier System-Login Page"); setVisible(true);

Java Layout with Component always in Top Right

百般思念 提交于 2019-11-27 08:48:31
问题 The primary GUI of my application is composed of a JDesktopPane at the CENTER of a frame's content pane using a BorderLayout. I am hoping to have a component placed in the top right of the screen that still allows the user to drag JInternalFrames within the space to the left and and bottom of this component. Setting the component to the NORTH or EAST of the BorderLayout seems to fill the entire space. I am thinking BorderLayout may not be the best layout manager for what I am trying to