- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Java 中的 Swing 制作一个 GUI,它执行以下操作:
当按下“添加”按钮时,将创建一个 JPanel 并填满 GUI 的整个宽度。 JPanel 内部将使用 FlowLayout 排列 JLabels,告知特定详细信息(即名称、用户 ID 等)。每当随后按下“添加”按钮时,我希望在前一个 JPanel 的正下方添加一个新的 JPanel,以便 JScrollPane 在必要时激活其垂直滚动条。基本上,我希望 JScrollPane 能够在其中动态添加新的 JPanel。
但是,我在尝试执行此操作时遇到了一些问题。我无法弄清楚 JScrollPane 中的面板应使用哪个 LayoutManager。我尝试过使用 GridlLayout,但这似乎不起作用,因为当有足够的 JPanel 来覆盖整个 JScrollPane 时,GridLayout 只是创建一个新列来挤压新的 JPanel,而不是将其添加到之前的 JPanel 下方。为了实现这个目标我应该做什么?以下是我所拥有的一些图片以及我想要发生的情况的示例。
这是我想要发生的事情:
任何帮助将不胜感激,提前感谢您! :)
编辑:这是我尝试使用 GridLayout 的代码示例。
public Window() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 701, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel buttonPanel = new JPanel();
contentPane.add(buttonPanel, BorderLayout.SOUTH);
JButton btnAdd = new JButton("Add");
buttonPanel.add(btnAdd);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel systemsPanel = new JPanel();
scrollPane.setViewportView(systemsPanel);
systemsPanel.setLayout(new GridLayout(8, 1, 0, 0)); //8 rows for now
for (int i = 1; i < 50; i++) {
systemsPanel.add(new JButton("test"), "cell 0 " + i);
}
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2); //Center the Frame
}
最佳答案
如果您想将 JPanel 行添加到 GUI 中,我会将它们放置在使用 GridLayout(0, 1) 或 (0, 1, x, y) 的 JPanel 中。 0 表示行数可变,1 表示 1 列,x 和 y 表示垂直和水平间隙。然后,我将其放入使用 JPanel 的 BorderLayout 中的北部位置,这样行就不会像它们本来想要的那样扩展以填充滚动 Pane 的视口(viewport)。我会将第二个包装器 JPanel 放入 JScrollPane 的视口(viewport)中。
添加任何组件后,我会在容器上调用重新验证和重新绘制,以便其布局管理器将激活并正确放置新组件,并重新绘制以消除可能悬卡在周围的任何脏像素(在删除时更重要)组件)。
例如,我的 MCVE:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class AddRowA extends JPanel {
private static final int PREF_W = 650;
private static final int PREF_H = 400;
// JPanel to hold all rows. uses gridlayout that has 1 column and variable number
// of rows
private JPanel rowHolderPanel = new JPanel(new GridLayout(0, 1, 1, 1));
public AddRowA() {
// outerPanel is a wrapper or container JPanel that is
// held by JScrollPane's viewport that holds the rowHolderPanel in
// a BorderLayout.PAGE_START location, so the rows don't expand unnecessarily
JPanel outerPanel = new JPanel(new BorderLayout());
outerPanel.add(rowHolderPanel, BorderLayout.PAGE_START);
JScrollPane scrollPane = new JScrollPane(outerPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel addPanel = new JPanel();
addPanel.add(new JButton(new AddAction("Add")));
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(addPanel, BorderLayout.PAGE_END);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class AddAction extends AbstractAction {
public AddAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel();
panel.add(new JLabel("Foo"));
panel.add(Box.createHorizontalStrut(25));
panel.add(new JButton("Bar"));
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
rowHolderPanel.add(panel);
rowHolderPanel.revalidate();
rowHolderPanel.repaint();
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Add Row A");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new AddRowA());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
但是,您最好使用 JTable...例如:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class AddRowB extends JPanel {
private MyTableModel tableModel = new MyTableModel();
private JTable table = new JTable(tableModel);
public AddRowB() {
JPanel addPanel = new JPanel();
addPanel.add(new JButton(new AddAction("Add")));
setLayout(new BorderLayout());
add(new JScrollPane(table));
add(addPanel, BorderLayout.PAGE_END);
}
private class AddAction extends AbstractAction {
public AddAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
RowData row = new RowData("Type", "Address", "User ID");
tableModel.addRow(row);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Add Row B");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new AddRowB());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class MyTableModel extends DefaultTableModel {
private static final String[] COLUMNS = {"Type", "Address", "User ID"};
public MyTableModel() {
super(COLUMNS, 0);
}
public void addRow(RowData rowData) {
Object[] row = {
rowData.getType(),
rowData.getAddress(),
rowData.getUserID()
};
addRow(row);
}
}
class RowData {
String type;
String address;
String userID;
public RowData(String type, String address, String userID) {
this.type = type;
this.address = address;
this.userID = userID;
}
public String getType() {
return type;
}
public String getAddress() {
return address;
}
public String getUserID() {
return userID;
}
}
关于java - 如何动态添加元素到 JScrollPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38021262/
我有一个 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
我是一名优秀的程序员,十分优秀!