我正在寻找一种策略,它允许我创建一个 Java Swing 应用程序并使用按钮来更改我的JFrame
.
我正在尝试使用JButton
s 和ActionListener
s 来做到这一点,但运气不佳。
我不能使用JDialog
orCardLayout
因为我一次只能看到一个页面供用户按顺序进行交互。
我正在寻找一种策略,它允许我创建一个 Java Swing 应用程序并使用按钮来更改我的JFrame
.
我正在尝试使用JButton
s 和ActionListener
s 来做到这一点,但运气不佳。
我不能使用JDialog
orCardLayout
因为我一次只能看到一个页面供用户按顺序进行交互。
这是一个完成这项工作的类:
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 );
}});
}
}
这是处理它的一种方法:
关键是Jpanels。将它们视为主 jframe 中的迷你 jframe。以下是有关这些面板的一些信息:
http://docs.oracle.com/javase/6/docs/api/javax/swing/JPanel.html
单击按钮时,每个按钮基本上都会切换显示的面板。将编码放入动作侦听器中。
这种方法比使用多个 JFrame 更好。