0

我有一个带有 2 个 jTables 的 jFrame(插入到 2 个 jScrollPanes 中)。然后,每个 jTable 都有 3 个 jButton。我怎样才能让它们得到以下结果:

在此处输入图像描述

我不太清楚使用什么 Layout 来管理它。谢谢!


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

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);
4

2 回答 2

4

编辑:嘎!有人打败了我……无论如何,这是一种略有不同的方法!

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

  • 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

使用BoxLayoutandBorderLayout.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();
  }
}

两桌布局

于 2013-05-22T17:15:48.823 回答
3

脚步:

  1. 给主 JFrame 一个 GridLayout,它有两列和一行。
  2. 添加到这个 JFrame 2 JPanel。
  3. 这 2 个面板各有一个FlowLayout Y 轴 BoxLayout。
  4. 向这 2 个面板中的每一个添加一个 JPanel,其中包含您的 3 个按钮。
  5. 将表格添加到 2 个面板中的每一个。

这就是为什么你看不到按钮,你忘了这样做:

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);

附言

您会注意到我将panel1&panel2从的布局更改FlowLayoutY-Axis BoxLayout. 这是因为按钮出现在表格旁边,而不是上方。将布局更改为Y-Axis BoxLayout固定的。

于 2013-05-22T16:30:08.090 回答