java - 如何在 JScrollPane 上显示按钮数组?

标签 java swing user-interface jbutton jscrollpane

我试图创建一个按钮数组并将它们放入滚动框中,但没有显示任何内容。另外,如何添加 Action 监听器?

JPanel buttons4Contacts = new JPanel();
buttons4Contacts.setLayout(new GridLayout(5, 5));

for (User user: Network.usersList){ //Make a JButton for every contact.
    JButton button = new JButton(user.getName());
    button.setPreferredSize(new Dimension(100,100));
    buttons4Contacts.add(button);
}

JPanel contactButtonHolder = new JPanel(new FlowLayout(FlowLayout.CENTER, 0,0));
contactButtonHolder.add(buttonsforContacts);
contacts = new JScrollPane(contactButtonHolder);

contacts.add(buttons4Contacts);
contacts.setPreferredSize(new Dimension(200,200));
rightPanelSouth.add(contacts);

最佳答案

正如其他 friend 之前提到的,您可以创建一个面板,在面板中添加所有按钮,最后在面板中添加滚动条。

这是我完成这项工作的简单代码:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ArrayOfButtons extends JFrame{

    final static int numberOfButtons=5;   

    // Label to display the message indicating which button generated the event.

    JLabel label=new JLabel();

    // Panel to accomodate the labels

    JPanel panel=new JPanel(); 

    //We create an array of buttons whose number depends on our choice  

    JButton buttons[]=new JButton[numberOfButtons]; 

public ArrayOfButtons(){
    super("Demo");

    //We create an instance of the class ButtonHandler which implements the interface
    //ActionListener. And this object "handler" is basically the object that handles 
    //the events.            

    ButtonHandler handler=new ButtonHandler();  

    //Using a for loop we create JButtons.    

    for(int i=0;i<numberOfButtons;i++){
        buttons[i]=new JButton("Button"+i);

        // here we register handler as the event listener for all the buttons.i.e if in 
        // case an event occurs in any of the buttons, the event listener (in our case
        //the handler object) calls an appropriate method that handles the event.            

        buttons[i].addActionListener(handler);

        // add each button to the panel

        panel.add(buttons[i]);
    }

   // we add the label to the JFrame 
   add(label,BorderLayout.SOUTH);
   // we add ScrollPane to the JFrame and in turn add Panel to the ScrollPane.
   add(new JScrollPane(panel));      
}

// This is an inner class that handles the events.(Look more on inner classes).

 public class ButtonHandler implements ActionListener{   

    //This method is called in case an action event occurs.For example you click on the
    //button      
     public void actionPerformed(ActionEvent event){

         // gets the name of the button and displays in the label.

         label.setText(event.getActionCommand());

     }       
}
}

添加您自己的 main 方法,代码就可以正常工作。希望这会有所帮助。

关于java - 如何在 JScrollPane 上显示按钮数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27454475/

相关文章:

JavaFX 一次运行大量倒计时器?

java - 阻止 objectweb asm 用 throw 替换无法访问的代码

Java:设置回相同的 JFrame

Java 碰撞检测并将对象设置回来

保龄球记分卡的 iOS UI 控件

java - 支付网关集成时获取 SSLException

java - Android Studio 1.0.2 m2respository gradle 问题

Java:如何等待 updateUI?

java - 如何使用Java绘制不完整的多边形

java - 如何在 Java 中实现这个(带下拉菜单的菜单栏)?