Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JScrollPane不会和gridbag一起进入JPanel_Java_Swing_Jscrollpane_Layout Manager_Gridbaglayout - Fatal编程技术网

Java JScrollPane不会和gridbag一起进入JPanel

Java JScrollPane不会和gridbag一起进入JPanel,java,swing,jscrollpane,layout-manager,gridbaglayout,Java,Swing,Jscrollpane,Layout Manager,Gridbaglayout,我的JScroll窗格包含一个Jlist,它的父对象是一个具有gridbag布局的JPanel,但它不想合适,它最终非常小,就像它的gridx是-1,y是零,而它的x应该是0,y应该是1 代码: 公共类StartGui { 私有静态JFrame=新JFrame(“”); 私有静态JPanel panel=newjpanel(); 私有静态JMenuBar菜单=新JMenuBar(); 私有静态DefaultListModel listModel=新DefaultListModel(); 私有静态

我的JScroll窗格包含一个Jlist,它的父对象是一个具有gridbag布局的JPanel,但它不想合适,它最终非常小,就像它的gridx是-1,y是零,而它的x应该是0,y应该是1

代码:

公共类StartGui
{
私有静态JFrame=新JFrame(“”);
私有静态JPanel panel=newjpanel();
私有静态JMenuBar菜单=新JMenuBar();
私有静态DefaultListModel listModel=新DefaultListModel();
私有静态JList目标=新JList(listModel);
私有静态JButton Start=新JButton(“Start”);
私有静态JButton Stop=新JButton(“Stop”);
私有静态JButton Configure=新JButton(“Configure”);
私有静态JButton AddRecipiants=新JButton(“AddRecipiants”);
private static JProgressBar Progress=new JProgressBar();
私有静态JLabel RLable=新JLabel(“Recipients:”);
私有静态JScrollPane lsp;
私有静态GridBagLayout gbl=新GridBagLayout();
私有静态GridBagConstraints gbc=新GridBagConstraints();
私有静态JFrame电子邮件=新JFrame();
私有静态JPanel panel2=新的JPanel();
私有静态JTextArea emailA=新JTextArea(“在此处键入电子邮件”);
私有静态JButton done=新JButton(“确定”);
公共静态void main(字符串[]args)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(菜单);
frame.setresizeable(false);
机架立根(0,0,500,400);
面板设置布局(gbl);
listModel.addElement(“TestEmailAddress@fake.com");
lsp=新的JScrollPane(目标);
emails.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
电子邮件。可设置大小(错误);
电子邮件。挫折(0,0,200,300);
面板2.设置布局(gbl);
电子邮件。添加(第2组);
gbc.gridx=0;
gbc.gridy=17;
gbc.gridwidth=20;
gbc.gridheight=3;
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=1;
gbc.weighty=0;
gbc.anchor=gridbag.NORTH;
gbc.插图=新插图(5,70,5,70);
gbl.setConstraints(完成,gbc);
第2组:添加(完成);
gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=20;
gbc.gridheight=17;
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=1;
gbc.weighty=1;
gbc.anchor=gridbag.NORTH;
gbc.插图=新插图(10,10,0,10);
gbl.setConstraints(emailA,gbc);
第2组。添加(emailA);
电子邮件。添加(第2组);
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.插图=新插图(5,5,5,5);
gbl.setConstraints(目标,gbc);
添加面板(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.插图=新插图(25,5,25,5);
gbl.setConstraints(开始,gbc);
面板。添加(开始);
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.插图=新插图(25,5,25,5);
gbl.设置约束(停止,gbc);
面板。添加(停止);
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.插图=新插图(35,5,40,5);
gbl.setConstraints(配置,gbc);
panel.add(配置);
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.插图=新插图(25,5,20,5);
gbl.setConstraints(addRecipients,gbc);
panel.add(addRecipients);
进度。设定最大值(100);
进度。设置字符串(“0%”;
进度。设置值(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.插图=新插图(25,5,15,5);
gbl.setConstraints(进度,gbc);
小组.增补(进度);
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.插图=新插图(5,45,0,45);
gbl.setConstraints(RLable,gbc);
面板。添加(RLable);
框架。添加(面板);
frame.setVisible(true);
}

首选的
JScrollPane
大小通常不是很大,通常是通过
可滚动的
界面(以及其他因素)确定的

您可以使用以下方法影响
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);
    }
  }
}

公共类BadLayout10{
公共静态void main(字符串[]args){
新的布局10();
}
公共广告版面10(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
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);
    }
  }
}