我的JScroll窗格包含一个Jlist,并且是一个具有gridbag layour的JPanel的父级,但是它不希望适合它最终结果非常小并且就像它的gridx是-1而y是零,当它的ax应为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);
}
答案 0 :(得分: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);
}
}
}