gpt4 book ai didi

java - 仅具有垂直滚动的 JPanel 和 JScrollPane

转载 作者:行者123 更新时间:2023-12-01 21:34:51 27 4
gpt4 key购买 nike

我有兴趣创建一个动态接收按钮的容器。当它达到容器宽度时,按钮将放置在第一行按钮的下方,并且必须显示垂直滚动条。尽管它无法显示水平滚动条,但容器可以水平调整大小。举个例子: enter image description here

单击 jbutton1,您可以根据面板尺寸添加新按钮。在这张图片中,不可能看到垂直滚动条,这就是问题所在。

我使用的代码如下:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ScrollPanelTest;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JScrollBar;

/**
*
* @author leandro.lima
*/
public class ScrollPane extends javax.swing.JFrame {

public int count = 1;
private WrapLayout layout = new WrapLayout(FlowLayout.LEADING, 5, 5);

/**
* Creates new form ScrollPane
*/
public ScrollPane() {
initComponents();
}

/**
* This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setMinimumSize(new java.awt.Dimension(0, 0));
jPanel1.setPreferredSize(new java.awt.Dimension(0, 0));
java.awt.FlowLayout flowLayout1 = new java.awt.FlowLayout();
flowLayout1.setAlignOnBaseline(true);
jPanel1.setLayout(flowLayout1);
jScrollPane1.setViewportView(jPanel1);
jPanel1.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addContainerGap(327, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(163, Short.MAX_VALUE)
.addComponent(jButton1))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1)
.addGap(34, 34, 34)))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
final JButton button = new JButton();
button.setPreferredSize(new Dimension(100, 100));
button.setSize(new Dimension(100, 100));

button.setAction(new AbstractAction("<html><center><h4>Button " + (count++) + "</h4><br>Remove me</center></html>") {
@Override
public void actionPerformed(ActionEvent ae) {
jPanel1.remove(button);
jPanel1.getRootPane().repaint();
getContentPane().repaint();
}
});

jPanel1.add(button);
jPanel1.getRootPane().repaint();
getContentPane().repaint();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {

}
//</editor-fold>

//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ScrollPane().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}

观察:我找不到添加按钮时重新绘制面板的方法,如果您知道,也请告诉我。

谢谢!

最佳答案

    jPanel1.setLayout(flowLayout1);
jScrollPane1.setViewportView(jPanel1);

您正在面板中使用 FlowLayout。 FlowLayout 不会重新计算面板的首选大小。由于首选尺寸永远不会改变,因此垂直滚动条永远不会出现。

这就是为什么您需要在面板上使用WrapLayout

您定义了一个 WrapLayout 变量,但您从未在面板上实际使用过 WrapLayout

另外,我不知道为什么你要使用 GroupLayout 来完成如此简单的事情。框架的默认布局管理器是 BorderLayout。只需使用面板创建滚动 Pane 并将滚动 Pane 添加到 BorderLayout.CENTER 即可。然后将包含该按钮的另一个面板添加到 BorderLayout.PAGE_END。它的两行代码。了解如何创建自己的 GUI,而不依赖 IDE 生成的复杂代码。

I couldn't find a way to repaint the panel when buttons are add, if you know, please show me too.

当您从可见的 GUI 添加(或删除)组件时,基本代码是:

panel.add(...);
panel.revalidate();
panel.repaint();

关于java - 仅具有垂直滚动的 JPanel 和 JScrollPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038129/

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