2011-12-14 95 views
8

我想在Java中有一個按鈕,顯示箭頭 - 就像在鍵盤上。 到目前爲止,我有這個爪哇 - 有按鈕顯示箭頭

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

它還挺工作...但不看相當不錯。

任何幫助,如何改善這種認識

回答

19

擺有一個默認的箭頭按鈕類,它是BasicArrowButton

例子:

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); 
+3

+1不錯!教我新東西:) – MByD 2011-12-14 09:33:00

4

只需用一個形象:

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

如果我建立應用程序到.JAR文件併發送給我的朋友...他會看到圖標? – 2011-12-14 09:46:46

+3

@PavelJanicek:如果你在jar中包含圖標並在你的類中正確地獲取它們作爲資源,那麼* YES *。 – 2011-12-14 09:47:55