2016-08-29 3 views
3

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.

+2

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 .. –

+1

.. um eine GUI durch ** Kombinieren Layout-Manager ** zu erstellen. –

+1

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. –

Antwort

3

Ihr Problem ist, dass Sie die gleiche Einschränkung verwenden c für jede Ihrer neuen Schaltflächen:

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

Die letzte Änderung Sie c aus oben war, als Sie tat:

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

Und dann verwenden Sie einfach dieselbe Einschränkung für alle 5 neuen Schaltflächen und fügen sie alle zum selben Gitterplatz hinzu.

Sie müssen die Einschränkungen entsprechend einstellen (z. B. c Werte, Sie haben diese Streuner unbenutzt b dort auch) für jeden einzelnen.

+0

Ja, ich habe es gerade gemerkt, ich werde sehen, ob ich es so hinbekomme. Ich habe das Layout gewechselt und es funktioniert jetzt Wunder –

2

Ok, also habe ich die Bodenplatte zu einem Boxlayout und habe die Tasten horizontal!

Code mit Fix!

/* 
* 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

Beachten Sie, dass Sie 'b' setzen, aber immer noch' c' zu 'add()' übergeben, obwohl es wahrscheinlich keine Rolle spielt, da Sie jetzt ein 'BoxLayout' verwenden . Immer noch nicht schlampig! –

+0

Ich ging zurück und repariere das! Ich wollte sehen, wie es funktioniert und es hat auch das Gleiche gemacht. –

+0

Ich würde einfach ein Panel mit einem 'FlowLayout' verwenden. Das 'FlowLayout' gibt automatisch 5 Pixel Abstand zwischen den Komponenten. – camickr

0

Sie müssen Flow-Layout zu Ihrem bottomPane verwenden. Ist besser als GridLayout für diesen Zweck.

3

GridBagLayout ist veraltet. Und es ist ein Schmerz, mit ihm zu arbeiten. Mit einem modernen Layout-Manager wie MigLayout kann Ihr Codebeispiel sehr schnell erstellt werden.

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); 
     }); 
    } 
} 

Das Layout wird mit einer Kombination verschiedener Einschränkungen erstellt. Sobald Sie dies lernen, sind die meisten praktischen Layouts ein Kinderspiel. Sie können mehr über den Manager website herausfinden.

Screenshot:

Screenshot of the example

+0

Wow, das ist großartig! Danke für den Tipp! –

Verwandte Themen