【问题标题】:Swing: Place jTables and jButtonsSwing:放置 jTables 和 jButtons
【发布时间】:2013-05-17 19:28:19
【问题描述】:

我有一个带有 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);

【问题讨论】:

    标签: java swing layout jtable jbutton


    【解决方案1】:

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

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

    • 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();
      }
    }
    

    【讨论】:

    • 我得到了接受的答案,但你的比我的好,所以 +1 :)
    【解决方案2】:

    步骤:

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

    附言

    您会注意到我将panel1panel2 的布局从FlowLayout 更改为Y-Axis BoxLayout。这是因为按钮出现在表格旁边,而不是上方。将布局更改为 Y-Axis BoxLayout 解决了这个问题。

    【讨论】:

    • 我按照你的方法,但我可以看到按钮。请问你能看到我上面的代码吗?
    • @user2144555 我猜你的意思是你看不到按钮。看看我编辑的答案,我把你编辑的,现在可以工作的代码放在那里。
    • @user2144555 很高兴我能帮上忙 :)
    • 你能告诉我如何改变表格的大小吗?
    • @user2144555 我不是摇摆专家,但我相信setPreferredSize 应该可以胜任。 [这是一个链接。](docs.oracle.com/javase/6/docs/api/javax/swing/…
    猜你喜欢
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2018-10-21
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多