0

我的 JScroll 窗格包含一个 Jlist,并由一个具有 gridbag 布局的 JPanel 作为父级,但它不想适合它最终会变得非常小并且就像它的 gridx 是 -1 和 y 是零一样,当它应该有轴为 0 和 y 为 1

代码:

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使用以下方法影响 的默认大小...

第一个将影响在视口中可见的行数,第二个将影响该行的高度和宽度。

您也没有将约束传递给滚动窗格的布局管理器(您正在尝试使用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 回答