2016-04-12 13 views
1

Ich habe eine kleine GUI-Anwendung erstellt, die jetzt nur die Präsentationsschicht hat. Es erstellt die grundlegende GUI, (aber noch keine Logik hinzugefügt). Ich habe Probleme mit der Einrichtung von Steuerelementen/Komponenten wie Textfeldern und Schaltflächen.Wie man diese Komponenten auf JPanel einrichtet?

Hier ist der Code:

Main.java

public class Main { 

    public static void main(String[] args) { 
    // Make a new Client (TempConverter application) 
    Client client = new Client(); 
    } 

} 

Client.java

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class Client extends JFrame{ 

    private JPanel panel; 
    private JTextField inputTextBox; 
    private JTextField outputTextBox; 
    private JButton convertButton; 

    public Client(){ 
     panel = new JPanel(); 
     inputTextBox = new JTextField(6); 
     outputTextBox = new JTextField(6); 
     convertButton = new JButton("Convert!"); 
     ConstructGUI(); 
    } 

    private void ConstructGUI(){ 
     this.setTitle("Temerature Converter"); 
     this.setSize(300, 400); 
     PanelLayout(); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    private void PanelLayout(){ 
     this.add(panel); 
     panel.add(inputTextBox); 
     panel.add(outputTextBox); 
     panel.add(convertButton); 
    } 

} 

Die Komponenten alle nebeneinander erscheinen, und es ist nicht, dass ich anders erwartet, aber egal, welches Layout ich versuchte (es sei denn, ich habe es falsch gemacht), es ändert sich nicht.

Muss ich etwas vielleicht überschreiben?

+0

Was ist Ihr Wunsch-Design? – rdonuk

+0

Um sie übereinander gestapelt zu haben :) Danke für die Antwort! – katie1245

Antwort

1

Sie können BoxLayout verwenden, um sie übereinander gestapelt zu haben.

private void PanelLayout(){ 
    this.add(panel); 

    //next three lines aligning the components horizontally 
    inputTextBox.setAlignmentX(Component.CENTER_ALIGNMENT); 
    outputTextBox.setAlignmentX(Component.CENTER_ALIGNMENT); 
    convertButton.setAlignmentX(Component.CENTER_ALIGNMENT); 
    //aligning horizontally end. If you don't want the align them horizontally just remove these three lines. 

    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
    panel.add(Box.createVerticalGlue());//remove this line if you don't want to center them vertically 
    panel.add(inputTextBox); 
    panel.add(outputTextBox); 
    panel.add(convertButton); 
    panel.add(Box.createVerticalGlue());//remove this line if you don't want to center them vertically 
} 
1

Sie könnten einen GridBagLayout oder GridLayout je nach verwenden, was Sie erreichen wollen ...

GridBagLayout

public class Client extends JFrame { 

    private JPanel panel; 
    private JTextField inputTextBox; 
    private JTextField outputTextBox; 
    private JButton convertButton; 

    public Client() { 
     panel = new JPanel(new GridBagLayout()); 
     inputTextBox = new JTextField(6); 
     outputTextBox = new JTextField(6); 
     convertButton = new JButton("Convert!"); 
     ConstructGUI(); 
    } 

    private void ConstructGUI() { 
     this.setTitle("Temerature Converter"); 
     PanelLayout(); 
    } 

    private void PanelLayout() { 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 
     this.add(panel); 
     panel.add(inputTextBox, gbc); 
     panel.add(outputTextBox, gbc); 
     panel.add(convertButton, gbc); 
    } 

} 

Werfen Sie einen Blick auf Laying Out Components Within a Container für weitere Details.

Ich würde auch Sie davon abhalten, direkt von JFrame erstreckt, sondern haben, beginnen Sie JPanel stattdessen durch die Erweiterung, wird es Ihren Code zu entkoppeln und eine bessere Wiederverwendbarkeit bieten unter anderem

Verwandte Themen