【问题标题】:How to position Java swing components when there is a JScrollPane有 JScrollPane 时如何定位 Java swing 组件
【发布时间】:2020-12-10 23:05:43
【问题描述】:

我是制作 GUI 和 Java Swing 的新手,我正在尝试制作一个显示 SQL 数据库中的表的 GUI。该表使用 JScrollPane 显示。起初我以为我的其他组件(JLabel 和 JTextField)没有被添加到内容窗格中,但实际上它们只是隐藏在 ScrollPane 下。减小 ScrollPane 的尺寸后,现在这些其他组件会显示出来,但它们无法使用 setBounds 方法定位,并且始终出现在同一位置,因此最后添加的组件完全覆盖了其他组件。除了代码之外,我还附上了 GUI 外观的屏幕截图。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

public class LibraryAppGUI extends JFrame {
    String sql;
    String DB_PATH = LibraryAppGUI.class.getResource("LibraryManagement3.sqlite").getFile();
    
    private JTable table;
    private String columns[] = {"PatronFirstName", "PatronLastName"};
    private TableModelListener tableModelListener;
    
    public LibraryAppGUI () {
        DefaultTableModel model = new DefaultTableModel(columns, 0);
        table = new JTable(model);
        
        try{populateSQL(table);} catch(Exception e1) {e1.printStackTrace();}
        table.setCellSelectionEnabled(true);
        table.setPreferredScrollableViewportSize(new Dimension(600, 300));
        table.setFillsViewportHeight(false);
        
        
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setVisible(true);
        getContentPane().add(scrollPane, BorderLayout.PAGE_START);
    }
    

    
    public void createSQL() throws ClassNotFoundException, SQLException {
        Class.forName("org.sqlite.jdbc");
        Connection connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
        PreparedStatement stmt = connection.prepareStatement("");
        
    }
    public void populateSQL(JTable table) throws ClassNotFoundException, SQLException {
        sql = "select PatronFirstName, PatronLastName\r\n" + 
                "FROM Patron\r\n";
        Class.forName("org.sqlite.JDBC");
        Connection connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
        PreparedStatement stmt = connection.prepareStatement(sql);
        ResultSet res = stmt.executeQuery();
        
        while(res.next()) {
            Object[] row = new Object[columns.length];
            for (int i = 1; i <= columns.length; i++) {
                row[i-1] = res.getObject(i);
            }
            ((DefaultTableModel) table.getModel()).insertRow(res.getRow()-1, row);  
        }
        res.close();
        connection.close();
    }
    
    
    public static void main(String[] args) {
        LibraryAppGUI window = new LibraryAppGUI();
        //label to prompt user
        JLabel welcome = new JLabel("Welcome to the library. Choose your patron: ");
        welcome.setBounds(50,50, 100, 30);
        window.getContentPane().add(welcome);
        
        JTextField user = new JTextField("Enter the full name in this box.");
        user.setBounds(150,150,100,30);
        window.getContentPane().add(user);
        
        window.setDefaultCloseOperation(EXIT_ON_CLOSE);
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        
    }
} 

【问题讨论】:

  • 图片只能在上传到本网站而非第三方网站时显示。

标签: java swing user-interface jscrollpane


【解决方案1】:

为什么要在两个不同的地方创建 Swing 组件?

  1. 不要在 main() 方法中创建组件。
  2. 标签、文本字段和表格都应在构造函数中创建并添加到框架中,与创建和添加滚动窗格/表格的方式相同。

在下面的代码中:

JLabel welcome = new JLabel("Welcome to the library. Choose your patron: ");
welcome.setBounds(50,50, 100, 30);
window.getContentPane().add(welcome);

JTextField user = new JTextField("Enter the full name in this box.");
user.setBounds(150,150,100,30);
window.getContentPane().add(user);

不应使用 setBounds(...) 语句。默认情况下,框架的内容窗格使用 BorderLayout。布局管理器会根据布局管理器的规则设置组件的大小/位置。

如果在添加组件时未指定约束,则使用 BorderLayout.CENTER。但是,您只能将一个组件添加到 CENTER,因此只有文本字段具有正确的大小/位置。标签被忽略。

因此,假设您将 GUI 代码从 main() 方法移至构造函数,正确的设计应该是:

JPanel top = new JPanel();
top.add(welcome);
top.add(user);

add(top, BorderLayot.PAGE_START);

那么你也可以使用:

//getContentPane().add(scrollPane, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);

这将在框架顶部显示标签和文本字段,在中心显示滚动窗格。然后滚动条会随着帧大小的变化而自动调整。

【讨论】:

    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多