ScrollPane添加到网格布局

时间:2015-05-17 21:50:33

标签: java mysql swing jscrollpane windowbuilder

在我的代码中我调用了几个项目(我的按钮,其名称导致不同的项目。名称和所有内容都来自数据库) 我想要一个J ScrollPane来包围我的按钮,我该怎么办?我只想在滚动窗格中调用按钮。这是我的代码

import java.sql.*;
import java.util.ArrayList;

import java.util.List;

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;

import javax.swing.JButton;

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


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(new GridLayout(10, 1));

    panel.add(new JLabel("Welcome to Admin Panel"));

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

    panel.add(new JLabel("Existing Projects"));

    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.add(panel);
    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;
    }

}

这看起来很正常,但实际上并非如此,因为某些原因它们不可见或不在滚动条内调用。请编辑我的代码吗?

3 个答案:

答案 0 :(得分:3)

首先将按钮添加到自己的容器中,这样您就可以与UI的其余部分分开控制按钮的布局

    JPanel panelFullOfButtons = new JPanel();
    try {
        for (int i = 0; i < projectNames.length; i++) {
            buttons[i] = new ProjectButton(projectNames[i]);
            buttons[i].setId(uniqueIds[i]);

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

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

然后添加&#34; main&#34;面板到框架的NORTH位置和&#34;按钮&#34;面板到CENTER

    frame.add(panel, BorderLayout.NORTH);
    frame.add(new JScrollPane(panelFullOfButtons), BorderLayout.CENTER);

请注意,在这种情况下,我很想尝试使用像JList之类的东西。有关详细信息,请参阅How to Use Lists

  

我做了同样的事,你能告诉我什么是错的吗?

// Problem #1...
JScrollPane pane = new JScrollPane();
pane.add(buttonPanel);

//...
// Problem #2...
panel.add(pane);
frame.add(panel);

这些相互竞争,移动内容并与现有内容重叠......

List of buttons

public AdminClass() {

    panel.setLayout(new GridLayout(3, 1));

    panel.add(new JLabel("Welcome to Admin Panel"));

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

    panel.add(new JLabel("Existing Projects"));

    List<String> projectNameList = new ArrayList<String>();
    for (int index = 0; index < 1000; index++) {
        projectNameList.add("Project " + index);
    }
    projectNames = projectNameList.toArray(new String[0]);

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

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

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

    frame.add(panel, BorderLayout.NORTH);
    frame.add(new JScrollPane(btnPnl1), BorderLayout.CENTER);

    frame.setVisible(true);
    frame.setSize(500, 500);

}

在这种情况下,我更喜欢使用JList来显示项目,或使用WrapLayout来设置按钮

答案 1 :(得分:2)

无法像这样创建GridLayoutpanel.setLayout(new GridLayout(10, 1));。如果将行声明为非零值,则将忽略列计数。当您将最大行数声明为十时,强制将第十一个组件添加到第二列(然后是第三列,第四列,依此类推)。使用panel.setLayout(new GridLayout(0, 1));代替强制只有一列frame.add(new JScrollPane(panel));来创建ScrollPane

但请注意,GridLayout会尝试在启用滚动之前尽可能缩小组件以适应容器大小。

答案 2 :(得分:1)

  

我只想让按钮部分位于J Scroll Pane

如果您只想要JButton(在循环中添加的那些):

  1. 创建一个新的JPanel,您可以将按钮添加到
  2. 将(1)添加到JScrollPane
  3. 将(2)添加到panel
  4. 例如:

    JPanel buttonPanel = new JPanel(new FlowLayout());
    for (int i = 0; i < projectNames.length; i++){
        buttons[i] = new ProjectButton(projectNames[i]);
        buttons[i].setId(uniqueIds[i]);
        panel.add(buttons[i]);
        buttonPanel[i].addActionListener(this);
    }
    JScrollPane scroller = new JScrollPane(buttonPanel);
    panel.add(scroller);
    

    您可能还会考虑使用不需要JScrollPane的其他组件,当然这取决于您的需求 - 例如JComboBox