2016-11-04 21 views
2

當前程序的目標是在窗口框架中的兩點之間創建厚度不等的線條。到現在爲止還挺好。Java applet如何使用JComboBox中選定的項目

接下來,我想程序識別用戶已經從JComboBox中進行了選擇。

此帖和代碼已更新。 (a)itemStateChanged方法被刪除。它沒有執行,所以不需要在程序中。 (b)每當修改任何對象時,actionPerformed方法都會更新以進行更新。 (c)顏色選擇可能是有史以來最醜陋的開關/案例陳述之一。 (一定會有更好的辦法)。 (d)我實施了Itamar Green的建議,關於JComboBox的定義。謝謝。

我不知道什麼? 注:Java 8.111。 O/S:Windows 8.1。 IDE:Ecilpse Java EE 4.6.0

所有對改進代碼或問題的迴應都非常感謝。

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class ThickPen extends JApplet implements ActionListener 
{ 

private static final long serialVersionUID = 1L; 

JLabel xStartLabel = new JLabel("X Start"); 
JLabel yStartLabel = new JLabel("Y Start"); 
JLabel xStopLabel = new JLabel("X Stop"); 
JLabel yStopLabel = new JLabel("Y Stop"); 
JLabel thickLabel = new JLabel("Thickness"); 

JComboBox<String> myColour; 
String theColour = "black"; 

TextField xStartField = new TextField(4); 
int xStart = 0; 
TextField yStartField = new TextField(4); 
int yStart = 0; 
TextField xStopField = new TextField(4); 
int xStop = 0; 
TextField yStopField = new TextField(4); 
int yStop = 0; 
TextField thicknessField = new TextField(4); 
int thick = 0; 

String[] colourString = {"black","blue","cyan","darkgray","gray","green", 
     "lightGray","magenta","orange","pink","red","white","yellow"}; 

int theIndex = 0; 

public void init() 
{ 
    setSize(550,500); 

    Container content = getContentPane(); 

    setLayout(new FlowLayout()); 
    xStartField.addActionListener(this); 
    yStartField.addActionListener(this); 
    xStopField.addActionListener(this); 
    yStopField.addActionListener(this); 
    thicknessField.addActionListener(this); 
    add(xStartLabel); 
    add(xStartField); 
    add(yStartLabel); 
    add(yStartField); 
    add(xStopLabel); 
    add(xStopField); 
    add(yStopLabel); 
    add(yStopField); 
    add(thickLabel); 
    add(thicknessField); 

    myColour = new JComboBox<String>(colourString); 
      // JComboBox<String> myColour = new JComboBox<String>(colourString); 

    myColour.setSelectedIndex(0);     // start with black 
    myColour.addActionListener(this); 

    add(myColour); 

    content.setBackground(Color.white); 
    content.setForeground(Color.black); 
} 

public void paint(Graphics g) 
{ 
    super.paint(g); 

    Dimension d = getSize(); 
    int fullWidth = d.width; 
    int fullHeight = d.height; 

    int deltaX = 0; 
    int deltaY = 0; 
    boolean xAxis = false; 

    System.out.println("So far Start x y: "+xStart+" "+yStart+" color: "+theColour); 

    if (xStart < 1 || xStart > fullWidth 
      || yStart < 1 || yStart > fullHeight 
      || xStop < 1 || xStop > fullWidth 
      || yStop < 1 || yStop > fullHeight 
      || thick < 1 || thick > fullHeight || thick > fullWidth) { 
     String outStr = "Start and stop numbers must be within this window frame"; 
     String outStr2 = "Current width: "+fullWidth+" height: "+fullHeight; 
     g.setColor(Color.white); 
     g.fillRoundRect(d.width/4, d.height/4, 300, 40, 4, 4); 
     g.setColor(Color.red); 
     g.drawString(outStr, d.width/4+10, d.height/4+15); 
     g.drawString(outStr2, d.width/4+10, d.height/4+30); 
     g.drawString("The index: "+theIndex, d.width/4, 300); 
    } else { 
     g.drawString("", d.width/4, 260); 
     deltaX = Math.abs(xStart - xStop); // determine absolute delta of two Xs 
     deltaY = Math.abs(yStart - yStop); // determine absolute delta of two Ys 
     if (deltaX > deltaY)     // make line thickness based on x axis if 
      xAxis = false;     // the x axis has the most 'room'. 
     else         // else, use the y axis. 
      xAxis = true; 
     pickAColour(g, theColour); 
     drawMyLine(g, xStart, yStart, xStop, yStop, thick, xAxis); 
    } 
    g.drawString("The index: "+theIndex, d.width/4, 300); 
    g.drawString("The color "+ theColour, d.width/4, 330); 
} 

public void drawMyLine(Graphics g, int xStart, int yStart, 
     int xStop, int yStop, int thick, boolean xAxis) 
{ 
    int count = 0; 
    while (count < thick) 
    { 
     g.drawLine(xStart, yStart, xStop, yStop); 
     count++; 
     if (xAxis) { 
      xStart++; 
      xStop++; 
     } else { 
      yStart++; 
      yStop++;  
     } 
    } 
} 

public void pickAColour(Graphics g, String theColour) 
{ 
    switch (theColour) { 
    case "black" : 
     g.setColor(Color.black); 
     break; 
    case "blue" : 
     g.setColor(Color.blue); 
     break; 
    case "cyan" : 
     g.setColor(Color.cyan); 
     break; 
    case "darkgray" : 
     g.setColor(Color.darkGray); 
     break; 
    case "gray" : 
     g.setColor(Color.gray); 
     break; 
    case "green" : 
     g.setColor(Color.green); 
     break; 
    case "lightGray" : 
     g.setColor(Color.lightGray); 
     break; 
    case "magenta" : 
     g.setColor(Color.magenta); 
     break; 
    case "orange" : 
     g.setColor(Color.orange); 
     break; 
    case "pink" : 
     g.setColor(Color.pink); 
     break; 
    case "red" : 
     g.setColor(Color.red); 
     break; 
    case "white" : 
     g.setColor(Color.white); 
     break; 
    case "yellow" : 
     g.setColor(Color.yellow); 
     break; 
    } // end of case statement 
} // end of pickAColour 

public void actionPerformed (ActionEvent ae) 
{ 
Object source=ae.getSource(); 

// xStart 
//  if (source==xStartField) 
//  { 
try { 
    xStart=Integer.parseInt(
      xStartField.getText()); 
} 
catch (NumberFormatException x) { 
    xStart= -1; 
} 
//  } 

// yStart 
//  else if (source==yStartField) 
//  { 
try { 
    yStart=Integer.parseInt(
      yStartField.getText()); 
} 
catch (NumberFormatException x) { 
    yStart= -1; 
} 
//  } 

// xStop 
//  else if (source==xStopField) 
//  { 
try { 
    xStop=Integer.parseInt(
      xStopField.getText()); 
} 
catch (NumberFormatException x) { 
    xStop= -1; 
} 
//  } 

// yStop 
//  else if (source==yStopField) 
//  { 
try { 
    yStop=Integer.parseInt(
      yStopField.getText()); 
} 
catch (NumberFormatException x) { 
    yStop= -1; 
} 
//  } 

// thickness 
//  else if (source==thicknessField) 
//  { 
try { 
    thick=Integer.parseInt(
      thicknessField.getText()); 
} 
catch (NumberFormatException x) { 
    thick= -1; 
} 
//  } else { 

if (source==myColour) { 
JComboBox<String> cb = (JComboBox<String>)ae.getSource(); 
// String theColour = (String)cb.getSelectedItem(); ///can; 
// Integer theIndex = (int)cb.getSelectedIndex(); 

theColour = (String)cb.getSelectedItem(); 
theIndex = (int)cb.getSelectedIndex(); 
} 
//  } 

repaint(); 
} // end of ActionEvent 

} // end of class 
+0

你解決了這個問題嗎? – ItamarG3

+0

感謝您的回覆。我更接近解決方案。你所描述的改變並不能解決問題。我離得更近,因爲程序不會崩潰。 (a)刪除對itemStateChanged的所有引用。該方法沒有執行,因此被刪除。 (b)我大大改變了行動的執行。我將嘗試編輯帖子以反映更改.... – johniccp

回答

0

init方法簡單地把這個

myColour = new JComboBox<String>(colourString); 

,而不是這個

JComboBox<String> myColour = new JComboBox<String>(colourString); 

。 問題是在此定義的myColour之間(在構件部分)

JComboBox<String> myColour; 

並在init()創建的一個區別。您正在初始化Init中的一個,但不是成員部分中的那個,因此當您嘗試使用actionPreformed中的myColour時,Java正嘗試從沒有對象的引用中調用方法。