好日子,確定這裏是我的問題:我有通過添加三種不同類型的GUI組件 創建一個小程序,以便用戶可以選擇以下選項:數字高級圖形用戶界面和圖形
- 數量:1 ,2,4,8,16,或這些 數的各種組合
- 類型的數字:圓形,橢圓形,矩形或正方形
- 顏色:紅色,藍色,綠色,黃色,粉紅色,黑色,青色,或洋紅色
我已經做了設計,但問題是繪製圖像,用戶選擇
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ovals extends JApplet implements ItemListener
{
private JCheckBox circleCB,ovalCB,rectangleCB,squareCB;
private Color currentColor = Color.black;
private JRadioButton redRB, greenRB, blueRB, yellowRB, pinkRB, cyanRB, magentaRB, blackRB;
private ButtonGroup ColorSelectBGroup;
private JComboBox numFig;
int Figure;
int num;
public String[] figNum = {"1", "2", "4", "8", "16", "32", "64", "128"};
public void init()
{
Container c = getContentPane();
c.setLayout(null);
circleCB = new JCheckBox("Circle");
ovalCB = new JCheckBox("Oval");
rectangleCB = new JCheckBox("Rectangle");
squareCB = new JCheckBox("Square");
redRB = new JRadioButton("Red");
greenRB = new JRadioButton("Green");
blueRB = new JRadioButton("Blue");
yellowRB = new JRadioButton("Yellow");
pinkRB = new JRadioButton("Pink");
cyanRB = new JRadioButton("Cyan");
magentaRB = new JRadioButton("Magenta");
blackRB = new JRadioButton("Black");
numFig = new JComboBox(figNum);
numFig.setMaximumRowCount(8);
circleCB.setSize(80, 30);
ovalCB.setSize(80, 30);
rectangleCB.setSize(80, 30);
squareCB.setSize(80, 30);
redRB.setSize(80, 30);
greenRB.setSize(80, 30);
blueRB.setSize(80, 30);
yellowRB.setSize(80, 30);
pinkRB.setSize(80, 30);
cyanRB.setSize(80, 30);
magentaRB.setSize(80, 30);
blackRB.setSize(80, 30);
numFig.setSize(80, 30);
circleCB.setLocation(100, 70);
ovalCB.setLocation(100, 110);
rectangleCB.setLocation(100, 150);
squareCB.setLocation(100, 190);
redRB.setLocation(300, 70);
greenRB.setLocation(300, 110);
blueRB.setLocation(300, 150);
yellowRB.setLocation(300, 190);
pinkRB.setLocation(300, 230);
cyanRB.setLocation(300, 270);
magentaRB.setLocation(300, 310);
blackRB.setLocation(300, 350);
numFig.setLocation(200, 70);
circleCB.addItemListener(this);
ovalCB.addItemListener(this);
rectangleCB.addItemListener(this);
squareCB.addItemListener((ItemListener) this);
redRB.addItemListener(this);
greenRB.addItemListener(this);
blueRB.addItemListener(this);
yellowRB.addItemListener(this);
pinkRB.addItemListener(this);
cyanRB.addItemListener(this);
magentaRB.addItemListener(this);
blackRB.addItemListener(this);
numFig.addItemListener(this);
c.add(circleCB);
c.add(ovalCB);
c.add(rectangleCB);
c.add(squareCB);
c.add(redRB);
c.add(greenRB);
c.add(blueRB);
c.add(yellowRB);
c.add(pinkRB);
c.add(cyanRB);
c.add(magentaRB);
c.add(blackRB);
c.add(numFig);
ColorSelectBGroup = new ButtonGroup();
ColorSelectBGroup.add(redRB);
ColorSelectBGroup.add(greenRB);
ColorSelectBGroup.add(blueRB);
ColorSelectBGroup.add(yellowRB);
ColorSelectBGroup.add(pinkRB);
ColorSelectBGroup.add(cyanRB);
ColorSelectBGroup.add(magentaRB);
ColorSelectBGroup.add(blackRB);
}
public void paint (Graphics g)
{
super.paint(g);
g.setColor(Color.orange);
g.drawLine(183, 50, 183, 350);
g.drawLine(291, 50, 291, 350);
}
public void itemStateChanged(ItemEvent e)
{
for (int i = 0; i < 10; i++)
{
switch(Figure)
{
case e.getSource() == circleCB:
(e.getStateChange() == ItemEvent.SELECTED)
e.drawLine(10, 10, 250, 10 + i * 10);
break;
case e.getSource() == rectangleCB:
(e.getStateChange() == ItemEvent.SELECTED)
e.drawRect(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10);
break;
case e.getSource() == ovalCB:
(e.getStateChange() == ItemEvent.SELECTED)
e.drawOval(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10);
break;
case e.getSource() == squareCB:
(e.getStateChange() == ItemEvent.SELECTED)
e.drawRect(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10);
break;
default:
}
if (e.getSource() == redRB)
currentColor = Color.red;
else if (e.getSource() == greenRB)
currentColor = Color.green;
else if (e.getSource() == blueRB)
currentColor = Color.blue;
else if (e.getSource() == yellowRB)
currentColor = Color.yellow;
else if (e.getSource() == pinkRB)
currentColor = Color.pink;
else if (e.getSource() == cyanRB)
currentColor = Color.cyan;
else if (e.getSource() == magentaRB)
currentColor = Color.magenta;
else if (e.getSource() == blackRB)
currentColor = Color.black;
repaint();
}
}
}
什麼我的下一個步驟,我有我不得不調整我的itemStateChanged和油漆的方法,但林不知道如何去了解它,請幫助
這段代碼是否可以編譯? ItemEvent類沒有你試圖調用的方法:'e.drawLine(10,10,250,10 + i * 10);'?你還說,'但問題是繪製用戶選擇的圖像「,但沒有詳細說明你的實際問題。這些細節很重要,所以請幫助我們爲您提供幫助。 –
另請注意,您將要在JPanel的paintComponent方法中執行所有繪圖,而不是在JApplet本身中執行。然後你會在applet中顯示JPanel。請查看Swing教程,特別是繪畫部分。 –
@HovercraftFullOfEels無法運行該文件並查看我的設計,您對調用方法是正確的,回顧您的工作,我不確定在哪裏調用它。但基本上用戶想要選擇一個形狀說圈,程序是想要畫圓圈,我不知道在用戶檢查CB和程序繪製它(如你所知)時,我的代碼放在哪裏。如果我沒有弄錯所有的繪圖都假設在paintMethod中發生,那麼事件方法如何配合? – user3456504