0

私の JScroll ペインには Jlist が含まれており、gridbag レイアウトを持つ JPanel を親にしていますが、正しく収まるようにしたくありません。最終的には非常に小さくなり、gridx が -1 で y がゼロのように動作します。 0 の ax と 1 の y

コード:

public class StartGui 
{
private static JFrame frame = new JFrame("");
private static JPanel panel = new JPanel();
private static JMenuBar menu = new JMenuBar();
private static DefaultListModel<String> listModel = new DefaultListModel<String>();
private static JList<String> Targets = new JList<String>(listModel);
private static JButton Start = new JButton("Start");
private static JButton Stop = new JButton("Stop");
private static JButton Configure = new JButton("Configure");
private static JButton AddRecipiants = new JButton("Add Recipiants");
private static JProgressBar Progress = new JProgressBar();
private static JLabel RLable = new JLabel("Recipiants:");

private static JScrollPane lsp;

private static GridBagLayout gbl = new GridBagLayout();
private static GridBagConstraints gbc = new GridBagConstraints();

private static JFrame emails = new JFrame();
private static JPanel panel2 = new JPanel();
private static JTextArea emailA = new JTextArea("type email here");
private static JButton done = new JButton("OK");

public static void main(String[] args)
{
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setJMenuBar(menu);
    frame.setResizable(false);
    frame.setBounds(0, 0, 500, 400);
    panel.setLayout(gbl);

    listModel.addElement("TestEmailAddress@fake.com");
    lsp = new JScrollPane(Targets);

    emails.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    emails.setResizable(false);
    emails.setBounds(0, 0, 200, 300);
    panel2.setLayout(gbl);
    emails.add(panel2);

    gbc.gridx = 0;
    gbc.gridy = 17;
    gbc.gridwidth = 20;
    gbc.gridheight = 3;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 0;
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.insets = new Insets(5,70,5,70);
    gbl.setConstraints(done, gbc);
    panel2.add(done);

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 20;
    gbc.gridheight = 17;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.insets = new Insets(10,10,0,10);
    gbl.setConstraints(emailA, gbc);
    panel2.add(emailA);

    emails.add(panel2);

    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 10;
    gbc.gridheight = 19;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(5,5,5,5);
    gbl.setConstraints(Targets, gbc);
    panel.add(lsp);

    gbc.gridx = 10;
    gbc.gridy = 11;
    gbc.gridwidth = 5;
    gbc.gridheight = 5;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.insets = new Insets(25,5,25,5);
    gbl.setConstraints(Start, gbc);
    panel.add(Start);

    gbc.gridx = 15;
    gbc.gridy = 11;
    gbc.gridwidth = 5;
    gbc.gridheight = 5;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.insets = new Insets(25,5,25,5);
    gbl.setConstraints(Stop, gbc);
    panel.add(Stop);

    gbc.gridx = 10;
    gbc.gridy = 4;
    gbc.gridwidth = 10;
    gbc.gridheight = 7;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.insets = new Insets(35,5,40,5);
    gbl.setConstraints(Configure, gbc);
    panel.add(Configure);

    gbc.gridx = 10;
    gbc.gridy = 0;
    gbc.gridwidth = 10;
    gbc.gridheight = 4;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.insets = new Insets(25,5,20,5);
    gbl.setConstraints(AddRecipiants, gbc);
    panel.add(AddRecipiants);

    Progress.setMaximum(100);
    Progress.setString("0%");
    Progress.setValue(0);
    gbc.gridx = 10;
    gbc.gridy = 16;
    gbc.gridwidth = 10;
    gbc.gridheight = 4;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.insets = new Insets(25,5,15,5);
    gbl.setConstraints(Progress, gbc);
    panel.add(Progress);

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 10;
    gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.insets = new Insets(5,45,0,45);
    gbl.setConstraints(RLable, gbc);
    panel.add(RLable);

    frame.add(panel);
    frame.setVisible(true);
}
4

1 に答える 1

3

通常、JScrollPane適切なサイズはそれほど大きくなく、通常はScrollableインターフェイス (およびその他の要因)から決定されます。

JList次の方法を使用して、 のデフォルト サイズに影響を与えることができます...

1 つ目はビュー ポートに表示される行数に影響し、2 つ目はその行の高さと幅に影響します。

また、スクロール ペインのレイアウト マネージャーに制約を渡していません (を使用しようとしていますJList)。これは機能しません...

あなたはこれをしているはずです...

panel.add(lsp, gbc);

そして、私はこれを大いに落胆させます...

gbl.setConstraints(Targets, gbc);

(追加する必要があります。一般的に上記を思いとどまらせ、使用をお勧めしadd(Component, Object)ます-しかし、それは私です)

更新しました

私の提案、対あなたのデフォルトコード...

ここに画像の説明を入力

public class BadLayout10 {

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

  public BadLayout10() {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
        }
        new StartGui();
      }
    });
  }

  public class StartGui {

    private JFrame frame = new JFrame("");
    private JPanel panel = new JPanel();
    private JMenuBar menu = new JMenuBar();
    private DefaultListModel<String> listModel = new DefaultListModel<String>();
    private JList<String> Targets = new JList<String>(listModel);
    private JButton Start = new JButton("Start");
    private JButton Stop = new JButton("Stop");
    private JButton Configure = new JButton("Configure");
    private JButton AddRecipiants = new JButton("Add Recipiants");
    private JProgressBar Progress = new JProgressBar();
    private JLabel RLable = new JLabel("Recipiants:");
    private JScrollPane lsp;
    private GridBagLayout gbl = new GridBagLayout();
    private GridBagConstraints gbc = new GridBagConstraints();
    private JFrame emails = new JFrame();
    private JPanel panel2 = new JPanel();
    private JTextArea emailA = new JTextArea("type email here");
    private JButton done = new JButton("OK");

    public StartGui() {

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setJMenuBar(menu);
      frame.setResizable(false);
      frame.setBounds(0, 0, 500, 400);
      panel.setLayout(gbl);

      listModel.addElement("TestEmailAddress@fake.com");
      Targets.setVisibleRowCount(10);
      Targets.setPrototypeCellValue("This is a really long test string");
      lsp = new JScrollPane(Targets);

      emails.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      emails.setResizable(false);
      emails.setBounds(0, 0, 200, 300);
      panel2.setLayout(gbl);
      emails.add(panel2);

      gbc.gridx = 0;
      gbc.gridy = 17;
      gbc.gridwidth = 20;
      gbc.gridheight = 3;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 1;
      gbc.weighty = 0;
      gbc.anchor = GridBagConstraints.NORTH;
      gbc.insets = new Insets(5, 70, 5, 70);
      gbl.setConstraints(done, gbc);
      panel2.add(done);

      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.gridwidth = 20;
      gbc.gridheight = 17;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 1;
      gbc.weighty = 1;
      gbc.anchor = GridBagConstraints.NORTH;
      gbc.insets = new Insets(10, 10, 0, 10);
      gbl.setConstraints(emailA, gbc);
      panel2.add(emailA);

      emails.add(panel2);

      gbc.gridx = 0;
      gbc.gridy = 1;
      gbc.gridwidth = 10;
      gbc.gridheight = 19;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 1;
      gbc.weighty = 1;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.insets = new Insets(5, 5, 5, 5);
//      gbl.setConstraints(Targets, gbc);
      panel.add(lsp, gbc);

      gbc.gridx = 10;
      gbc.gridy = 11;
      gbc.gridwidth = 5;
      gbc.gridheight = 5;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 1;
      gbc.weighty = 1;
      gbc.anchor = GridBagConstraints.CENTER;
      gbc.insets = new Insets(25, 5, 25, 5);
      gbl.setConstraints(Start, gbc);
      panel.add(Start);

      gbc.gridx = 15;
      gbc.gridy = 11;
      gbc.gridwidth = 5;
      gbc.gridheight = 5;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 1;
      gbc.weighty = 1;
      gbc.anchor = GridBagConstraints.CENTER;
      gbc.insets = new Insets(25, 5, 25, 5);
      gbl.setConstraints(Stop, gbc);
      panel.add(Stop);

      gbc.gridx = 10;
      gbc.gridy = 4;
      gbc.gridwidth = 10;
      gbc.gridheight = 7;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 1;
      gbc.weighty = 1;
      gbc.anchor = GridBagConstraints.CENTER;
      gbc.insets = new Insets(35, 5, 40, 5);
      gbl.setConstraints(Configure, gbc);
      panel.add(Configure);

      gbc.gridx = 10;
      gbc.gridy = 0;
      gbc.gridwidth = 10;
      gbc.gridheight = 4;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 1;
      gbc.weighty = 1;
      gbc.anchor = GridBagConstraints.CENTER;
      gbc.insets = new Insets(25, 5, 20, 5);
      gbl.setConstraints(AddRecipiants, gbc);
      panel.add(AddRecipiants);

      Progress.setMaximum(100);
      Progress.setString("0%");
      Progress.setValue(0);
      gbc.gridx = 10;
      gbc.gridy = 16;
      gbc.gridwidth = 10;
      gbc.gridheight = 4;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 1;
      gbc.weighty = 1;
      gbc.anchor = GridBagConstraints.CENTER;
      gbc.insets = new Insets(25, 5, 15, 5);
      gbl.setConstraints(Progress, gbc);
      panel.add(Progress);

      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.gridwidth = 10;
      gbc.gridheight = 1;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 1;
      gbc.weighty = 1;
      gbc.anchor = GridBagConstraints.CENTER;
      gbc.insets = new Insets(5, 45, 0, 45);
      gbl.setConstraints(RLable, gbc);
      panel.add(RLable);

      frame.add(panel);
      frame.setVisible(true);
    }
  }
}
于 2013-01-17T02:05:02.733 に答える