我正在尝试向我的JFrame添加一个水平滚动条,该滚动条本质上包含一堆JButton。在我的代码中,您可以看到我添加了19个绿色的JButton(称为“地面”)以及其他几个按钮。最后几个显然不在屏幕上。我想要实现一个滚动条,以便可以查看所有按钮。不幸的是,我似乎无法使JScrollPane滚动条正常工作。它会显示,但实际上不会滚动。
感谢您的反馈!
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class World {
public static void main(String[] args) {
// Just defining my own colour hues
Color blue = new Color(77, 121, 255);
Color green = new Color(0, 255, 0);
Color grey = new Color(166, 166, 166);
Color red = new Color(255, 102, 102);
// Create the JFrame
JFrame f = new JFrame("World");
// Creating the JButtons. They're added to the JFrame at the end of the code.
JButton object0 = new JButton("Object");
object0.setBounds(0, 0, 80, 80);
object0.setBackground(red);
JButton object1 = new JButton("Object");
object1.setBounds(80, 80, 80, 80);
object1.setBackground(red);
// This just automatically adds the "Ground" buttons to the frame, as they are many of them.
ArrayList<JButton> groundList = new ArrayList<JButton>();
for (int i = 0; i < 19; i++) {
groundList.add(new JButton("Ground"));
}
JButton temp;
int x = 80;
for (int i = 0; i < 19; i++) {
temp = groundList.get(i);
temp.setBounds(x, 0, 80, 80);
temp.setBackground(green);
f.add(temp);
x = x + 80;
}
JButton wall = new JButton("Wall");
wall.setBounds(80, 160, 80, 80);
wall.setBackground(grey);
JButton avatar = new JButton("Avatar");
avatar.setBounds(0, 80, 80, 80);
avatar.setBackground(blue);
f.add(object0);
f.add(object1);
f.add(wall);
f.add(avatar);
JScrollPane scroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVisible(true);
f.add(scroll); // This adds the scroll bars but doesn't actually make them work.
f.setSize(800, 200);
f.setVisible(true);
}
}
那是因为您需要将可滚动内容拖放到滚动条实例中。因此,您需要首先创建一个JPanel并将所有按钮添加到面板,然后创建JScrollPane并将面板传递给其构造函数。
将UI组件直接添加到JFrame并不是一个好习惯。
尝试运行此程序并检查更改,它应该可以工作:
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class World {
public static void main(String[] args) {
// Just defining my own colour hues
Color blue = new Color(77, 121, 255);
Color green = new Color(0, 255, 0);
Color grey = new Color(166, 166, 166);
Color red = new Color(255, 102, 102);
// Create the JFrame
JFrame f = new JFrame("World");
//button will be added to this panel
JPanel myPanel = new JPanel();
// Creating the JButtons. They're added to the JFrame at the end of the code.
JButton object0 = new JButton("Object");
object0.setBounds(0, 0, 80, 80);
object0.setBackground(red);
JButton object1 = new JButton("Object");
object1.setBounds(80, 80, 80, 80);
object1.setBackground(red);
// This just automatically adds the "Ground" buttons to the frame, as they are many of them.
ArrayList<JButton> groundList = new ArrayList<JButton>();
for (int i = 0; i < 19; i++) {
groundList.add(new JButton("Ground"));
}
JButton temp;
int x = 80;
for (int i = 0; i < 19; i++) {
temp = groundList.get(i);
temp.setBounds(x, 0, 80, 80);
temp.setBackground(green);
myPanel.add(temp);
x = x + 80;
}
JButton wall = new JButton("Wall");
wall.setBounds(80, 160, 80, 80);
wall.setBackground(grey);
JButton avatar = new JButton("Avatar");
avatar.setBounds(0, 80, 80, 80);
avatar.setBackground(blue);
myPanel.add(object0);
myPanel.add(object1);
myPanel.add(wall);
myPanel.add(avatar);
//notice how the panel is dropped inside the scroll pane
JScrollPane scroll = new JScrollPane(myPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVisible(true);
//f.add(scroll); // This adds the scroll bars but doesn't actually make them work.
//finally add the scroll to frame's content pane .. which is more proper than adding to frame directly
f.getContentPane().add(scroll, BorderLayout.CENTER);
f.setSize(800, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
但是
如果考虑将初始首选大小设置为面板,则所有按钮都将换行,并且不需要水平滚动:
myPanel.setPreferredSize(new Dimension(100, 200));