Java 如何用数字绘制JButton的角?
我正在用JavaSwing制作一个简单的Kakuro应用程序,我使用了Java 如何用数字绘制JButton的角?,java,swing,jbutton,Java,Swing,Jbutton,我正在用JavaSwing制作一个简单的Kakuro应用程序,我使用了JButtons作为单元格。我已经完成了从生成网格(setBackground(Color.BLACK)和setBackground(Color.WHITE))到填充唯一数字的所有工作 但问题是,我不知道如何在JButtons的末尾绘制“线索”。我想要的类似于: 有时数字可能只出现在3面、2面甚至1面上 我曾想过设置背景图像,但这是不可能的,因为数字是动态生成的数字的总和(网格是动态的) 那么你知道如何得到这种按钮吗?如果不
JButton
s作为单元格。我已经完成了从生成网格(setBackground(Color.BLACK)
和setBackground(Color.WHITE)
)到填充唯一数字的所有工作
但问题是,我不知道如何在JButton
s的末尾绘制“线索”。我想要的类似于:
有时数字可能只出现在3面、2面甚至1面上
我曾想过设置背景图像,但这是不可能的,因为数字是动态生成的数字的总和(网格是动态的)
那么你知道如何得到这种按钮吗?如果不可能,我还有什么其他选择
提前非常感谢(我真的被卡住了)。没有任何理由不能通过覆盖
paint()
方法在JButton中绘制任何想要的内容
public class KakuroSquare extends JButton
{
/* ... */
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
/* Your paint logic. */
}
}
我曾想过设置背景图像,但这是不可能的,因为数字是动态生成的数字的总和(网格是动态的)
按钮的图像也可以动态生成。您也可以使用自定义组件。在这种情况下,可以非常直接地进行绘制:
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()));
}
}
非常简单和方便的方法是使用
BorderLayout
将JLabels
添加到JButton
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();
}
});
}
}
对Swing(非顶级)容器使用paintComponent(Graphics)
!