2016-03-23 11 views
1

私はJava 2Dグラフィックス・ペイント・プログラムを作成しようとしています。私はフローレイアウトを使用しています。私は3つのパネルを使用しています。最初の2つはボタン、コンボボックスなどの行で、3つ目は塗りつぶしに使用される白い大きな白いパネルを意味します。最初の2つのパネルがきれいに表示されますが、ペイントパネルは2番目のボタンパネルの隣に小さな白いボックスとして表示されます。どんな助けもありがとう。JPanelは小さな白いボックスのように見えます

public class DrawingApp extends JFrame 
{ 
    private final topButtonPanel topPanel = new topButtonPanel(); 
    private final bottomButtonPanel bottomPanel = new bottomButtonPanel(); 
    private final PaintPanel paintPanel = new PaintPanel(); 

    public DrawingApp() 
    { 
     super("Java 2D Drawings"); 
     setLayout(new FlowLayout()); 
     add(topPanel); 
     add(bottomPanel); 
     add(paintPanel); 
    } 

    public static void main(String[] args) 
    { 
     DrawingApp frame = new DrawingApp(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(750,500); 
     frame.setVisible(true); 
    } 
} 

public class topButtonPanel extends JPanel 
{ 

    private final String[] names = {"Line", "Oval", "Rectangle"}; 

    private final JButton undo = new JButton("Undo"); 
    private final JButton clear = new JButton("Clear"); 
    private final JLabel shape = new JLabel("Shape:"); 
    private final JComboBox<String> shapesComboBox = new JComboBox(names); 
    private final JCheckBox filled = new JCheckBox("Filled"); 

    public topButtonPanel() 
    { 
     super(); 
     setLayout(new FlowLayout()); 
     add(undo); 
     add(clear); 
     add(shape); 
     shapesComboBox.setMaximumRowCount(3); 
     add(shapesComboBox); 
     add(filled); 
    } 
} 


public class bottomButtonPanel extends JPanel 
{ 

    private final JCheckBox useGradient = new JCheckBox("Use Gradient"); 
    private final JButton firstColor = new JButton("1st Color"); 
    private final JButton secondColor = new JButton("2nd Color"); 
    private final JLabel lineWidthLabel = new JLabel("Line Width:"); 
    private final JLabel dashLengthLabel = new JLabel("Dash Length:"); 
    private final JTextField lineWidthField = new JTextField(2); 
    private final JTextField dashLengthField = new JTextField(2); 
    private final JCheckBox filled = new JCheckBox("Dashed"); 

    public bottomButtonPanel() 
    { 
     super(); 
     setLayout(new FlowLayout()); 
     add(useGradient); 
     add(firstColor); 
     add(secondColor); 
     add(lineWidthLabel); 
     add(lineWidthField); 
     add(dashLengthLabel); 
     add(dashLengthField); 
     add(filled); 
    } 
} 

public class PaintPanel extends JPanel 
{ 

    public PaintPanel() 
    { 
     super(); 
     setBackground(Color.WHITE); 
     setSize(700,400); 
    } 
} 
+0

'パブリッククラスtopButtonPanelは、一般的なJavaの命名法を学ぶ下さいJPanel'を拡張

は詳細についてはLaying Out Components Within a Containerを参照してください。これらの経営者がどのように機能するかをよりよく理解している必要があり(conve命名ntions - たとえば'EachWordUpperCaseClass'、' firstWordLowerCaseMethod() '、' firstWordLowerCaseAttribute'が 'UPPER_CASE_CONSTANT'でなければ)それを一貫して使用してください。機能が変更されていないので、JPanelを拡張する理由はありません。ちょうど1のインスタンスを使用してください。 –

答えて

1

基本的に、それはSwing APIの仕組みに関する誤解です。レイアウトマネージャは、それはだようになりますようsetSizeを使用して

スイングが可能(とどこに置かれるべきである)べきか、大きなコンポーネントに関する意思決定を行うために使用されるレイアウト管理APIに(大きく)依存している

は、無意味ですあなたのコンポーネントのサイズをどう考えるべきかについて自分自身で決定し、それに応じてそれを調整します。

あなたが例えば

public class PaintPanel extends JPanel 
{ 

    public PaintPanel() 
    { 
     super(); 
     setBackground(Color.WHITE); 
    } 

    public Dimension getPreferredSize() { 
     return new Dimension(700, 400); 
    } 
} 

ちょうどレイアウトマネージャは、これらの値を無視する彼らの権利範囲内である、覚えて、あなたがgetPreferred/Minimum/MaximumSizeを使用するようにコンポーネントをたいと思いますどのように大きなについてレイアウトマネージャに提案を行うことができますあなたが