2016-08-29 5 views
3

私のJava swingプロジェクトにボタンを印刷する際に問題が発生しています。クラスのために、私はGUIを複製すると仮定しています。これまでのところ、私はそれをうまくやることができました。しかし、私は、横にお互いに隣り合っているのと同じ位置に、ボタンが重なっている問題があります。以下は、ボタンがパネルにどのように印刷されているかのイメージです。ボタンは互いに水平に表示されません。互いに重なり合っているだけです

私は、ラベルとテキストボックス(Toppane)とボタンを収めたパネルの2つのパネルを持っています。計5個(bottomPane)です。私は5つのボタンをGUIの一番下に印刷するのを苦労しています。私は何かが簡単でないように感じる。

-------------------------------------------------------------- 
|     label [textfield]       | 
|     label [textField]       | 
|     label [textfield]       | 
|------------------------------------------------------------- 
| [button] [button] [button] [button] [button]    | 
-------------------------------------------------------------- 

しかし、私はこの取得:

-------------------------------------------------------------- 
|     label [textfield]       | 
|     label [textField]       | 
|     label [textfield]       | 
|------------------------------------------------------------- 
|    [  Button's 12345  ]    | 
-------------------------------------------------------------- 

コード:個人的に

package book; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import java.awt.GridBagLayout; 
import java.awt.GridBagConstraints; 


/** 
* 
* @author KJ4CC 
*/ 
public class Book extends JFrame { 
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     Book book = new Book(); 
     book.bookingUI(); 
    } 
    public static void bookingUI(){ 

     //sets windows, and pane in the UI 
     JFrame frame = new JFrame("Ye old Book store"); 
     JPanel toppane = new JPanel(new GridBagLayout()); 
     JPanel bottomPane = new JPanel(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     frame.setSize(1000, 600); 
     frame.setVisible(true); 

     //adds labels to the window 
     JLabel num = new JLabel("Enter Number of Items in this Order"); 
     JLabel bookID = new JLabel("111111"); 
     JLabel quantityItem = new JLabel("222222"); 
     JLabel itemInfo = new JLabel("333zfgfsfg333"); 
     JLabel subtotal = new JLabel("4444444"); 

     //adding the labels to the panel 
     c.anchor = GridBagConstraints.EAST; 
     c.weighty = 1; 
     c.gridx = 2; 
     c.gridy = 1; 
     toppane.add(num, c); 
     c.gridx = 2; 
     c.gridy = 2; 
     toppane.add(bookID, c); 
     c.gridx = 2; 
     c.gridy = 3; 
     toppane.add(quantityItem, c); 
     c.gridx = 2; 
     c.gridy = 4; 
     toppane.add(itemInfo,c); 
     c.gridx = 2; 
     c.gridy = 5; 
     toppane.add(subtotal,c); 
     bottomPane.setBackground(Color.GREEN); 
     frame.add(toppane,BorderLayout.EAST); 

     //adds textfields to the frame 
     JTextField amount = new JTextField(); 
     JTextField id = new JTextField(); 
     JTextField quantity = new JTextField(); 
     JTextField info = new JTextField(); 
     JTextField total = new JTextField(); 

     //add textfield to panel 
     c.ipadx = 230; 
     c.gridx = 3; 
     c.gridy= 1; 
     toppane.add(amount, c); 
     c.gridx = 3; 
     c.gridy = 2; 
     toppane.add(id, c); 
     c.gridx = 3; 
     c.gridy = 3; 
     toppane.add(info, c); 
     c.gridx = 3; 
     c.gridy = 4; 
     toppane.add(total, c); 
     c.gridx = 3; 
     c.gridy = 5; 
     toppane.add(quantity,c); 

    //setting up buttons to be placed onto the bottompanel 
    JButton processItem = new JButton("Process Item"); 
    JButton confirmItem = new JButton("Confirm Item"); 
    JButton viewOrder = new JButton("View Order"); 
    JButton finishOrder = new JButton("Finish Order "); 
    JButton newOrder = new JButton("New Order"); 
    JButton exit = new JButton("Exit"); 

    //adding the buttons to the pane. 
    GridBagConstraints b = new GridBagConstraints(); 
    b.anchor = GridBagConstraints.NORTHWEST; 
    bottomPane.add(processItem, c); 
    bottomPane.add(confirmItem,c); 
    bottomPane.add(viewOrder, c); 
    bottomPane.add(finishOrder,c); 
    bottomPane.add(newOrder,c); 

    bottomPane.add(exit, c); 
    bottomPane.setBackground(Color.BLUE); 
    frame.add(bottomPane,BorderLayout.SOUTH); 
    } 
} 

を、それは私が使用していレイアウトマネージャとは何かを持っているような気が。適切なアプリケーションに適切に使用しているかどうかはわかりません。私はGridBagLayoutを使っています。それはこれまで私が学校に使ってきたものです。

+2

1)*「はい、私は色の選択は醜いですけど....教授が何を望んでいるか」*ここに示されたコードに色付けを含める必要はありません。レイアウトの問題が修正された後、いつでも後で置くことができます!2)ASCIIアートや3)* "私は' GridBagLayout'を使用していましたが、それは今まで私が学校に使ってきたすべてのものです。* Whhen allあなたはハンマーです。すべてが爪のように見えます。異なるレイアウトマネージャーは、さまざまなものに適していて、しばしば最も簡単です。 –

+1

.. **レイアウトマネージャーを組み合わせてGUIを作成する**。 –

+1

私は少し芸術を加えました –

答えて

3

あなたの問題は、あなたがあなたの新しいボタンの一人一人のために同じ制約cを使用していることである:あなたがやったとき

bottomPane.add(processItem, c); 
bottomPane.add(confirmItem,c); 
bottomPane.add(viewOrder, c); 
bottomPane.add(finishOrder,c); 
bottomPane.add(newOrder,c); 

あなたはcに行われた最後の変更が上記のアップだった:

c.gridx = 3; 
c.gridy = 5; 

そして、5つの新しいボタンすべてに対して同じ制約を再利用しているため、それらをすべて同じグリッド位置に追加します。

これに応じて制約を設定する必要があります(たとえば、cの値を設定すると、そこには使用されていない迷路bもあります)。

+0

ええ、私はちょうどそれに気づいた、私はこの方法で動作するように私は傾けることが表示されます。私はレイアウトを変更しました。今度は不思議になります –

2

私はボトムパネルをboxlayoutに設定して、ボタンを水平にしました!

修正コード!

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package book; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.BoxLayout; 
import java.awt.GridBagLayout; 
import java.awt.GridBagConstraints; 
import java.awt.Insets; 


/** 
* 
* @author KJ4CC 
*/ 
public class UserInterface extends JFrame { 

    public void startUI(){ 

     UserInterface gui = new UserInterface(); 
     gui.bookingUI(); 
    } 
    public static void bookingUI(){ 
     //sets windows, and pane in the UI 
     JFrame frame = new JFrame("Ye old Book store"); 
     JPanel toppane = new JPanel(new GridBagLayout()); 
     JPanel bottomPane = new JPanel(); 
     bottomPane.setLayout(new BoxLayout(bottomPane, BoxLayout.LINE_AXIS)); <---------------------------------------------Here is the fix 
     GridBagConstraints c = new GridBagConstraints(); 
     frame.setSize(800, 300); 
     frame.setVisible(true); 
     //adds labels to the window 
     JLabel num = new JLabel("Enter Number of Items in this Order"); 
     JLabel bookID = new JLabel("111111"); 
     JLabel quantityItem = new JLabel("222222"); 
     JLabel itemInfo = new JLabel("33333"); 
     JLabel subtotal = new JLabel("4444444"); 
     //adding the labels to the panel----------------------------------------------------- 
     c.insets = new Insets(5,0,0,0); 
     c.gridx = 2; 
     c.gridy = 1; 
     toppane.add(num, c); 
     c.gridx = 2; 
     c.gridy = 2; 
     toppane.add(bookID, c); 
     c.gridx = 2; 
     c.gridy = 3; 
     toppane.add(quantityItem, c); 
     c.gridx = 2; 
     c.gridy = 4; 
     toppane.add(itemInfo,c); 
     c.gridx = 2; 
     c.gridy = 5; 
     toppane.add(subtotal,c); 
     toppane.setBackground(Color.GREEN); 
     frame.add(toppane); 


     //adds textfields to the frame ---------------------------------------------------- 
     JTextField amount = new JTextField(); 
     JTextField id = new JTextField(); 
     JTextField quantity = new JTextField(); 
     JTextField info = new JTextField(); 
     JTextField total = new JTextField(); 
     //add textfield to panel 
     c.ipadx = 400; 
     c.insets = new Insets(5,10,0,0); 
     c.gridx = 3; 
     c.gridy= 1; 
     toppane.add(amount, c); 
     c.gridx = 3; 
     c.gridy = 2; 
     toppane.add(id, c); 
     c.gridx = 3; 
     c.gridy = 3; 
     toppane.add(info, c); 
     c.gridx = 3; 
     c.gridy = 4; 
     toppane.add(total, c); 
     c.gridx = 3; 
     c.gridy = 5; 
     toppane.add(quantity,c); 

    //----------------------------------------------------------BUTTOM PANE------------------------- 
    //setting up buttons to be placed onto the bottompanel 
    JButton processItem = new JButton("Process Item"); 
    JButton confirmItem = new JButton("Confirm Item"); 
    JButton viewOrder = new JButton("View Order"); 
    JButton finishOrder = new JButton("Finish Order "); 
    JButton newOrder = new JButton("New Order"); 
    JButton exit = new JButton("Exit"); 
    //adding the buttons to the pane.--------------------------------------------------------------- 
    GridBagConstraints b = new GridBagConstraints(); 
    b.ipadx = 20; 
    b.ipady = 20; 
    b.gridx = 0; 
    b.gridy = 1; 
    bottomPane.add(processItem, c); 
    b.gridx = 0; 
    b.gridy = 2; 
    bottomPane.add(confirmItem,c); 
    b.gridx = 0; 
    b.gridy = 3; 
    bottomPane.add(viewOrder, c); 
    b.gridx = 0; 
    b.gridy = 4; 
    bottomPane.add(finishOrder,c); 
    b.gridx = 0; 
    b.gridy = 5; 
    bottomPane.add(newOrder,c); 
    b.gridx = 0; 
    b.gridy = 6; 
    bottomPane.add(exit, c); 
    bottomPane.setBackground(Color.BLUE); 
    frame.add(bottomPane,BorderLayout.SOUTH); 
    frame.setSize(810, 310); 

    } 



} 
+0

'b'を設定していますが、まだ' c'を 'add()'に渡していますが、今は 'BoxLayout'を使用しているので、 。それでも、怠け者にならないでください! –

+0

私は戻ってそれを修正!私はそれがどのように働いているのか見たいと思っていて、同じこともしました。 –

+0

私は 'FlowLayout'を持つパネルを使います。 'FlowLayout'は自動的にコンポーネント間に5ピクセルの間隔を与えます。 – camickr

0

ボトムペインにフローレイアウトを使用する必要があります。この使用のためにGridLayoutより優れているわけではありません。

3

GridBagLayoutは旧式です。そしてそれは一緒に働く痛みです。 MigLayoutのような最新のレイアウトマネージャを使用すると、コードサンプルを非常に迅速に作成できます。

package com.zetcode; 

import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import net.miginfocom.swing.MigLayout; 

/** 
* MigLayout demonstration example. 
* @author Jan Bodnar 
* Website zetcode.com 
*/ 

public class MigLayoutBookEx extends JFrame { 

    public MigLayoutBookEx() { 

     initUI(); 
    } 

    private void initUI() { 

     JLabel lbl1 = new JLabel("Label"); 
     JLabel lbl2 = new JLabel("Label"); 
     JLabel lbl3 = new JLabel("Label"); 

     JTextField field1 = new JTextField(15); 
     JTextField field2 = new JTextField(15); 
     JTextField field3 = new JTextField(15); 

     JButton btn1 = new JButton("Button"); 
     JButton btn2 = new JButton("Button"); 
     JButton btn3 = new JButton("Button"); 
     JButton btn4 = new JButton("Button"); 
     JButton btn5 = new JButton("Button"); 

     createLayout(lbl1, field1, lbl2, field2, lbl3, field3, 
       btn1, btn2, btn3, btn4, btn5); 

     setTitle("MigLayoutExample"); 

     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    private void createLayout(JComponent... arg) { 

     setLayout(new MigLayout("ins 10lp, gap 5lp 8lp, fillx", "[center]")); 

     add(arg[0], "split 2, span"); 
     add(arg[1], "wrap"); 
     add(arg[2], "split 2, span"); 
     add(arg[3], "wrap"); 
     add(arg[4], "split 2, span"); 
     add(arg[5], "wrap"); 
     add(arg[6], "split 5, gapy 5lp, align left"); 
     add(arg[7]); 
     add(arg[8]); 
     add(arg[9]); 
     add(arg[10]); 

     pack(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> { 
      MigLayoutBookEx ex = new MigLayoutBookEx(); 
      ex.setVisible(true); 
     }); 
    } 
} 

レイアウトは、さまざまな制約の組み合わせで作成されます。一度これを学ぶと、最も実用的なレイアウトはケーキです。あなたはマネージャのwebsiteで詳細を知ることができます。

スクリーンショット:

Screenshot of the example

+0

うわー、それは素晴らしいです!ヒントをありがとう! –

関連する問題