Ich habe Probleme beim Drucken von Schaltflächen zu meinem Java-Swing-Projekt. Für den Unterricht sollte ich eine GUI replizieren. Bisher konnte ich es gut machen. Aber ich habe ein Problem mit den Tasten, die einander an der gleichen Stelle einander gegenüberliegend horizontal nebeneinander überlappen. Im Folgenden sehen Sie, wie die Schaltflächen auf das Bedienfeld gedruckt werden.Schaltflächen werden nicht horizontal zueinander angezeigt. Nur überlappend übereinander
So habe ich zwei Panels, eines, das die Etiketten und Textfelder (Toppane
) und eines, das die Tasten beherbergt, insgesamt 5 (bottomPane
). Ich versuche, die fünf Tasten über den unteren Rand der GUI zu drucken und habe eine harte Zeit. Ich fühle mich, als würde ich etwas Einfaches vermissen.
--------------------------------------------------------------
| label [textfield] |
| label [textField] |
| label [textfield] |
|-------------------------------------------------------------
| [button] [button] [button] [button] [button] |
--------------------------------------------------------------
Aber ich bekomme diese:
--------------------------------------------------------------
| label [textfield] |
| label [textField] |
| label [textfield] |
|-------------------------------------------------------------
| [ Button's 12345 ] |
--------------------------------------------------------------
Code:
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);
}
}
Persönlich fühle ich mich wie es etwas mit dem Layout-Manager zu tun, die ich verwende. Ich weiß nicht, ob ich es richtig für die richtige Anwendung verwende. Ich habe GridBagLayout
verwendet, und das ist alles, was ich bis jetzt für die Schule verwendet habe.
1) * "Ja, ich weiß, dass die Farbwahl ist hässlich ....aber das, was der Professor will, haha "* Das heißt nicht, dass du den Code, der hier gezeigt wird, einfärben musst. Du könntest ihn später einfügen, nachdem das Layoutproblem behoben wurde! 2) Erstelle ASCII-Kunst oder eine einfache Zeichnung von das * vorgesehene * Layout der GUI bei minimaler Größe, und wenn resizierbar, mit mehr Breite und Höhe 3) * "Ich habe' GridBagLayout' verwendet, und das ist alles, was ich bisher für die Schule verwendet habe. "* When all Sie haben einen Hammer, alles sieht wie ein Nagel aus.Verschiedene Layout-Manager sind gut für verschiedene Dinge, und es ist oft am einfachsten .. –
.. um eine GUI durch ** Kombinieren Layout-Manager ** zu erstellen. –
Ich habe ein wenig Kunst um zu zeigen, worüber ich spreche, haha, und ich dachte außerdem, dass gridbaglayout das automatisch tun würde, aber ich denke, ich werde mir andere Manager ansehen. –