JFrame 中的滚动条不显示
Scroll Bar in JFrame Not Showing
我正在尝试向我的 JFrame 添加一个水平滚动条,它基本上包含一堆 JButton。在我的代码中,您可以看到我添加了 19 个名为 "Ground" 的绿色 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));
结果:
我正在尝试向我的 JFrame 添加一个水平滚动条,它基本上包含一堆 JButton。在我的代码中,您可以看到我添加了 19 个名为 "Ground" 的绿色 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));
结果: