[英]Gui in java with textarea for Instance variables
I've been trying to makke a working GUI for the past week. 在过去的一周中,我一直在尝试制作可工作的GUI。 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. 我已经创建了侦听器,但是不能做的两件事是在GUI中创建一个显示实例变量的文本区域。 And also the SportCar and the Van constructors require 3 and 4 user inputted numbers, this too I cannot do.
SportCar和Van构造函数也需要3和4个用户输入的数字,我也不能这样做。 Please help I have been stuck on GUI for too long.
请帮助我在GUI上停留太长时间。 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. 另外,如果要在声明了声明的类之外的另一个类中访问它,
testGui.value[i]
像testGui.value[i]
那样调用静态数组。 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. 如果您必须在许多不同的类中访问此数据结构,则还可能考虑使用列表和使用Singleton模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.