2017-02-15 16 views
0

私は、ユーザがJSlidersを使用して設定できる色の四角形と円を作成できるプログラムを作成しようとしています。私はこれを設定するためにGroupLayoutを使用しようとしていますが、それはすべきであると思われる方法では動作しません。GroupLayoutが正しく整列していない

円、四角、および色のボタンが隣り合って表示されるようにしたいが、JSlidersとその名前のJLabelがColor 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回括弧をチェックしましたが、それが問題ではないことを保証するために、それをより多くのステートメントに分けましたが、それでも起こっています。

ご協力いただきありがとうございます。




答えて

1

さて、あなたはコード

の一行は、単に「I」のこの

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

毎日投与後に、この

setLayout(layout); 

を追加逃しましたID私は、チェックしてください。本当にありがとう! –

関連する問題