简体   繁体   English

Java - 有按钮显示箭头

[英]Java - having buttons displaying arrows

I would like to have a buttons in Java which shows the arrows - like on the keyboard. 我想在Java中有一个显示箭头的按钮 - 就像在键盘上一样。 So far I have this 到目前为止,我有这个

JButton arrowUp = new JButton("^");
JButton arrowDown = new JButton("v");
JButton arrowLeft = new JButton("<");
JButton arrowRight = new JButton(">");

It kinda works ... but does not look quite nice. 它有点工作......但看起来不太好看。

Any help how to improve this is appreciated 任何帮助如何改善这一点是值得赞赏的

Swing has a default arrow button class that is BasicArrowButton Swing有一个默认的箭头按钮类,即BasicArrowButton

Example: 例:

    JFrame frame = new JFrame("Arrow Button Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new BasicArrowButton(BasicArrowButton.EAST), BorderLayout.EAST);
    frame.add(new BasicArrowButton(BasicArrowButton.NORTH), BorderLayout.NORTH);
    frame.add(new BasicArrowButton(BasicArrowButton.SOUTH), BorderLayout.SOUTH);
    frame.add(new BasicArrowButton(BasicArrowButton.WEST), BorderLayout.WEST);
    frame.pack();
    frame.setVisible(true);

Using an ImageIcon on the button, or one of the Swing BasicArrowButtons is probably the best approach. 在按钮上使用ImageIcon ,或者使用其中一个Swing BasicArrowButtons可能是最好的方法。 However, you could also use Unicode arrow symbols. 但是,您可以使用Unicode箭头符号。 For example 例如

\◄ : ◄ \◄ :◄

\← : ← \← :←

\► : ► \► :►

\→ : → \→ :→

Some resources: 一些资源:

You would need to ensure that the font(s) your application may use supports these symbols. 您需要确保应用程序可能使用的字体支持这些符号。

Just use a image: 只需使用图片:

ImageIcon icon = new ImageIcon("images/icon.gif");
JButton button2 = new JButton(icon);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM