gpt4 book ai didi

java - Swing 逐页转换(MultiPages Widget)

转载 作者:行者123 更新时间:2023-12-02 07:38:08 25 4
gpt4 key购买 nike

我正在寻找一种策略,它允许我创建 Java Swing 应用程序并使用按钮更改我的 JFrame 上显示的组件。

我正在尝试使用 JButtonActionListener 来做到这一点,但运气不佳。

我不能使用JDialogCardLayout因为我一次只能显示一个页面供用户按顺序进行交互。

最佳答案

这是一个完成这项工作的类:

enter image description here

public class MultiPages extends JPanel implements ActionListener {

MultiPages() {
super(new BorderLayout());
add(leftButton, BorderLayout.WEST);
add(pages, BorderLayout.CENTER);
add(rightButton, BorderLayout.EAST);
leftButton.addActionListener(this);
rightButton.addActionListener(this);
animation.setInitialDelay(10);
animation.setRepeats(false);
}

public void addPage(JComponent page) {
page.setLocation(0, 0);
pages.add(page);
pages.setComponentZOrder(page, pages.getComponentCount() - 1);
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() { doLayout(); }});
}

@Override
public void doLayout() {
Dimension size = getParent().getSize();
size.width -= leftButton.getWidth() + rightButton.getWidth();
pages.setSize(size);
for (Component page : pages.getComponents()) {
page.setSize(size);
}
super.doLayout();
}

private void scrollLeft() {
direction = true;
Component next = pages.getComponents()[pages.getComponentCount() - 1];
pages.setComponentZOrder(next, 1);
animation.start();
leftButton.setEnabled(false);
rightButton.setEnabled(false);
}

private void scrollRight() {
direction = false;
animation.start();
leftButton.setEnabled(false);
rightButton.setEnabled(false);
}

@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == leftButton) {
scrollLeft();
} else if (src == rightButton) {
scrollRight();
} else if (src == animation) {
Component onTop = pages.getComponents()[0];
onTop.setLocation(onTop.getX() + (direction ? +4 : -4), 0);
if (Math.abs(onTop.getX()) < onTop.getWidth()) {
animation.start();
} else {
if (direction) {
pages.setComponentZOrder(onTop, 1);
} else {
pages.setComponentZOrder(onTop, pages.getComponentCount() - 1);
}
onTop.setLocation(0, 0);
leftButton.setEnabled(true);
rightButton.setEnabled(true);
}
}
}
private final JPanel pages = new JPanel(null);
private final JButton leftButton = new JButton("<<");
private final JButton rightButton = new JButton(">>");
private final Timer animation = new Timer(0, this);
private boolean direction;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Multi pages demo App");

frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MultiPages multiPage = new MultiPages();
multiPage.setPreferredSize(new Dimension(250, 100));

JPanel page;
page = new JPanel();
page.setBackground(Color.CYAN);
page.setLayout(new BorderLayout());
page.add(new JLabel("First page", SwingConstants.CENTER), BorderLayout.CENTER);
multiPage.addPage(page);
page = new JPanel();
page.setBackground(Color.YELLOW);
page.setLayout(new BorderLayout());
page.add(new JLabel("Second page", SwingConstants.CENTER), BorderLayout.CENTER);
multiPage.addPage(page);
page = new JPanel();
page.setBackground(Color.GREEN);
page.setLayout(new BorderLayout());
page.add(new JLabel("Third page", SwingConstants.CENTER), BorderLayout.CENTER);
multiPage.addPage(page);
page = new JPanel();
page.setBackground(Color.RED);
page.setLayout(new BorderLayout());
page.add(new JLabel("Fourth page", SwingConstants.CENTER), BorderLayout.CENTER);
multiPage.addPage(page);

frame.add(multiPage, BorderLayout.CENTER);

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

对于 CardLayout 的优点这是没有动画的同一事物的实现,因为 CardLayout 不公开它的内部映射。

您可以看到它更简单、代码更少、错误更少...

public class MultiPagesCardLayout extends JPanel implements ActionListener {

MultiPagesCardLayout() {
super( new BorderLayout());
add( _leftBtn , BorderLayout.WEST );
add( _pages , BorderLayout.CENTER );
add( _rightBtn, BorderLayout.EAST );
_leftBtn .addActionListener( this );
_rightBtn.addActionListener( this );
_animation.setInitialDelay( 10 );
_animation.setRepeats( false );
}

public void addPage( Component page ) {
_pages.add( page, "" + ( _pages.getComponentCount() - 1 ));
}

@Override
public void actionPerformed( ActionEvent e ) {
Object src = e.getSource();
if( src == _leftBtn ) {
((CardLayout)_pages.getLayout()).previous( _pages );
}
else if( src == _rightBtn ) {
((CardLayout)_pages.getLayout()).next( _pages );
}
}

private final JPanel _pages = new JPanel( new CardLayout());
private final JButton _leftBtn = new JButton( "<<" );
private final JButton _rightBtn = new JButton( ">>" );
private final Timer _animation = new Timer( 0, this );

public static void main( String[] args ) {
SwingUtilities.invokeLater( new Runnable() {
@Override public void run() {
JFrame frame = new JFrame( "Multi pages demo App" );
frame.setLayout( new BorderLayout());
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
MultiPagesCardLayout multiPage = new MultiPagesCardLayout();
multiPage.setPreferredSize( new Dimension( 250, 100 ));
JPanel page;
page = new JPanel();
page.setBackground( Color.CYAN );
page.setLayout( new BorderLayout());
page.add( new JLabel( "First page", SwingConstants.CENTER ), BorderLayout.CENTER );
multiPage.addPage( page );
page = new JPanel();
page.setBackground( Color.YELLOW );
page.setLayout( new BorderLayout());
page.add( new JLabel( "Second page", SwingConstants.CENTER ), BorderLayout.CENTER );
multiPage.addPage( page );
page = new JPanel();
page.setBackground( Color.GREEN );
page.setLayout( new BorderLayout());
page.add( new JLabel( "Third page", SwingConstants.CENTER ), BorderLayout.CENTER );
multiPage.addPage( page );
page = new JPanel();
page.setBackground( Color.RED );
page.setLayout( new BorderLayout());
page.add( new JLabel( "Fourth page", SwingConstants.CENTER ), BorderLayout.CENTER );
multiPage.addPage( page );
frame.add( multiPage, BorderLayout.CENTER );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}});
}
}

关于java - Swing 逐页转换(MultiPages Widget),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111466/

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