Frage

Ich habe ein Applet zu schreiben, in der linken Seite ich eine Platte verwenden muß eine Liste der Fahrzeuge enthält, die eine Liste von Tasten sein können, was das Problem ist, die Anzahl der Fahrzeuge sind nicht gegeben !!! so, ich brauche Panel scrollen, wenn die Anzahl von Fahrzeugen zu viel ist,

ich dies tun für JFrame, aber es hat nicht mit Panel richtig funktioniert, bitte helfen Sie mir mit einem Beispiel

Sie den Code i-Scrolling-Panel verwenden:

 public class VehicleList extends JPanel {
    private ArrayList<VehicleReport> vehicles;
    private ArrayList<JButton> v_buttons =  new ArrayList<JButton>();



 public void showList(ArrayList<Vehicles> vehicles)
 {
   this.vehicles = vehicles;
   //...
    add(getScrollpane());
    setSize(155,300);

 }

public JScrollPane getScrollpane()
{
  JPanel panel = new JPanel();
  panel.setPreferredSize(new DimensionUIResource(150, 300));
   GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraint = new GridBagConstraints();
    panel.setLayout(gridbag);
    constraint.fill = GridBagConstraints.HORIZONTAL;
    JLabel title = new JLabel("Vehiles list");
    constraint.gridwidth = 2;
    constraint.gridx = 0;
    constraint.gridy = 0;
    constraint.ipady = 230;
    gridbag.setConstraints(title, constraint);
    panel.add(title);
    // end of set title
    constraint.gridwidth = 1;
    int i=1;

    for(JButton jb : v_buttons )
    {
        constraint.gridx =0;
        constraint.gridy = i;
        gridbag.setConstraints(jb, constraint);
        panel.add(jb);
        JLabel vehicle_lable = new JLabel("car" + i);
        constraint.gridx = 1;
        constraint.gridy = i;
        gridbag.setConstraints(vehicle_lable, constraint);
        panel.add(vehicle_lable);
        i++;
    }
JScrollPane jsp = new JScrollPane(panel);
 return jsp;

}

}

in jaframe nach JScrollPane in dem JFrame ich diesen Ort

pack ();

setSize (250, 250);

setLocation (100, 300);

und es funktioniert gut !!!!

War es hilfreich?

Lösung

Sie zeigen auch nicht, uns den Layout-Manager des VehicleList JPanel. Für den Fall, setzen Sie es nicht, wird standardmäßig Flowlayout, im Gegensatz zu JFrame (die Sie erwähnt dies in nicht funktioniert), deren Inhaltsfenster standardmäßig Border. Vielleicht brauchen Sie nur aus dem entsprechenden Code zu ändern:

//...
add(getScrollpane());

//...
setLayout(new BorderLayout());
add(getScrollpane(), BorderLayout.CENTER);

Andere Tipps

Sie müssen die Scrolling-Politik auf dem horizontalen setzen und vertikal:

public void setHorizontalScrollBarPolicy(int policy)
public void setVerticalScrollBarPolicy(int policy)

Mit:

JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS 

Und:

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
JScrollPane.VERTICAL_SCROLLBAR_NEVER
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS 

So zum Beispiel:

jscrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top