Java JFrame中的滚动条未显示
我试图在我的JFrame中添加一个水平滚动条,它实际上包含一组JButton。在我的代码中,您可以看到我添加了19个名为Ground的绿色jbutton,以及其他几个按钮。最后几部电影显然已脱离了银幕。我想要的是实现一个滚动条,这样我就可以查看所有的按钮。不幸的是,我似乎无法让JScrollPane滚动条工作。它会显示,但实际上不会滚动 我希望得到一些反馈Java JFrame中的滚动条未显示,java,swing,jframe,jscrollpane,Java,Swing,Jframe,Jscrollpane,我试图在我的JFrame中添加一个水平滚动条,它实际上包含一组JButton。在我的代码中,您可以看到我添加了19个名为Ground的绿色jbutton,以及其他几个按钮。最后几部电影显然已脱离了银幕。我想要的是实现一个滚动条,这样我就可以查看所有的按钮。不幸的是,我似乎无法让JScrollPane滚动条工作。它会显示,但实际上不会滚动 我希望得到一些反馈 import javax.swing.*; import java.awt.*; import java.util.ArrayList;
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);
}
}
结果:
JScrollPane是一个复杂的组件,您确实应该从阅读和开始,更好地了解它的工作原理和使用方法。JScrollPane包含一个JViewportView,它维护对要考虑用于布局的基本组件的引用,在此基础上,添加组件,然后让布局管理器系统执行其余操作。。。这意味着,你不会逃脱挫折,你必须努力了解布局管理器是如何工作的。我根据你的实现添加了代码示例,水平滚动按预期工作。回答很好,@Cortex;非常感谢你。一个简单的问题是:如果我想按按钮的顺序准确地放置按钮,这就是为什么我在按钮上使用了setBounds方法,我该如何保持原来的布局,或者更确切地说,保持原来的顺序,并且仍然有滚动条?很高兴我帮了忙,在这种情况下,由于FlowLayout,收进边界的x和y将无效。按钮将按添加到面板的顺序显示。因此,请更改myPanel.add.;的位置/顺序。。;。还要添加这个temp.setTextInteger.toStringi;在循环的绿色按钮中,您可以看到加法的顺序。一般来说,课程安排包含在主题下,我鼓励你花时间学习。
myPanel.setPreferredSize(new Dimension(100, 200));