名为Project2的类文件
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或==无关紧要,无论我写的是什么,程序都能正常工作。因为计算和退出按钮工作。不是那样的。因此,在你假设它之前,我建议你先尝试一下这个程序,然后再从头脑中做出一些事情
答案 0 :(得分:1)
在您的示例中,我希望直接将事件源与按钮进行比较,而不是使用操作命令:
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if (source == ISLM1103) {
/* do something... */
}
/* and so on and so on... */
}
只要您没有goint从某些没有引用切换按钮的第3个组件调用操作,这将是有效的。但在这种情况下,这将是设计缺陷(并且需要使用单独的Action
s)
答案 1 :(得分:1)
ToggleButton#ToggleButton(String)
将text
设置为显示而不是actionCommand
,您似乎期望这样。这一行
private JToggleButton ISLM1103 = new JToggleButton("ISLM1103");
相当于:
private JToggleButton ISLM1103;
{
ISLM1103 = new JToggleButton();
ISLM1103.setText("ISLM1103"); // <--
}