2012-01-08 13 views
2

我在Java Swing中做了一個簡單的Kakuro應用程序,並且我使用了JButton作爲單元格。我已經完成了從生成網格(setBackground(Color.BLACK)setBackground(Color.WHITE))到填充唯一編號的所有內容。如何用數字來繪製JButton的角落?

但問題是,我不知道如何在JButton的末尾畫上「線索」。我想是類似於:

enter image description here

有時數字可能僅出現在三面,兩面甚至1個側。

我想設置背景圖像,但它不可能,因爲數字是動態生成的數字(網格是動態的)的總和。

那麼任何想法如何得到這種JButton?或者如果它不可能,我還有什麼其他選擇?

非常感謝(我真的被卡住了)。

回答

3

非常簡單和舒適的方法是使用BorderLayout添加JLabelsJButton

import java.awt.BorderLayout; 
import java.awt.Color; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

public class FourLabelsInButton { 

    private JFrame frame; 
    private JButton myButton1; 
    private JLabel myButton1_Label_N; 
    private JLabel myButton1_Label_E; 
    private JLabel myButton1_Label_W; 
    private JLabel myButton1_Label_S = new JLabel(); 

    public FourLabelsInButton() { 
     myButton1_Label_N = new JLabel("45"); 
     myButton1_Label_N.setHorizontalAlignment(JLabel.CENTER); 
     myButton1_Label_N.setForeground(Color.red); 

     myButton1_Label_E = new JLabel("1"); 
     myButton1_Label_E.setHorizontalAlignment(JLabel.CENTER); 
     myButton1_Label_E.setForeground(Color.red); 

     myButton1_Label_W = new JLabel("9"); 
     myButton1_Label_W.setHorizontalAlignment(JLabel.CENTER); 
     myButton1_Label_W.setForeground(Color.red); 

     myButton1_Label_S = new JLabel("21"); 
     myButton1_Label_S.setHorizontalAlignment(JLabel.CENTER); 
     myButton1_Label_S.setForeground(Color.red); 

     myButton1 = new JButton(); 
     myButton1.setBackground(Color.black); 
     myButton1.setLayout(new BorderLayout()); 
     myButton1.add(myButton1_Label_N, BorderLayout.NORTH); 
     myButton1.add(myButton1_Label_E, BorderLayout.EAST); 
     myButton1.add(myButton1_Label_W, BorderLayout.WEST); 
     myButton1.add(myButton1_Label_S, BorderLayout.SOUTH); 

     frame = new JFrame(); 
     frame.add(myButton1); 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       FourLabelsInButton ggg = new FourLabelsInButton(); 
      } 
     }); 
    } 
} 
0

不應該有任何理由,你不能簡單地通過覆蓋paint()方法在JButton中繪製任何你想要的東西。

public class KakuroSquare extends JButton 
{ 
    /* ... */ 

    @Override 
    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 

     /* Your paint logic. */ 
    } 
} 
+1

使用**'的paintComponent(圖形)'** Swing(非頂級)容器! – 2012-01-08 12:18:34

2

我想設置背景圖像的,但它不是可能的,因爲數字是動態生成的數字總和(網格是動態的)。

按鈕的圖像也可以動態生成。

+0

有一個相關示例[此處](http://stackoverflow.com/a/4151403/230513)。 – trashgod 2012-01-08 12:51:11

2

您也可以使用自定義組件。對於這種情況,繪畫可以實現非常簡單:

class KakuroComponent extends JComponent { 

    private final int[] numbers; 

    public KakuroComponent(int... numbers) { 
     this.numbers = numbers; 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     int w = getWidth(); 
     int h = getWidth(); 

     g.setColor(Color.black); 
     g.fillRect(0, 0, w, h); 

     g.setColor(Color.white); 
     g.drawLine(0, 0, w, h); 
     g.drawLine(w - 1, 0, 0, h - 1); 

     if (numbers[0] > 0) // if there is a top number 
      drawStringCentered(g, String.valueOf(numbers[0]), w/2, h/6); 
     if (numbers[1] > 0) // if there is a left number 
      drawStringCentered(g, String.valueOf(numbers[1]), w/6, h/2); 
     if (numbers[2] > 0) // if there is a right number 
      drawStringCentered(g, String.valueOf(numbers[2]), w * 5/6, h/2); 
     if (numbers[3] > 0) // if there is a bottom number 
      drawStringCentered(g, String.valueOf(numbers[3]), w/2, h * 5/6); 
    } 

    void drawStringCentered(Graphics g, String s, int x, int y) { 
     Rectangle2D bounds = g.getFontMetrics().getStringBounds(s, g); 
     g.drawString(s, (int) (x - bounds.getCenterX()), (int) (y - bounds.getCenterY())); 
    } 
}