layout-manager

Gap between JPanels in GridLayout

▼魔方 西西 提交于 2019-12-24 16:06:18
问题 The code below produces nine individual JPanels in which are 9 buttons. The nine JPanels are arranged onto a base JPanel using GridLayout. This base JPanel is then placed onto the ContentPane using Border Layout. I'm using borders for the JButtons and each individual JPanel to clearly define their separation. The JButtons inside each JPanel look fine but there is a gap between the JPanels which is causing the appearance of a double line which is bugging the hell out of me. I've tried adding

How do I stop center of BorderLayout from taking up east and west?

醉酒当歌 提交于 2019-12-24 15:13:53
问题 I have a program in which I use BorderLayout and the EAST and WEST positions are not filled, though the CENTER one is. As a result, the center component extends to the space where the east and west components would be. How can I prevent this? I know I could just use a blank JPanel in the east and west positions, but that seems hacky. 回答1: If what you want is empty space around your center panel, you can add a border to it: panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10); 回答2:

set custom location for a component in box layout

喜夏-厌秋 提交于 2019-12-24 14:55:20
问题 I have a frame, inside this frame I have a panel with box layout, inside this panel I have 4 more panels. mainFrame = new JFrame("Basket Game"); mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); mainPanel.add(options); mainPanel.add(pname); mainPanel.add(info); mainPanel.add(gamearea); mainFrame.setContentPane(mainPanel); mainFrame.pack(); mainFrame.getContentPane().setBackground(Color.LIGHT_GRAY); mainFrame.setResizable(false); mainFrame.setVisible

How does GridBagLayout functions such as gridwidth/height and gridx/y work to scale the size of the GUI?

て烟熏妆下的殇ゞ 提交于 2019-12-24 14:25:20
问题 I was recently introduced to GridBagLayout to substitute the vanilla JPanel layout, but I am having trouble working with the functions like gridwidth/height and gridx/y. I am not sure how they work exactly in changing the size of the GUI and the positions of the buttons. Below is a code my professor gave me to use for reference, and I tried fiddling with certain numbers but the results never turned out to be what I expected to happen, and I am not sure why. EDIT 1: To clarify, I am wondering

CardLayout changing panel from another class

会有一股神秘感。 提交于 2019-12-24 14:13:00
问题 I'm teaching myself java GUI and after going through some examples on stackoverflow I got stuck on this one (below). What I wanted to know was how would I go to the previous JPanel from another class? If I had a JButton in Window3 that had an actionlistener, how would i tell it to go to the previous class? import java.awt.*; import java.awt.event.*; import javax.swing.*; /* Here we are first declaring our class that will act as the * base for other panels or in other terms the base for

BorderLayout.CENTER doesn't center

 ̄綄美尐妖づ 提交于 2019-12-24 12:19:10
问题 I can't get my JLabel to center in my JPanel after adding a ChartPanel to it: JPanel panel = new JPanel(new BorderLayout()); panel.add(visualiser(ternaire), BorderLayout.NORTH);//visualiser(ternaire) is the ChartPanel panel.add(new JLabel("L'alliage a bien été enregistré."), BorderLayout.CENTER); JOptionPane jop = new JOptionPane(); jop.showMessageDialog(null, panel, "Information", JOptionPane.PLAIN_MESSAGE); Any idea why BorderLayout.CENTER doesn't work ? 回答1: JLabel by default is

GridBagLayout not coming out as expected

元气小坏坏 提交于 2019-12-24 10:24:43
问题 I'm creating a GUI that should have buttons on top, a view screen in the middle, and at the bottom, an area with labels and text entry fields which change based on which button is pressed. The main panel on which everything lays is a GridBagLayout. The panel which holds the text entry fields is a CardLayout. I feel as though I've set the GridBagConstraints and everything else as it should be, but it comes up nothing as expected. I imagine there is a problem with how I've set the

How do I align components in Java Swing?

寵の児 提交于 2019-12-24 09:27:05
问题 I'm building a simple beginner app in Java and I need your help with aligning components. What I'm trying to do is to align component(JLabel "name") to the left side of the panel. I've already tried with "new FlowLayout(FlowLayout.LEFT)" but it didn't work so I'm asking you to help me. Here is the picture of the frame and source code below it. public class firstClass extends JFrame implements ActionListener { private JFrame frame1; private JFrame frame2; private JPanel mainPanelFirst; private

How to resize a JPanel to fit a JFrame in “docknorth” with no interference to remaining JPanels

≯℡__Kan透↙ 提交于 2019-12-24 07:58:04
问题 I am doing a little test of a demo Swing GUI. In this demo, the JFrame is composed of 3 "master" JPanel s. If you will, the first ( jp1 ) is composed of JLabel s, and the other two are composed of several other JPanel s. I am using MigLayout. Here is my sample code: // All the jPanels JFrame frame = new JFrame(); frame.setLayout(new MigLayout()); JPanel jp1 = new JPanel(); jp1.setLayout(new MigLayout()); jp1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); JPanel jp2 = new

MigLayout align center won't center JLabel component

血红的双手。 提交于 2019-12-24 07:15:42
问题 I am using MigLayout I find it flexible etc,but I am having a problem with centring stuff with it. I tried using gapleft 50% but it seems like the percent number needs to change on different frame sizes, because it's also depending on component's size. so if the component is centred using gapleft 25% , it will be on a different location if i resize the width of my frame. I've tried using just align center and it doesn't nothing at all. I've also tried new CC().alignX("center").spanX() and