简体   繁体   中英

Gui in java with textarea for Instance variables

I've been trying to makke a working GUI for the past week. I have attempted the grid bag layout and now the following. Howeverr I cannot get all the things I need working to be on there. Take the following code I have

public class testGUI extends JPanel {
  protected static double [] value;  
  JPanel jp = new JPanel();
  JTextArea jt = new JTextArea(10,40);

  public testGUI()
  {
JButton btn1 = new JButton("SportCar");
JButton btn2 = new JButton("Van");
btn1.addActionListener(new ButtonListener());
btn2.addActionListener(new ButtonListener());
jp.add(jt);
add(btn1);
add(btn2);
 }
 public static void main(String[] args) {
for (int i=0; i<args.length;i++)
{   value[i]= Double.parseDouble(args[i]);
}
JFrame frame = new JFrame();
frame.getContentPane().add(new testGUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
  }
}

class ButtonListener implements ActionListener {
ButtonListener() {} 
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("SportCar"))
{   Vehicle car1 = new SportCar(value[0],value[1],value[2]);
    System.out.println("You have made a new Sportcar");

}
else if(e.getActionCommand().equals("Van"))
{   Vehicle car1= new Van(value[0],value[1],value[2],value[3]);
    System.out.println("You have made a new Van");

}
  }
}

I have made the listener but the two things I cannot do are create a text area in the GUI which displays the instance variables. And also the SportCar and the Van constructors require 3 and 4 user inputted numbers, this too I cannot do. Please help I have been stuck on GUI for too long. Thank you

First of all. You should initialize your array. Thats done right before your loop with:

value = new double[args.length];

Also you should call your static array like so testGui.value[i] if you want to access it within another class than the one where you declared it. You might also think about using a List and using the Singleton Pattern if you have to access this data structure throughout many different classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM