[英]Gui in java with textarea for Instance variables
在過去的一周中,我一直在嘗試制作可工作的GUI。 我嘗試了網格袋布局,現在嘗試以下布局。 但是,我無法獲得在那里工作所需的所有東西。 拿我下面的代碼
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");
}
}
}
我已經創建了偵聽器,但是不能做的兩件事是在GUI中創建一個顯示實例變量的文本區域。 SportCar和Van構造函數也需要3和4個用戶輸入的數字,我也不能這樣做。 請幫助我在GUI上停留太長時間。 謝謝
首先。 您應該初始化數組。 在循環之前就完成了:
value = new double[args.length];
另外,如果要在聲明了聲明的類之外的另一個類中訪問它, testGui.value[i]
像testGui.value[i]
那樣調用靜態數組。 如果您必須在許多不同的類中訪問此數據結構,則還可能考慮使用列表和使用Singleton模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.