1
我想編寫一個程序,它檢查選中哪個RadioButton(圓形,實心圓或矩形)。然後用戶單擊畫布字段上的某個地方。它應該識別用戶點擊的座標,然後在該位置繪製任何選定的單選按鈕。Java Canvas Class不繪畫
不幸的是,它不這樣做。事實上,我的代碼甚至沒有達到「ActionListener」。我查了5次代碼,做了一些測試,但仍然無法弄清楚。
希望你能幫助
public class Optionen extends JFrame {
MeinCanvas drawspace = new MeinCanvas();
ButtonGroup btngrp= new ButtonGroup();
int xpos, ypos;
public Optionen(String titel) {
super(titel);
setLayout(new FlowLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1,20,20));
JRadioButton opt1 = new JRadioButton();
JRadioButton opt2 = new JRadioButton();
JRadioButton opt3 = new JRadioButton();
opt1.setActionCommand("Kreis"); //circle
opt1.setActionCommand("Scheibe"); // filled circle
opt1.setActionCommand("Rechteck"); //rectangle
opt1.setText("Kreis");
opt2.setText("Scheibe");
opt3.setText("Rechteck");
btngrp.add(opt1);
btngrp.add(opt2);
btngrp.add(opt3);
panel.add(opt1);
panel.add(opt2);
panel.add(opt3);
add(drawspace);
add(panel);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Optionen fenster = new Optionen("Optionswindow");
fenster.pack();
fenster.setSize(450,500);
fenster.setResizable(true);
fenster.setVisible(true);
}
class MeinCanvas extends Canvas {
class MeinMausAdapter extends MouseAdapter {
public void mousPressed(MouseEvent e) {
xpos = e.getX();
ypos = e.getY();
JOptionPane.showInputDialog(this);
repaint();
}
}
public MeinCanvas() {
addMouseListener(new MeinMausAdapter());
setBackground(Color.black);
setForeground(Color.orange);
}
public void paint(Graphics g) {
ButtonModel btnmdl= m_formauswahl.getSelection();
String label = btnmdl.getActionCommand();
int w = (int) (Math.random()*300);
int h = (int) (Math.random()*300);
if (label.equals("Kreis"))
g.drawOval(xpos, ypos, w, w);
if (label.equals("Scheibe"))
g.fillOval(xpos, ypos, w, h);
if (label.equals("Rechteck"))
g.drawRect(xpos, ypos, w, h);
}
public Dimension getMinimumSize() {
return new Dimension(300,300);
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
}
}
Eclipse的告訴我,該代碼是好的,但每當我運行程序時,它會立即拋出異常。下面是轉儲:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at kapitel10.Optionen$MeinCanvas.paint(Optionen.java:79)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我見過人們經常在遊戲中使用'Canvas'和'JFrame'。你不應該在Swing中使用AWT,因爲一個是重量級的,一個是輕量級的,儘管我也聽說JFrames是重量級的。你能解釋一下嗎? – 2014-09-06 02:01:25
你能不能在我的臉上狠狠地打我?非常感謝氣墊船充滿了鰻魚!有時我感覺遲鈍。我的代碼現在完美運行!一開始我很害怕它會讓我整個週末終於開始運行。我仍然是初學者。再次感謝! – Cappuccino90 2014-09-06 11:21:00