Swing:放置jTables和jButton

时间:2013-05-22 16:25:17

标签: java swing layout jtable jbutton

我有一个带有2个jTables的jFrame(插入2个jScrollPanes中)。然后,我为每个jTable提供了3个jButton。如何放置它们以获得以下结果:

enter image description here

我不太清楚用于管理它的布局。 谢谢!


我有这个,但我看不到按钮:

JButton addButton1 = new JButton();           
JButton deleteButton1 = new JButton();
JButton playButton1 = new JButton();
JButton addButton2 = new JButton();           
JButton deleteButton2 = new JButton();
JButton playButton2 = new JButton();


JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));

JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.setBorder(javax.swing.BorderFactory.createTitledBorder("List 1"));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setBorder(javax.swing.BorderFactory.createTitledBorder("List 2"));    

JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(addButton1);
panel3.add(deleteButton1);
panel3.add(playButton1);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.add(addButton2);
panel4.add(deleteButton2);
panel4.add(playButton2);

JScrollPane tableContainer1 = new JScrollPane(table1);
panel1.add(tableContainer1, BorderLayout.CENTER);
JScrollPane tableContainer2 = new JScrollPane(table2);
panel2.add(tableContainer2, BorderLayout.CENTER);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());

frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);

frame.pack();   
frame.setLocationRelativeTo(null);
frame.setVisible(true);

2 个答案:

答案 0 :(得分:4)

编辑: Gah!有人打败了我......无论如何,这里的方法略有不同!

有很多方法可以做到这一点,但我会尝试以下方法:

  • JFrame> BoxLayout使用X_AXIS
    • JPanel#1> BorderLayout
      • [BorderLayout.NORTH] JPanel用于按钮> FlowLayout使用FlowLayout.LEFT
      • [BorderLayout.CENTER] JScrollPane与表#1
    • JPanel#2> BorderLayout
      • [BorderLayout.NORTH] JPanel用于按钮> FlowLayout使用FlowLayout.LEFT
      • [BorderLayout.CENTER] JScrollPane与表#2

使用BoxLayoutBorderLayout.CENTER将确保表格随框架调整大小并尽可能多地填充空间。 这是一个简单的例子:

public class TwoTableJFrameTest extends JFrame
{
  public TwoTableJFrameTest()
  {
    setTitle("Two Table Layout");
    setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));

    JPanel table1Panel = new JPanel(new BorderLayout(5, 5));

    JPanel table1ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    table1ButtonPanel.add(new JButton("Button 1"));
    table1ButtonPanel.add(new JButton("Button 2"));
    table1ButtonPanel.add(new JButton("Button 3"));

    JTable table1 = new JTable(new DefaultTableModel(new Object[]{"Column 1", "Column 2"}, 10));

    table1Panel.add(table1ButtonPanel, BorderLayout.NORTH);
    table1Panel.add(new JScrollPane(table1));

    JPanel table2Panel = new JPanel(new BorderLayout(5, 5));

    JPanel table2ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    table2ButtonPanel.add(new JButton("Button 1"));
    table2ButtonPanel.add(new JButton("Button 2"));
    table2ButtonPanel.add(new JButton("Button 3"));

    JTable table2 = new JTable(new DefaultTableModel(new Object[]{"Column 1", "Column 2"}, 10));

    table2Panel.add(table2ButtonPanel, BorderLayout.NORTH);
    table2Panel.add(new JScrollPane(table2));

    add(table1Panel);
    add(table2Panel);

    pack();
  }
}

two table layout

答案 1 :(得分:3)

<强>步骤:

  1. 为主JFrame提供一个包含两列和一行的GridLayout。
  2. 添加到此JFrame 2 JPanels。
  3. 这两个面板将各自具有 FlowLayout Y轴BoxLayout。
  4. 为这两个面板中的每一个添加一个包含3个按钮的JPanel。
  5. 将表格添加到两个面板中的每一个。
  6. 这就是为什么你看不到按钮,你忘了这样做:

    panel1.add(panel3);
    panel2.add(panel4);
    

    无论如何,这是工作代码:

    JButton addButton1 = new JButton();
    JButton deleteButton1 = new JButton();
    JButton playButton1 = new JButton();
    JButton addButton2 = new JButton();
    JButton deleteButton2 = new JButton();
    JButton playButton2 = new JButton();
    
    
    JFrame frame = new JFrame();
    frame.setLayout(new GridLayout(1, 2));
    
    JPanel panel1 = new JPanel();
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
    panel1.setBorder(javax.swing.BorderFactory.createTitledBorder("List 1"));
    JPanel panel2 = new JPanel();
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
    panel2.setBorder(javax.swing.BorderFactory.createTitledBorder("List 2"));
    
    JPanel panel3 = new JPanel();
    panel3.setLayout(new FlowLayout());
    panel3.add(addButton1);
    panel3.add(deleteButton1);
    panel3.add(playButton1);
    JPanel panel4 = new JPanel();
    panel4.setLayout(new FlowLayout());
    panel4.add(addButton2);
    panel4.add(deleteButton2);
    panel4.add(playButton2);
    
    panel1.add(panel3);
    panel2.add(panel4);
    
    JScrollPane tableContainer1 = new JScrollPane(table1);
    panel1.add(tableContainer1, BorderLayout.CENTER);
    JScrollPane tableContainer2 = new JScrollPane(table2);
    panel2.add(tableContainer2, BorderLayout.CENTER);
    
    
    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new FlowLayout());
    
    frame.getContentPane().add(panel1);
    frame.getContentPane().add(panel2);
    
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    

    <强> P.S。

    你会注意到我改变了panel1&amp;的布局从panel2FlowLayout的{​​{1}}。这是因为按钮出现在桌子旁边,而不是在上面。将布局更改为固定的Y-Axis BoxLayout