2017-07-24 1 views
3

Ich habe drei JButton s, zwei JLabel s und eine JTextField in meinem Programm und ein JPanel mit FlowLayout zu arrangieren. Ich möchte drei Tasten am Anfang der JPanel und der Rest am Ende der JPanel anordnen. Ich brauche Platz zwischen den drei Tasten und den letzten drei Komponenten, aber der Code funktioniert nicht. Kann mir jemand helfen?Wie JComponents oben und unten von JPanel

Dies ist der Code:

JPanel panelMenu=new Jpanel(new FlowLayout()) 
panelMenu.add (btnOpen); 
panelMenu.add (lblPage); 
panelMenu.add (txtCurrentPage); 
panelMenu.add (lblTotalPage); 
panelMenu.add (btnBackword); 
panelMenu.add (btnNext); 
getContentPanel.add(panelMenu); 
+3

Das Tutorial ist der richtige Ort zu starten: https://docs.oracle.com/javase/tutorial/uiswing/ –

Antwort

2

Die FlowLayout Klasse stellt Komponenten in einer Reihe. Um zu bekommen, was Sie wollen, können Sie GridBagLayout verwenden.

Beispielcode:

import java.awt.CardLayout; 
import java.awt.Color; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

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

public class LayoutExamle { 
    JFrame frame; 
    JPanel panel; 
    JButton b1, b2, b3; 
    JLabel l1, l2; 
    JTextField t1; 

    GridBagConstraints constraints; 

    public LayoutExamle(){ 
     initComp(); 
    } 

    public void initComp(){ 
     frame = new JFrame("Example"); 
     frame.setLayout(new CardLayout()); 

     panel = new JPanel(); 

     panel.setSize(400, 350); 
     panel.setLayout(new GridBagLayout()); 
     panel.setBackground(Color.WHITE); 

     constraints = new GridBagConstraints(); 

     b1 = new JButton("<<"); 
     constraints.gridx = 0; 
     constraints.gridy = 0; 
     constraints.gridwidth = 2; 
     constraints.anchor = GridBagConstraints.NORTHWEST; 
     constraints.insets = new Insets(12, 43, 0, 0); 
     panel.add(b1, constraints); 

     b2 = new JButton("Open"); 
     constraints.gridx = 2; 
     constraints.gridy = 0; 
     constraints.gridwidth = 2; 
     constraints.anchor = GridBagConstraints.NORTHWEST; 
     constraints.insets = new Insets(12, 18, 0, 0); 
     panel.add(b2, constraints); 

     b3 = new JButton(">>"); 
     constraints.gridx = 4; 
     constraints.gridy = 0; 
     constraints.anchor = GridBagConstraints.NORTHWEST; 
     constraints.insets = new Insets(12, 12, 0, 0); 
     panel.add(b3, constraints); 

     l1 = new JLabel("jLabel1"); 
     constraints.gridx = 0; 
     constraints.gridy = 1; 
     constraints.anchor = GridBagConstraints.NORTHWEST; 
     constraints.insets = new Insets(234, 43, 12, 0); 
     panel.add(l1, constraints); 

     l2 = new JLabel("jLabel1"); 
     l2.setText("jLabel2"); 
     constraints.gridx = 2; 
     constraints.gridy = 1; 
     constraints.anchor = GridBagConstraints.NORTHWEST; 
     constraints.insets = new Insets(234, 18, 12, 0); 
     panel.add(l2, constraints); 

     t1 = new JTextField(); 
     constraints.gridx = 4; 
     constraints.gridy = 1; 
     constraints.gridwidth = 2; 
     constraints.ipadx = 130; 
     constraints.anchor = GridBagConstraints.NORTHWEST; 
     constraints.insets = new Insets(230, 12, 12, 31); 
     panel.add(t1, constraints); 

     frame.add(panel); 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     new LayoutExamle(); 
    } 
} 

Screenshot:

enter image description here

+0

danke dein B Ich habe die Antwort – Meshu

+1

@Meshu So können Sie diese Antwort akzeptieren, wenn es für Sie funktioniert, so dass andere auch dies hilfreich finden. Ich freue mich jedoch, Ihnen zu helfen. – Blasanka

1

Eine Möglichkeit ist, zwei Platten zu schaffen. Dann können Sie die Platten auf den Inhaltsbereich des Rahmens standardmäßig die hinzufügen, verwendet einen Border:

frame.add(topPanel, BorderLayout.PAGE_START); 
frame.add(bottomPanel, BorderLayout.PAGE_END);