..what是繪製點的更有效的方法..
使用現有的Unicode字符,代表模具的面孔。他們從代碼點9856開始。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class DieRoll {
private JComponent ui = null;
int dieStart = 9856;
JLabel dieLabel = new JLabel();
Random r = new Random();
DieRoll() {
initUI();
}
public void initUI() {
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
Font dieFont = null;
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
String firstDie = new String(Character.toChars(dieStart));
for (Font font : fonts) {
if (font.canDisplayUpTo(firstDie)<0) {
// the first font that will render the Die Faces
dieFont = font;
break;
}
}
dieLabel.setFont(dieFont.deriveFont(200f));
ui.add(dieLabel);
setDie();
Action rollDie = new AbstractAction("Roll the Die") {
@Override
public void actionPerformed(ActionEvent e) {
setDie();
}
};
ui.add(new JButton(rollDie), BorderLayout.PAGE_START);
}
private void setDie() {
StringBuilder sb = new StringBuilder("<html><body>");
// convert the numbers to HTML Unicode characters
sb.append(String.format("&#%1s; ", dieStart + r.nextInt(6)));
sb.append(String.format("&#%1s; ", dieStart + r.nextInt(6)));
dieLabel.setText(sb.toString());
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
DieRoll o = new DieRoll();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
如果您包含代碼,將會更容易地幫助您解決問題。此外,如果您將注意力集中在最後一個更具體的問題(mouseListener)中,您可能會受益,因爲第一個問題太寬泛。見[問]。 – walen
沒有示例代碼很難回答這個問題 – Lemonov
[This example](http://stackoverflow.com/questions/21033199/how-do-i-stop-my-paint-method-form-repeating-twice/21033258# 21033258)應該爲你的第一個問題提供一個想法 – MadProgrammer