- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图在 JScrollpane
中显示一系列按钮。阅读周围,我设法退出此代码,但没有显示任何内容。我不明白可能的错误。谢谢你的帮助
按照建议我做了一些更改,我编辑了但不起作用
已编辑或者我很笨,或者这是其他问题。这是我带有输出图像的完整代码
public class Main extends javax.swing.JFrame {
private final JPanel gridPanel;
public Main() {
initComponents();
// EXISTING PANEL
gridPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(gridPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(scrollPane, BorderLayout.CENTER);
this.Avvio();
}
private void Avvio() {
JPanel pane = new JPanel(new GridBagLayout());
pane.setBorder(BorderFactory.createLineBorder(Color.BLUE));
pane.setLayout(new GridBagLayout());
for (int i = 0; i < 10; i++) {
JButton button;
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = i;
pane.add(button, c);
button = new JButton("Button 2");
c.gridx = 1;
c.gridy = i;
pane.add(button, c);
button = new JButton("Button 3");
c.gridx = 2;
c.gridy = i;
pane.add(button, c);
}
gridPanel.add(pane);
gridPanel.revalidate();
gridPanel.repaint();
}
}
最佳答案
好的,从你在另一个答案中的评论:
No problem for compile , simply the Jpanel is empty. The buttons does not appear.
调用 this.Avvio();
后,您必须调用:
this.add(scrollPane);
this.pack();
这将产生以下输出(在调整大小之前和之后):
但是还是没有JScrollPanel
这至少解决了第一个问题,但是您的代码中有更多错误,其中一些错误已经在其他答案中进行了注释:
您正在扩展 JFrame
,这不是必需的,因为您可以创建一个 JFrame
实例/对象并在以后使用它。您永远不会更改 JFrame
的行为,这就是不需要扩展它的原因。参见 Extends JFrame vs. creating it inside the program有关这方面的更多信息。
您没有调用 pack()
或 setSize(...)
这会创建一个小窗口,您需要手动调整它的大小。在使您的 JFrame
可见之前调用推荐的 pack()
。 (正如本答案开头所建议的那样)。
您正在调用 .invokeLater()
方法两次。你只需要调用一次,我更喜欢这种方式:
SwingUtilities.invokeLater(() -> new Main()); //Note there is no call to .setVisible(true); as per point #1. It should go later in the program like: frame.setVisible(true);
您正在调用 gridPanel.revalidate();
和 gridPanel.repaint()
虽然它不会影响您的程序,但您不需要它GUI 仍然不可见,因此这些调用对您的程序没有影响,您可以安全地删除它们。
您在 for
循环的每次迭代中创建了一个新的 GridBagConstraints
对象,您只需在其中更改其属性并在 外部声明它code>for
循环,这将使您的程序变得更好。
遵循上述建议后,您可以得到如下代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class Main {
private final JPanel gridPanel;
private JFrame frame;
public Main() {
// EXISTING PANEL
gridPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(gridPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(scrollPane, BorderLayout.CENTER);
this.Avvio();
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
private void Avvio() {
JPanel pane = new JPanel(new GridBagLayout());
pane.setBorder(BorderFactory.createLineBorder(Color.BLUE));
pane.setLayout(new GridBagLayout());
for (int i = 0; i < 10; i++) {
JButton button;
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = i;
pane.add(button, c);
button = new JButton("Button 2");
c.gridx = 1;
c.gridy = i;
pane.add(button, c);
button = new JButton("Button 3");
c.gridx = 2;
c.gridy = i;
pane.add(button, c);
}
gridPanel.add(pane);
}
public static void main(String args[]) {
/* Create and display the form */
SwingUtilities.invokeLater(() -> {
new Main();
});
}
}
仍然产生这个输出:
我们可能有两个嵌套的 for
循环,用于 GridBagConstraints
属性以及按钮的生成:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class ScrollablePaneWithButtons {
private static final int ROWS = 10;
private static final int COLS = 3;
private JFrame frame;
private JPanel pane;
private JButton[][] buttons;
private GridBagConstraints gbc;
private JScrollPane scroll;
private JButton[] menuButtons;
private JPanel menuPane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new ScrollablePaneWithButtons()::createAndShowGui);
}
private void createAndShowGui() {
frame = new JFrame(this.getClass().getSimpleName());
pane = new JPanel();
pane.setLayout(new GridBagLayout());
menuPane = new JPanel();
menuPane.setLayout(new GridLayout(1, 3));
buttons = new JButton[ROWS][COLS];
menuButtons = new JButton[] {new JButton("Edit"), new JButton("Delete"), new JButton("Sort Fields")};
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.weightx = 0.5;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
buttons[i][j] = new JButton("Button " + (j + 1));
gbc.gridx = j;
gbc.gridy = i;
pane.add(buttons[i][j], gbc);
}
}
scroll = new JScrollPane(pane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
for (JButton b : menuButtons) {
menuPane.add(b);
}
frame.add(scroll);
frame.add(menuPane, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
而且这个例子(在我看来)更容易阅读和跟进。这是上面代码生成的输出:
您仍然可以选择要使用的代码,可以在此答案的第一部分进行修改,第二部分按照上述建议进行修改,或者最后一个更短。
关于Java JScrollpane 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47774639/
我有一个 JScrollPane,它的内容 Pane 有一个 JPanel。我向该 JPanel 添加了较小的 JPanel,正如预期的那样,如果我添加太多 JPanel,将会出现一个垂直滚动条。 问
我正在尝试在另一个 JScrollPane 中添加一个 JScrollPane。内部 JScrollPane 只会水平滚动,外部只会垂直滚动。 在这张图中,您可以看到添加 mainPanel 时水平滚
我正在尝试创建一个带有滚动条的容器,并且容器内部有两个内部面板。顶部内面板内还有另一个 JScrollPane。 但目前我遇到的问题是,当我的顶部内面板太长(宽度)时,顶部内面板内的滚动条被禁用,我只
你们能让我知道禁用水平滚动条的最佳方法是什么吗? 我有 宽度:100% 和 高度:280px 的 div。当我们有很长的连续文本(没有任何空格)时,我们会显示一个水平滚动条。 顺便说一句,我正在使用
我注意到这个问题主要出现在 OS 10.5.8 上的 Firefox 3.6.6 上,Safari 上偶尔也会出现这种情况(准备好惊讶的表情 - IE 实际上每次都工作正常 - 什么?!)。 我的网址
所以我有一堆JTable。每个JTable 都位于JScrollPane 内。然后,我将每个 JScrollPane 添加到 JPanel 中。然后,我将此 JPanel 添加到 JScrollPan
我在 JScrollPane 上放置了多个 JPanel。现在我有了它,所以如果你的鼠标在框架之外,那么它就不会拖动 JPanels。 当我在一个方向上移动组件时,我需要让它滚动。 (例如,如果我
有什么区别 JScrollPane.getViewportBorderBounds() and JScrollPane.getViewport() and JscrollPane.getVisible
此应用程序适用于触摸屏。我只需要仅当用户触摸 JScrollPane 区域时 JScrollPane 的滚动条可见。 我是 GUI 和 swing 的新手。这会很有帮助,我不明白是什么,或者如果在其他
出于布局目的,我需要在滚动内容底部和容器底部之间放置 15px 的空间:div class="scroll-pane" . 造型容器 .scroll-pane { padding-bottom:15p
我需要一个可以显示很多图像的程序,并且我需要一个可以滚动的窗口。我阅读了文档并在论坛上进行了搜索,但仍然没有成功。我尝试使用 JScrollPane 和 JFrame,如下所示。 JScrollPan
我今天遇到了这个新事物,但我不知道为什么。例如,当我想在面板中显示某些内容时,我只需将其添加到面板即可;但为什么我不能直接添加表格到滚动 Pane ,为什么我必须调用 setviewportview(
我今天遇到了这个新事物,我不知道为什么。例如,当我想在面板中显示某些内容时,我只需将其添加到面板即可;但是为什么我不能直接添加表格到滚动 Pane ,为什么我必须调用 setviewportview(
我一直在尝试缩小 JScrollPane 的内容宽度,例如。我已将 HorizontalScrollBarPolicy 设置为 NEVER,但这最终导致没有 ScrollBar 出现并且内容不再显
所以,在下面的代码中,我在左侧有一个 JTextArea。右上角的 JScrollPane 看起来不错。使用相同的代码,我还在右下侧添加了一个 JScrollPane,但是尽管代码相同,但保存了首选大
我在 div 上使用 JScrollPane 来滚动它。 div 包含一个自写的 javascript 代码,它从我的 tumblr 提要中获取最后几篇文章。但是,即使滚动 Pane 显示正常,但由于
请看下面的代码块 import java.awt.Color; import java.awt.Dimension; import javax.swing.JFrame; import javax.s
我在 Swing 中制作了一个简单的 GUI,其中在 JScrollPane 内、JSPlitPane 内、JPanel 内、....在 JFrame 内有一个大 JPanel(显示大 Buffere
我有一个带滚动条的 JPanel,我想向它添加很多 JLabel。但是滚动条不起作用。我无法使用滚动条,即使面板已满,它也不会滚动。这是我的代码: import java.awt.*; import
我正在尝试在 JScrollPane 中添加 2 个图像。第一个图像是背景,第二个图像与第一个图像重叠。当我运行我的程序时,问题只显示第二张图像! 请帮忙 ImageIcon ii = new Ima
我是一名优秀的程序员,十分优秀!