在 JScrollPane 中创建新按钮

New button creation inside JScrollPane

我已经编写了所谓的 'error less' 代码,但我在使用该应用程序时遇到了某些问题。这以前工作得很好,但一旦我将代码作为 Jframe 移交给 windowbuilder,它就开始无法工作了。 这是我的代码:

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.RootPaneContainer;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JMenuBar;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;


public class AdminClass implements ActionListener  {

ProjectButton[] buttons = new ProjectButton[35];

//Creating data field for unique Ids in the form of array list
ArrayList<Integer> uniqueIDList = new ArrayList<Integer>();
String[] projectNames;
int[] uniqueIds;

Connection conn1 = null;
Statement stmt1 = null;

JFrame frame = new JFrame("Admin Panel");
private JPanel panel = new JPanel();
JButton btnNewButton = new JButton("Add New Project");

public AdminClass() {
    panel.setLayout(null);
    JLabel label = new JLabel("Welcome to Admin Panel");
    label.setFont(new Font("Tahoma", Font.BOLD, 15));
    label.setBounds(148, 0, 188, 45);
    panel.add(label);
    btnNewButton.setForeground(Color.GREEN);
    btnNewButton.setFont(new Font("Yu Gothic", Font.BOLD, 13));
    btnNewButton.setBounds(0, 46, 482, 45);

    btnNewButton.addActionListener(this);
    panel.add(btnNewButton);

    JLabel label_1 = new JLabel("Existing Projects");
    label_1.setFont(new Font("Tunga", Font.BOLD, 15));
    label_1.setBounds(183, 90, 102, 45);
    panel.add(label_1);

    conn1 = sqliteConnection.dbConnector();

    try{
          Class.forName("org.sqlite.JDBC");
          conn1.setAutoCommit(false);
          stmt1 = conn1.createStatement();
          ResultSet rs1 = stmt1.executeQuery( "SELECT * FROM Project;" );

          List<String> projectNameList = new ArrayList<String>();

          while ( rs1.next() ){
              int id = rs1.getInt("uniqueid");
              String projectName = rs1.getString("name");

              projectNameList.add(projectName);
              uniqueIDList.add(id);
          }

          // Converting array list to array
          projectNames = new String[projectNameList.size()];
          projectNameList.toArray(projectNames);

          uniqueIds = convertIntegers(uniqueIDList);


          rs1.close();
          stmt1.close();
          conn1.close();
    }
    catch ( Exception e1 ) {
        JOptionPane.showMessageDialog(null, e1);    
    }

    // Adding buttons to the project
    try{
        for (int i = 0; i < projectNames.length; i++){
            buttons[i] = new ProjectButton(projectNames[i]);
            buttons[i].setId(uniqueIds[i]);

            panel.add(buttons[i]);
            buttons[i].addActionListener(this);

        }
    }
    catch (Exception e2){
        JOptionPane.showMessageDialog(null, e2);
    }

    frame.getContentPane().add(panel);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(25, 133, 430, 307);
    panel.add(scrollPane);
    frame.setVisible(true);
    frame.setSize(500, 500);

}

public void actionPerformed(ActionEvent e) {

    for (int j = 0; j < buttons.length; j ++){
        if (e.getSource() == buttons[j]){
            AdminStatus sc = new AdminStatus(buttons[j].getId());
            frame.dispose();
        }
    }

    if (e.getSource() == btnNewButton){

        frame.dispose();
        WindowProjectAdmin wpa = new WindowProjectAdmin();

    }

}

//Method to convert integar array list to integar array
public int[] convertIntegers(List<Integer> integers)
    {
        int[] ret = new int[integers.size()];
        for (int i=0; i < ret.length; i++)
        {
            ret[i] = integers.get(i).intValue();
        }
        return ret;
    }

}

我希望按钮在我的滚动条中产生。如果您可以修复它,也有人编辑代码。

一般不推荐使用null Layout。您应该重新考虑这个 approach.I 将使用例如 GridLayout,这样您的按钮应该不会出现问题。

如果你想使用 null Layout,你应该使用 setBounds(),在容器内的每个组件上使用 null Layout。所以你需要 "manually" 在 JPanel 上选择它的大小和位置。

更重要的是,我认为这不是一个好主意,只在 JOptionPane 上显示异常情况下的消息,而没有 printStackTrace()。这样调试起来就会困难得多。