2017-02-15 7 views
0

저는 사용자가 JSliders를 사용하여 설정할 수있는 색상의 사각형과 원을 만들 수있는 프로그램을 만들려고했습니다. GroupLayout을 사용하여이 작업을 설정하려고 시도하지만 작업해야하는 방식으로 작동하지 않습니다.GroupLayout이 제대로 정렬되지 않았습니다.

원, 사각형 및 색상 버튼이 서로 옆에 나타나기를 원하지만 JSliders와 그 이름 JLabels이 색상 JButton (그 아래로 내려감)과 함께 수평으로 나타나는 것처럼 보입니다.

그러나 오른쪽에서 왼쪽으로 이동하여 적합하지 않은 경우 새로운 행으로 이동하는 것처럼 플로우 레이아웃을 사용하는 것처럼 보입니다.

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 
     } 

나는 주위를 둘러 보았다, 정말 왜 이런 일이에 관해서는 어떤 이유를 보지 못했다

Image of components being placed side to side

. 많은 파기와 생각을 한 후에도 나는 이것이 왜 효과가 있는지 알지 못합니다. 아마 20 번이나 괄호를 검사했는데 문제가 아닌 것을 보장하기 위해 더 많은 진술로 구분했습니다. 그러나 여전히 일어나고 있습니다.

도움을 주시면 대단히 감사하겠습니다.




답변

1

글쎄, 당신은

그냥 내가 ' "이

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

매일 투여 한 후이

setLayout(layout); 

을 추가 한 줄의 코드를 놓쳤다 이드는 안돼. " 정말 고마워! –