作为Swing的新手,我不确定使用哪种布局来设计以下设计。请建议我。 我尝试使用GridLayout,但它将框架分成相等的部分,这会混淆按钮区域。我读到了使用GridBagLayout,但在Swing中是全新的,我不确定它是否会起作用。由于我在这个项目上有很多工作而且时间很少,所以我不想浪费时间。所以寻找一个简单而有保障的解决方案。 请建议我一个简单的解决方案。
设计:框架的上半部分有两个编辑器(每个编辑器占据框架的一半宽度)。接下来它将在中心有一个按钮。按钮后面将是两个可滚动的表格(每个表格占据框架宽度的一半)。
编辑:谢谢大家。所以我从GridBagLayout开始,我接近我想要的。唯一的问题是我希望第二行只有一个提交按钮,但由于某种原因我的提交按钮嵌入到第三行的可滚动表中。基本上第二行根本没有出现。以下是我写的代码。请建议我更正。
class SplitPane扩展了JFrame {
private static JPanel panel2;
private static JPanel panel5;
private static JScrollPane panel3;
private static JScrollPane panel4;
protected JSplitPane split;
public SplitPane(JChemPaintPanel p){
JFrame f = new JFrame("");
//f.setLayout(new GridLayout(3, 2, 10, 10 ));
f.addWindowListener(new JChemPaintPanel.AppCloser());
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(800, 800));
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=0;
c.fill=GridBagConstraints.BOTH;
c.weightx=0.3;
c.weighty=0.3;
p.setPreferredSize( new Dimension( 200, 200 ) );
p.setMaximumSize(new Dimension(300, 300));
pane.add(p, c);
c.gridx=1;
c.gridy=0;
c.fill=GridBagConstraints.BOTH;
c.weightx=0.3;
c.weighty=0.3;
pane.add(createPanel2(), c);
c.gridx=0;
c.gridy=1;
c.gridheight=2;
c.gridwidth=0;
c.fill=GridBagConstraints.NONE;
//c.weightx=0.3;
//c.weighty=0.3;
pane.add(createPanel5(), c);
c.gridx=0;
c.gridy=2;
c.gridheight=1;
c.gridwidth=1;
c.fill=GridBagConstraints.BOTH;
c.weightx=0.3;
c.weighty=0.3;
pane.add(createPanel3(), c);
c.gridx=1;
c.gridy=2;
c.gridheight=1;
c.gridwidth=1;
c.fill=GridBagConstraints.BOTH;
c.weightx=0.3;
c.weighty=0.3;
pane.add(createPanel4(), c);
f.add(pane);
f.pack();
Point point = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
int w2 = 1000;
int h2 = 1000;
f.setLocation(point.x - w2, point.y - h2);
f.setVisible(true);
}
//top right
public static JPanel createPanel2(){
panel2 = new JPanel();
panel2.setLayout( new BorderLayout() );
panel2.add((new TextArea("Panel2")));
panel2.setPreferredSize( new Dimension( 200, 200 ) );
panel2.setMaximumSize(new Dimension(300, 300));
return panel2;
}
public static JPanel createPanel5(){
panel5 = new JPanel();
JButton b1 = new JButton("Submit");
panel5.add(b1);
return panel5;
}
//bottom left
public static JScrollPane createPanel3(){
Label label_prop = new Label("Properties:", Label.LEFT);
String[] columnNames = {"Properties","",};
Object[][] data = {
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""}
};
JTable table = new JTable(data, columnNames);
table.setBackground(Color.LIGHT_GRAY);
table.setRowHeight(20);
table.setBorder(BasicBorders.getMenuBarBorder());
panel3 = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
panel3.add(new JButton("Submit"));
panel3.add(label_prop);
panel3.setPreferredSize( new Dimension( 5, 5 ) );
panel3.setMinimumSize( new Dimension( 5, 5 ) );
return panel3;
}
//bottom right
public static JScrollPane createPanel4(){
panel4 = new JScrollPane();
String[] columnNames = {"Activities","",};
Object[][] data = {
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""},
{"", "",}, {"", ""}, {"", ""},{"", ""}
};
JTable table = new JTable(data, columnNames);
table.setBackground(Color.LIGHT_GRAY);
table.setRowHeight(20);
table.setBorder(BasicBorders.getMenuBarBorder());
panel4 = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
panel4.setPreferredSize( new Dimension( 5, 5 ) );
panel4.setMinimumSize( new Dimension( 5, 5 ) );
return panel4;
}
}
答案 0 :(得分:0)
看看A Visual Guide to Layout Managers,有一些示例GUI使用布局管理器,并告诉您在哪里可以找到每个布局管理器的操作方法页面。这非常有帮助。