gpt4 book ai didi

Java JScrollpane 不可见

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:48:01 26 4
gpt4 key购买 nike

我试图在 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();

}
}

enter image description here

最佳答案

好的,从你在另一个答案中的评论:

No problem for compile , simply the Jpanel is empty. The buttons does not appear.

调用 this.Avvio(); 后,您必须调用:

this.add(scrollPane);
this.pack();

这将产生以下输出(在调整大小之前和之后):

enter image description here enter image description here

但是还是没有JScrollPanel

这至少解决了第一个问题,但是您的代码中有更多错误,其中一些错误已经在其他答案中进行了注释:

  1. 您正在扩展 JFrame,这不是必需的,因为您可以创建一个 JFrame 实例/对象并在以后使用它。您永远不会更改 JFrame 的行为,这就是不需要扩展它的原因。参见 Extends JFrame vs. creating it inside the program有关这方面的更多信息。

  2. 您没有调用 pack()setSize(...) 这会创建一个小窗口,您需要手动调整它的大小。在使您的 JFrame 可见之前调用推荐的 pack()。 (正如本答案开头所建议的那样)。

  3. 您正在调用 .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);
  4. 您正在调用 gridPanel.revalidate();gridPanel.repaint() 虽然它不会影响您的程序,但您不需要它GUI 仍然不可见,因此这些调用对您的程序没有影响,您可以安全地删除它们。

  5. 您在 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();
});
}
}

仍然产生这个输出:

enter image description here

但是......我们还可以再改进一点!

我们可能有两个嵌套的 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);
}
}

而且这个例子(在我看来)更容易阅读和跟进。这是上面代码生成的输出:

enter image description here

您仍然可以选择要使用的代码,可以在此答案的第一部分进行修改,第二部分按照上述建议进行修改,或者最后一个更短。

关于Java JScrollpane 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47774639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com