Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've rearranged your code and added in a GridBagLayout. I had to guess how you wanted to arrange your components.</p> <ol> <li><p>Always make sure that your Swing components are on the Event Dispatch thread. That's what the main method does.</p></li> <li><p>Separate your GUI components from your data definitions. This makes verifying your GUI components easier.</p></li> <li><p>Define your GUI components in the order that they are placed on the main JPanel.</p></li> <li><p>Always define a main JPanel. This gives you more flexibility in which layout manager(s) you want to use to define your GUI.</p></li> <li><p>Always use one or more layout managers.</p></li> </ol> <p>Here's my version of your code.</p> <pre><code>import java.awt.Component; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class Layouttest implements Runnable { protected Insets ogranicznik = new Insets(5, 10, 5, 5); @Override public void run() { JFrame ramka = new JFrame(); JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); JTextField txt = new JTextField("cos"); addComponent(panel, txt, 0, 0, 1, 1, ogranicznik, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL); JComboBox listaRozwijalna = new JComboBox(setDropBoxValues()); addComponent(panel, listaRozwijalna, 1, 0, 1, 1, ogranicznik, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL); JList lista = new JList(setListValues()); JScrollPane sp = new JScrollPane(lista); addComponent(panel, sp, 0, 1, 1, 1, ogranicznik, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL); ramka.add(panel); ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ramka.setSize(500, 500); ramka.pack(); ramka.setVisible(true); } protected void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets, int anchor, int fill) { GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0); container.add(component, gbc); } protected String[] setListValues() { String[] test = { "cos", "cos2", "cos3", "cos4", "cos5", "cos6", "cos7", "cos8", "cos9", "cos10", "cos11", "cos12" }; return test; } protected String[] setDropBoxValues() { String[] dropBoxList = { "uno", "due", "tre" }; return dropBoxList; } public static void main(String[] args) { SwingUtilities.invokeLater(new Layouttest()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload