gpt4 book ai didi

java - CardLayout 中的可滚动 JPanel?

转载 作者:行者123 更新时间:2023-11-29 03:22:28 25 4
gpt4 key购买 nike

我一直在寻找一种方法来将垂直滚动条添加到 JPanel 中,而后者又被添加到 CardLayout 面板中。我查阅了这里所有与实现可滚动的 JPanel 相关的帖子,但我不知道如何使用这个特定的 CardLayout 来实现它。 Oracle 没有提供我可以使用的示例。

也许我没有为 JWindow 或我使用的任何其他组件使用正确的配置。

我在程序的精简版下方放置了一个垂直滚动条。

我的问题是如何在代码底部实现 JPanelScrollable 类,使其可以滚动?

import javax.swing.*;

import java.awt.*;

import java.net.URL ;


public class Program2 extends JFrame
{
public Program2()
{
super("Flash CC");
Container2 container = new Container2();
setSize(700, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setFocusable( true ) ;
add(container);

setVisible(true) ;
}


public static void main(String[] args)
{
Program2 prog = new Program2();
}
}

class Container2 extends JPanel
{
private CardLayout cardLayout = new CardLayout() ;
private JPanel1 jPanelFirst = new JPanel1() ;
private JPanel2 jPanelSecond = new JPanel2() ;
private JPanelScrollable jPanelThird = new JPanelScrollable() ;

//Constructor
Container2()
{
this.setLayout( cardLayout ) ;
this.setFocusable( true ) ;

JScrollPane scrollFrame = new JScrollPane(jPanelThird);

this.add( jPanelFirst, "first card" ) ;
this.add( jPanelSecond, "second card" ) ;
this.add( scrollFrame , "third card" ) ;

cardLayout.show( this, "third card" ) ;
}
}

class JPanel1 extends JPanel
{

}

class JPanel2 extends JPanel
{

}

class JPanelScrollable extends JPanel
{
// here many, many, many elemnts will go
// and a vertical scroll barr is needed to view'm all.
JPanelScrollable()
{
this.setOpaque( true ) ;
this.setLayout( null ) ;
for(int i=0; i<30; i++)
{
JButton b = new JButton("Button" + i) ;
b.setBounds(0, (i * 100), 100, 50) ;
this.add(b) ;
}

}
}

最佳答案

将面板添加到 JScrollPane

JScrollPane scrollPane = new JScrollPane(jPanelThird);

将滚动 Pane 添加到 CardLayout

this.add(scrollPane, "third card");

参见 How to use scroll panes了解更多详情

更新了工作示例

Card1 Card2

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestCardLayout {

public static void main(String[] args) {
new TestCardLayout();
}

private int index = 0;

public TestCardLayout() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

final CardLayout cl = new CardLayout();
final JPanel cardPane = new JPanel(cl);
cardPane.add(new JLabel("Hello"), "1");
cardPane.add(new JScrollPane(createForm()), "2");

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(cardPane);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

JButton next = new JButton("Next");
next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
index++;
if (index > 1) {
index = 0;
}
if (index == 0) {
cl.show(cardPane, "1");
} else {
cl.show(cardPane, "2");
}
}
});
frame.add(next, BorderLayout.SOUTH);
}

});
}

public JPanel createForm() {
JPanel form = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
for (int index = 0; index < 100; index++) {
form.add(new JTextField(10), gbc);
}
return form;
}
}

关于java - CardLayout 中的可滚动 JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795036/

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