我有一个带有内容的JPanel,并希望用箭头包围它,这样当你点击一个jpanel改变(到另一个)。我已经习惯了这种用户界面模式/成语我想在闪光灯中但是已经找不到它了。 通常单击箭头也会点击箭头,选中后会闪烁。
问题: 1.即使在闪存中也可以参考我正在谈论的内容? 2.知道在Java中执行此操作的库吗?也许用JavaFX? 3.示例app / code? p>
答案 0 :(得分:2)
这可以通过普通的Swing来完成。
建议:
CardLayout
轻松切换容器BasicArrowButton
作为方向按钮我很快会提供SSCCE。与此同时,另见:
这是一个非常 冗长的 SSCCE :
public final class SwingTransitionDemo {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame("Swing Transition Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(JMainPane.newInstance());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static final class JMainPane extends JPanel{
private static final int PAD = 5;
private JMainPane(){
super(new GridBagLayout());
setBackground(Color.PINK);
GridBagConstraints c = new GridBagConstraints();
// Create north button
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(PAD, 0, PAD, 0);
c.fill = GridBagConstraints.HORIZONTAL;
final BasicArrowButton north = new BasicArrowButton(BasicArrowButton.NORTH);
north.addActionListener(BasicArrowButtonActionListener.getInstance());
add(north, c);
// Create west button
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(0, PAD, 0, PAD);
c.fill = GridBagConstraints.VERTICAL;
final BasicArrowButton west = new BasicArrowButton(BasicArrowButton.WEST);
west.addActionListener(BasicArrowButtonActionListener.getInstance());
add(west, c);
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.VERTICAL;
add(JCardPane.getInstance(), c);
// Create east button
c.gridx = 2;
c.gridy = 1;
c.insets = new Insets(0, PAD, 0, PAD);
c.fill = GridBagConstraints.VERTICAL;
final BasicArrowButton east = new BasicArrowButton(BasicArrowButton.EAST);
east.addActionListener(BasicArrowButtonActionListener.getInstance());
add(east, c);
// Create south button
c.gridx = 1;
c.gridy = 2;
c.insets = new Insets(PAD, 0, PAD, 0);
c.fill = GridBagConstraints.HORIZONTAL;
final BasicArrowButton south = new BasicArrowButton(BasicArrowButton.SOUTH);
south.addActionListener(BasicArrowButtonActionListener.getInstance());
add(south, c);
}
public static final JMainPane newInstance(){
return new JMainPane();
}
private static final class JCardPane extends JPanel{
private static JCardPane INSTANCE;
private JCardPane(){
super(new CardLayout());
setBorder(BorderFactory.createLineBorder(Color.BLACK));
add(Box.createRigidArea(new Dimension(200, 200)), "");
add(Card.newInstance(Color.RED, "North"), String.valueOf(BasicArrowButton.NORTH));
add(Card.newInstance(Color.ORANGE, "WEST"), String.valueOf(BasicArrowButton.WEST));
add(Card.newInstance(Color.CYAN, "SOUTH"), String.valueOf(BasicArrowButton.SOUTH));
add(Card.newInstance(Color.GREEN, "EAST"), String.valueOf(BasicArrowButton.EAST));
}
public static final JCardPane getInstance(){
if(INSTANCE == null){
INSTANCE = new JCardPane();
}
return INSTANCE;
}
@Override
public Dimension getPreferredSize(){
return new Dimension(200, 200); // for demonstration purposes only
}
private static final class Card extends JPanel{
private Card(final Color c, final String s){
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBackground(c);
// Create components
add(Box.createVerticalGlue());
final JLabel label = new JLabel(s);
label.setAlignmentX(CENTER_ALIGNMENT);
add(label);
add(Box.createVerticalGlue());
}
public static final Card newInstance(final Color c, final String s){
return new Card(c, s);
}
}
}
private static final class BasicArrowButtonActionListener implements ActionListener{
private static BasicArrowButtonActionListener INSTANCE;
private BasicArrowButtonActionListener(){} // prevent external instantiation
public static final BasicArrowButtonActionListener getInstance(){
if(INSTANCE == null){
INSTANCE = new BasicArrowButtonActionListener();
}
return INSTANCE;
}
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout)JCardPane.getInstance().getLayout();
cl.show(JCardPane.getInstance(), String.valueOf((((BasicArrowButton)e.getSource()).getDirection())));
}
}
}
}
初始显示
如果你点击,那就说“向南”方向箭头......
这会使用各种布局,包括GridBagLayout
,BoxLayout
和CardLayout
。请注意,此代码的大部分都是只是填充。玩弄它并让我知道这是否在球场!
另见: