【发布时间】:2015-12-17 11:52:25
【问题描述】:
名为 Project2 的 Class 文件
public class Project2 extends JPanel implements ActionListener
{
private JToggleButton ISLM1103 = new JToggleButton("ISLM1103");
private JToggleButton GENG215 = new JToggleButton("GENG215");
private JToggleButton ESPU107 = new JToggleButton("ESPU107");
private JToggleButton ESPU1452 = new JToggleButton("ESPU1452");
private JToggleButton HSS110 = new JToggleButton("HSS110");
private JToggleButton Calculate = new JToggleButton("Calculate");
private JToggleButton Exit = new JToggleButton("Exit");
public Project2()
{
//Adding the action even to the buttons
ISLM1103.addActionListener(this);
GENG215.addActionListener(this);
ESPU107.addActionListener(this);
ESPU1452.addActionListener(this);
HSS110.addActionListener(this);
Calculate.addActionListener(this);
Exit.addActionListener(this);
//Set the Layout
setLayout(new GridLayout(12,12));
//Adding the buttons
add(ISLM1103);
add(GENG215);
add(ESPU107);
add(ESPU1452);
add(HSS110);
add(Calculate);
add(Exit);
}
public static void main(String[] args)
{
new Project2();
}
public void actionPerformed(ActionEvent actionlistner)
{
// initializing the arraylist
ArrayList<String> courseList = new ArrayList<String>();
if (actionlistner.getActionCommand().equals("ISLM1103"))
courseList.add("ISLM1103");
if (actionlistner.getActionCommand().equals("GENG215"))
courseList.add("GENG215");
if (actionlistner.getActionCommand().equals("ESPU107"))
courseList.add("ESPU107");
if (actionlistner.getActionCommand().equals("ESPU1452"))
courseList.add("ESPU1452");
if (actionlistner.getActionCommand().equals("HSS110"))
courseList.add("HSS110");
if (actionlistner.getActionCommand() == "Calculate")
{
try
{
FileWriter writer = new FileWriter("Course.txt");
BufferedWriter course = new BufferedWriter(writer);
PrintWriter out = new PrintWriter(course);
for(int i = 0; i < courseList.size(); i++)
{
if(courseList.get(i) != null)
out.println(courseList.get(i));
}
out.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
if (actionlistner.getActionCommand() == ("Exit"))
System.exit(0);
}
}
这个叫做项目一,它是我的构造函数,非常好
public class Project1 extends JFrame
{
private Project2 topleft; // Buttons
private Project3 topright; // UAEU - Picture
private Project4 bottomleft; // Schedule
private Project5 bottomright; // Help - Pad
// Constructor
public Project1() throws IOException
{
// Display a title.
setTitle(" UAE University Interactive Course Calculator");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a GridLayout manager.
setLayout(new GridLayout(2, 2));
// Create the custom panels.
topleft = new Project2();
topright = new Project3();
bottomleft = new Project4();
bottomright = new Project5();
// Create the button panel.
add(topleft);
add(topright);
add(bottomleft);
add(bottomright);
// setting formatting options
pack();
setResizable(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
}
// Main method
public static void main(String[] args) throws IOException
{
new Project1();
}
}
只关注项目 2,当出现 If 函数时,看看我是否删除了数组打印出来的动作侦听器,所以这不是数组问题。现在程序不会添加我告诉它在按下按钮时添加的值。有人可以帮忙吗?
编辑!!! = 不管我写什么,我放 .equals 或 == 程序都可以工作。因为计算和退出按钮有效。这并不是说。因此,在您假设它之前,我建议您先尝试该程序,然后再从您的脑海中假设事情
【问题讨论】:
-
if (actionlistner.getActionCommand() == "Calculate") {?不。不要使用==或!=比较字符串。请改用equals(...)或equalsIgnoreCase(...)方法。了解==检查两个 对象引用 是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否以相同的顺序具有相同的字符,这就是这里的重点。令我惊讶的是,您在代码的其他地方正确地做到了——为什么在这里不正确? -
实际上,如果我 put.equals 或 == 并不重要,因为我试图删除 if 语句并保留 courselist.add 并单击计算,它可以工作。我尝试了也可以使用的退出按钮。所以不是这样。您可以复制粘贴我的程序并从主程序中删除项目 3、4、5,它会运行,但您将面临与我相同的问题
-
请阅读minimal reproducible example,然后从 M.C.V.E. 底部链接的页面仔细阅读并了解调试小程序。页面。
-
在构造函数中显示你的
JFrame是不好的设计——构造函数应该初始化你的原始对象实例并且只。将.setVisible(true)移动到您的main(String... args)入口点。
标签: java actionevent