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