java - GroupLayout 未正确对齐

标签 java grouplayout

我一直在尝试制作一个程序,允许用户创建可以使用 JSliders 设置的颜色的正方形和圆形。我正在尝试使用 GroupLayout 来设置它,但它没有按照它看起来应该的方式工作。

我希望圆形、方形和颜色按钮彼此相邻显示,但 JSlider 及其名称 JLabels 与颜色 JButton 水平对齐(从其向下)。

但是,它们看起来就像只是使用了流布局,从右到左,如果不适合则向下移动到新行。

Image of components being placed side to side

import java.util.ArrayList;
import java.text.*;
import javax.swing.*;
import javax.swing.GroupLayout;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;

public class SMYS01 extends JFrame{

    private JLabel redL=new JLabel("red"), greenL=new JLabel("green"), blueL=new JLabel("blue"), alphaL=new JLabel("alpha");
    private JButton openColorSliders=new JButton("Change colors");
    private JSlider red=new JSlider(JSlider.HORIZONTAL, 0,255,130);
    private JSlider green=new JSlider(JSlider.HORIZONTAL, 0,255,130);
    private JSlider blue=new JSlider(JSlider.HORIZONTAL, 0,255,130);
    private JSlider alpha=new JSlider(JSlider.HORIZONTAL, 0,255,255);
    private int redColor=130, blueColor=130, greenColor=130, alphaColor=255;    

    public static void main(String[] args) {
        SMYS01 window = new SMYS01();    
    }


    private JButton makeSquareB = new JButton("New Square" /*, add icon later*/);

    private JButton makeCircleB = new JButton("New Circle");

    private Color background = new Color(0, 150, 0);



    public SMYS01(){
        //Anonymous actionlisteners and changelisteners for each button and slider. 
        //not essential for layout problems so leaving them out
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane();
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        MyPanel thing = new MyPanel();

        setContentPane(thing);

        setSize(thing.getPreferredSize());

        setTitle("Art!");
    }


    private class MyPanel extends JPanel {

        public MyPanel() {
            //MouseMotionListener and MouseListener not related
            GroupLayout layout = new GroupLayout(this);
            red.setMajorTickSpacing(50);
            red.setMinorTickSpacing(5);
            red.setPaintTicks(true);
            red.setPaintLabels(true);
            blue.setMajorTickSpacing(50);
            blue.setMinorTickSpacing(5);
            blue.setPaintTicks(true);
            blue.setPaintLabels(true);
            green.setMajorTickSpacing(50);
            green.setMinorTickSpacing(5);
            green.setPaintTicks(true);
            green.setPaintLabels(true);
            alpha.setMajorTickSpacing(50);
            alpha.setMinorTickSpacing(5);
            alpha.setPaintTicks(true);
            alpha.setPaintLabels(true);
            layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);

            GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
            hGroup.addComponent(makeCircleB);
            hGroup.addComponent(makeSquareB);
            hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(openColorSliders)
                    .addComponent(redL)
                    .addComponent(red)
                    .addComponent(greenL)
                    .addComponent(green)
                    .addComponent(blueL)
                    .addComponent(blue)
                    .addComponent(alphaL)
                    .addComponent(alpha));

            GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
            vGroup
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(makeCircleB)
                    .addComponent(makeSquareB)
                    .addComponent(openColorSliders));
            vGroup
                    .addComponent(redL)
                    .addComponent(red)
                    .addComponent(greenL)
                    .addComponent(green)
                    .addComponent(blueL)
                    .addComponent(blue)
                    .addComponent(alphaL)
                    .addComponent(alpha);
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);





        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(700, 500);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(background);
            g.drawRect((int)g.getClipBounds().getX(),(int)g.getClipBounds().getY(),(int)g.getClipBounds().getWidth(),(int)g.getClipBounds().getHeight());
            g.fillRect((int)g.getClipBounds().getX(),(int)g.getClipBounds().getY(),(int)g.getClipBounds().getWidth(),(int)g.getClipBounds().getHeight());


            //An iterated loop drawing the shapes using info from square and circle classes
            //Both of which implement a shape interface. Not part of problem
        }

我环顾四周,并没有真正看到任何发生这种情况的原因。经过大量的挖掘和思考,我仍然不知道为什么这是有效的。我检查了我的括号大概20次,并且我把它分成了更多的语句,以保证它们不是问题,但它仍然在发生。

非常感谢任何帮助!

<小时/> <小时/> <小时/>

最佳答案

好吧,你错过了一行代码

添加这个

setLayout(layout);

就在这之后

layout.setHorizontalGroup(hGroup);
layout.setVerticalGroup(vGroup);

关于java - GroupLayout 未正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42239263/

相关文章:

java - 科学记数法转十进制

尽管限制访问,Java 类变量并不是不可变的

java - 如何重新创建这个基于 JFrame 的进度条?

java - GroupLayout 未给出所需的结果

java - Eclipse 中的 Maven 项目创建问题

java - 无法检索日期

java - 在 JPanel 调整大小上使用 GroupLayout 将 JButtons 保持在 JPanel 中心的最佳方法是什么

java - 使用手动间隙时的组布局、错误/奇怪行为

java - 使用 Matisse GUI 构建器设置最大宽度

java - 如何使用计时器移动方 block