如果添加了太多按钮,JToolBar 会显示箭头
JToolBar show arrow if too many buttons were added
当可见容器小于 space 所有按钮所需的尺寸时,有没有办法使用箭头(如 ) in JToolBar
(API)?
(为了便于说明,请参阅 WebUI zkoss)
UI manager (L&F) 应该是普通的swing。
Is there a way to have an arrow
它不是 API 的一部分,因此您需要创建自己的。
您或许可以使用以下内容给您一些想法。当没有足够的 space 用于所有按钮时,此示例将按钮添加到 left/right,以便您可以滚动查看每个按钮:
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
public class ScrollContainer extends JPanel
implements ActionListener, ComponentListener
{
private Container container;
private List<Component> removedComponents = new ArrayList<Component>();
private JButton forwardButton;
private JButton backwardButton;
public ScrollContainer(Container container)
{
this.container = container;
setLayout( new BorderLayout(5, 0) );
addComponentListener( this );
// Create buttons to control scrolling
backwardButton = new BasicArrowButton( BasicArrowButton.WEST );
configureButton( backwardButton );
forwardButton = new BasicArrowButton( BasicArrowButton.EAST);
configureButton( forwardButton );
// Layout the panel
add( backwardButton, BorderLayout.WEST );
add( container );
add( forwardButton, BorderLayout.EAST );
}
// Implement the ComponentListener
public void componentResized(ComponentEvent e)
{
// When all components cannot be shown, add the forward button
int freeSpace = getSize().width - container.getPreferredSize().width;
if (backwardButton.isVisible())
freeSpace -= backwardButton.getPreferredSize().width;
forwardButton.setVisible( freeSpace < 0 );
// We have free space, redisplay removed components
while (freeSpace > 0 && ! removedComponents.isEmpty())
{
if (removedComponents.size() == 1)
freeSpace += backwardButton.getPreferredSize().width;
Object o = removedComponents.get(removedComponents.size() - 1);
Component c = (Component)o;
freeSpace -= c.getSize().width;
if (freeSpace >= 0)
{
container.add(c, 0);
removedComponents.remove(removedComponents.size() - 1);
}
}
// Some components still not shown, add the backward button
backwardButton.setVisible( !removedComponents.isEmpty() );
// repaint();
}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void componentHidden(ComponentEvent e) {}
// Implement the ActionListener
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
// Scroll the components in the container
if (source == forwardButton)
scrollForward();
else
scrollBackward();
}
/*
* Simulate scrolling forward
* by remove the first component from the container
*/
private void scrollForward()
{
if (container.getComponentCount() == 1)
return;
// Remove and save the first component
Component c = container.getComponent(0);
container.remove( c );
removedComponents.add( c );
// Allow for backwards scrolling
backwardButton.setVisible( true );
// All components are showing, hide the forward button
int backwardButtonWidth = backwardButton.getPreferredSize().width;
int containerWidth = container.getPreferredSize().width;
int panelWidth = getSize().width;
if (backwardButtonWidth + containerWidth <= panelWidth)
forwardButton.setVisible( false );
// Force a repaint of the panel
revalidate();
repaint();
}
/*
* Simulate scrolling backward
* by adding a removed component back to the container
*/
private void scrollBackward()
{
if (removedComponents.isEmpty())
return;
// Add a removed component back to the container
Object o = removedComponents.remove(removedComponents.size() - 1);
Component c = (Component)o;
container.add(c, 0);
// Display scroll buttons when necessary
if (removedComponents.isEmpty())
backwardButton.setVisible( false );
forwardButton.setVisible( true );
revalidate();
repaint();
}
private void configureButton(JButton button)
{
button.setVisible( false );
button.addActionListener( this );
}
private static void createAndShowGUI()
{
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
toolBar.add( new JButton("one") );
toolBar.add( new JButton("two222222") );
toolBar.add( new JButton("three") );
toolBar.add( new JButton("four") );
toolBar.add( new JButton("five") );
toolBar.add( new JButton("six666666666") );
toolBar.add( new JButton("seven") );
toolBar.add( new JButton("eight") );
toolBar.add( new JButton("nine9999999") );
toolBar.add( new JButton("ten") );
ScrollContainer container = new ScrollContainer(toolBar);
JFrame frame = new JFrame("Scroll Container");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(container, BorderLayout.NORTH);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
因此您需要修改代码以创建包含额外按钮的弹出菜单。
当可见容器小于 space 所有按钮所需的尺寸时,有没有办法使用箭头(如 JToolBar
(API)?
(为了便于说明,请参阅 WebUI zkoss)
UI manager (L&F) 应该是普通的swing。
Is there a way to have an arrow
它不是 API 的一部分,因此您需要创建自己的。
您或许可以使用以下内容给您一些想法。当没有足够的 space 用于所有按钮时,此示例将按钮添加到 left/right,以便您可以滚动查看每个按钮:
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
public class ScrollContainer extends JPanel
implements ActionListener, ComponentListener
{
private Container container;
private List<Component> removedComponents = new ArrayList<Component>();
private JButton forwardButton;
private JButton backwardButton;
public ScrollContainer(Container container)
{
this.container = container;
setLayout( new BorderLayout(5, 0) );
addComponentListener( this );
// Create buttons to control scrolling
backwardButton = new BasicArrowButton( BasicArrowButton.WEST );
configureButton( backwardButton );
forwardButton = new BasicArrowButton( BasicArrowButton.EAST);
configureButton( forwardButton );
// Layout the panel
add( backwardButton, BorderLayout.WEST );
add( container );
add( forwardButton, BorderLayout.EAST );
}
// Implement the ComponentListener
public void componentResized(ComponentEvent e)
{
// When all components cannot be shown, add the forward button
int freeSpace = getSize().width - container.getPreferredSize().width;
if (backwardButton.isVisible())
freeSpace -= backwardButton.getPreferredSize().width;
forwardButton.setVisible( freeSpace < 0 );
// We have free space, redisplay removed components
while (freeSpace > 0 && ! removedComponents.isEmpty())
{
if (removedComponents.size() == 1)
freeSpace += backwardButton.getPreferredSize().width;
Object o = removedComponents.get(removedComponents.size() - 1);
Component c = (Component)o;
freeSpace -= c.getSize().width;
if (freeSpace >= 0)
{
container.add(c, 0);
removedComponents.remove(removedComponents.size() - 1);
}
}
// Some components still not shown, add the backward button
backwardButton.setVisible( !removedComponents.isEmpty() );
// repaint();
}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void componentHidden(ComponentEvent e) {}
// Implement the ActionListener
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
// Scroll the components in the container
if (source == forwardButton)
scrollForward();
else
scrollBackward();
}
/*
* Simulate scrolling forward
* by remove the first component from the container
*/
private void scrollForward()
{
if (container.getComponentCount() == 1)
return;
// Remove and save the first component
Component c = container.getComponent(0);
container.remove( c );
removedComponents.add( c );
// Allow for backwards scrolling
backwardButton.setVisible( true );
// All components are showing, hide the forward button
int backwardButtonWidth = backwardButton.getPreferredSize().width;
int containerWidth = container.getPreferredSize().width;
int panelWidth = getSize().width;
if (backwardButtonWidth + containerWidth <= panelWidth)
forwardButton.setVisible( false );
// Force a repaint of the panel
revalidate();
repaint();
}
/*
* Simulate scrolling backward
* by adding a removed component back to the container
*/
private void scrollBackward()
{
if (removedComponents.isEmpty())
return;
// Add a removed component back to the container
Object o = removedComponents.remove(removedComponents.size() - 1);
Component c = (Component)o;
container.add(c, 0);
// Display scroll buttons when necessary
if (removedComponents.isEmpty())
backwardButton.setVisible( false );
forwardButton.setVisible( true );
revalidate();
repaint();
}
private void configureButton(JButton button)
{
button.setVisible( false );
button.addActionListener( this );
}
private static void createAndShowGUI()
{
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
toolBar.add( new JButton("one") );
toolBar.add( new JButton("two222222") );
toolBar.add( new JButton("three") );
toolBar.add( new JButton("four") );
toolBar.add( new JButton("five") );
toolBar.add( new JButton("six666666666") );
toolBar.add( new JButton("seven") );
toolBar.add( new JButton("eight") );
toolBar.add( new JButton("nine9999999") );
toolBar.add( new JButton("ten") );
ScrollContainer container = new ScrollContainer(toolBar);
JFrame frame = new JFrame("Scroll Container");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(container, BorderLayout.NORTH);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
因此您需要修改代码以创建包含额外按钮的弹出菜单。