Domanda

Sto cercando di fare un layout per la mia applet, ma non riesco a gestire un problema - alcuni degli elementi (ad esempio JCombobox) sono grandi quanto possono essere - prendere tutto in applet. La funzione Imposta non funziona. Cosa posso fare per renderli normali? (Alcuni elementi ad esempio jbutton e jlabels hanno dimensioni corrette).

Il mio codice:

JPanel cp=new JPanel();

String[] s = new String[2];
s[0] = "Price";
s[1] = "Name";

JComboBox c = new JComboBox(s);

JProgressBar pb=new JProgressBar(17, 23);
pb.setValue(20);
pb.setStringPainted(true);

JLabel l=new JLabel("Name of product");

JButton b=new JButton("Send a message");
b.setEnabled(true);

cp.add(c);
cp.add(pb);
cp.add(l);
cp.add(b);

GroupLayout layout = new GroupLayout(cp);
cp.setLayout(layout);

layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

layout.setHorizontalGroup(
   layout.createSequentialGroup()
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
           .addComponent(c)
           .addComponent(pb)
           .addComponent(l)
           .addComponent(b))
);
layout.setVerticalGroup(
   layout.createSequentialGroup()
      .addComponent(c)
      .addComponent(pb)
      .addComponent(l)
      .addComponent(b)
);

add(cp);
È stato utile?

Soluzione

Prova a usare il setPreferredSize() metodo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top