我正在尝试添加一个菜单栏,通过对象创建调用其他类,以及尝试将图像添加到帧中。
代码确实可以编译,但我在代码中放置的组件似乎都没有出现?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import javax.swing.border.*;
public class Operation
extends JFrame implements ActionListener
{
String op="",pre="";
JFrame calFrame = new JFrame("Calculator");
JMenuBar mainBar = new JMenuBar();
JMenu homeMenu = new JMenu("Home");
JMenuItem welcomeScreen = new JMenuItem("Welcome Screen");
JMenuItem listHome = new JMenuItem("List Accessories");
JMenu jumpMenu = new JMenu("Jump To");
JMenuItem jtSclingTool = new JMenuItem("Scaling Tool");
JMenuItem jtMetricConv = new JMenuItem("Metric Converter");
JMenu helpMenu = new JMenu("Help");
JMenuItem helpScreen = new JMenuItem("Help Screen");
JMenu exitMenu = new JMenu("Exit");
JMenuItem exitToHome = new JMenuItem("Exit to Welcome Screen");
JMenuItem exitProgram= new JMenuItem("Exit Program");
public void Calculator()
{
calFrame.setBounds(0,0,1920,1080);
calFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calFrame.setLayout(null);
JTextField txtid = new JTextField();
txtid.setBounds(20,20,400,100);
calFrame.add(txtid);
txtid.setFont(new java.awt.Font("Lucida Grande", 0, 19));
txtid.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
txtid.setText("0");
txtid.setEnabled(false);
mainBar.add(homeMenu);
mainBar.add(jumpMenu);
mainBar.add(helpMenu);
mainBar.add(exitMenu);
homeMenu.add(welcomeScreen);
homeMenu.add(listHome);
jumpMenu.add(jtSclingTool);
jumpMenu.add(jtMetricConv);
helpMenu.add(helpScreen);
exitMenu.add(exitToHome);
exitMenu.add(exitProgram);
welcomeScreen.addActionListener(this);
listHome.addActionListener(this);
helpScreen.addActionListener(this);
exitToHome.addActionListener(this);
exitProgram.addActionListener(this);
jtSclingTool.addActionListener(this);
jtMetricConv.addActionListener(this);
JButton a = new JButton ("1");
a.setBounds(20,440,100,100);
calFrame.add(a);
JButton b = new JButton ("2");
b.setBounds(120,440,100,100);
calFrame.add(b);
JButton c = new JButton ("3");
c.setBounds(220,440,100,100);
calFrame.add(c);
JButton d = new JButton ("4");
d.setBounds(20,340,100,100);
calFrame.add(d);
JButton e = new JButton ("5");
e.setBounds(120,340,100,100);
calFrame.add(e);
JButton f = new JButton ("6");
f.setBounds(220,340,100,100);
calFrame.add(f);
JButton g = new JButton ("7");
g.setBounds(20,240,100,100);
calFrame.add(g);
JButton h = new JButton ("8");
h.setBounds(120,240,100,100);
calFrame.add(h);
JButton i = new JButton ("9");
i.setBounds(220,240,100,100);
calFrame.add(i);
JButton j = new JButton ("0");
j.setBounds(120,540,100,100);
calFrame.add(j);
JButton k = new JButton ("+");
k.setBounds(320,240,100,200);
calFrame.add(k);
JButton l = new JButton ("-");
l.setBounds(320,140,100,100);
calFrame.add(l);
//
JButton m = new JButton ("*");
m.setBounds(220,140,100,100);
calFrame.add(m);
JButton n = new JButton ("/");
n.setBounds(120,140,100,100);
calFrame.add(n);
JButton o = new JButton ("=");
o.setBounds(320,440,100,200);
calFrame.add(o);
JButton p = new JButton (".");
p.setBounds(220,540,100,100);
calFrame.add(p);
JButton q = new JButton ("Del");
q.setBounds(20,540,100,100);
calFrame.add(q);
JButton r = new JButton ("AC");
r.setBounds(20,140,100,100);
calFrame.add(r);
calFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
a.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
if(op.equals("="))
{
txtid.setText("1");
op="";
}
else if(txtid.getText().equals("0"))
txtid.setText("1");
else
txtid.setText(txtid.getText()+"1");
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
if(op.equals("="))
{
txtid.setText("2");
op="";
}
else if(txtid.getText().equals("0"))
txtid.setText("2");
else
txtid.setText(txtid.getText()+"2");
}
});
c.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
if(op.equals("="))
{
txtid.setText("3");
op="";
}
else if(txtid.getText().equals("0"))
txtid.setText("3");
else
txtid.setText(txtid.getText()+"3");
}
});
d.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
if(op.equals("="))
{
txtid.setText("4");
op="";
}
else if(txtid.getText().equals("0"))
txtid.setText("4");
else
txtid.setText(txtid.getText()+"4");
}
});
e.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
if(op.equals("="))
{
txtid.setText("5");
op="";
}
else if(txtid.getText().equals("0"))
txtid.setText("5");
else
txtid.setText(txtid.getText()+"5");
}
});
f.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
if(op.equals("="))
{
txtid.setText("6");
op="";
}
else if(txtid.getText().equals("0"))
txtid.setText("6");
else
txtid.setText(txtid.getText()+"6");
}
});
g.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae){
if(op.equals("="))
{
txtid.setText("7");
op="";
}
else if(txtid.getText().equals("0"))
txtid.setText("7");
else
txtid.setText(txtid.getText()+"7");
}
});
h.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
if(op.equals("="))
{
txtid.setText("8");
op="";
}
else if(txtid.getText().equals("0"))
txtid.setText("8");
else
txtid.setText(txtid.getText()+"8");
}
});
i.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
if(op.equals("="))
{
txtid.setText("9");
op="";
}
else if(txtid.getText().equals("0"))
txtid.setText("9");
else
txtid.setText(txtid.getText()+"9");
}
});
j.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
if(op.equals("="))
{
txtid.setText("0");
op="";
}
else if(txtid.getText().equals("0"))
txtid.setText("0");
else
txtid.setText(txtid.getText()+"0");
}
});
k.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
op="+";
pre=txtid.getText();
txtid.setText("");
}
});
l.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
op="-";
pre=txtid.getText();
txtid.setText("");
}
});
m.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
op="*";
pre=txtid.getText();
txtid.setText("");
}
});
n.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
op="/";
pre=txtid.getText();
txtid.setText("");
}
});
p.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
if(op.equals("="))
{
txtid.setText("0.");
op="";
}
else
txtid.setText(txtid.getText()+".");
}
});
q.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
op="del";
String str=txtid.getText();
int p = str.length();
if(p-1!=0)
txtid.setText(txtid.getText().substring(0,p-1));
else
txtid.setText("0");
}
});
r.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
op=""; //AC
pre=""; //AC
txtid.setText("0");
}
});
o.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
switch(op)
{
case "":
txtid.setText("0");
break;
case "+":
txtid.setText((Float.parseFloat(pre)+Float.parseFloat(txtid.getText()))+"");
break;
case "-":
txtid.setText((Float.parseFloat(pre)-Float.parseFloat(txtid.getText()))+"");
break;
case "*":
txtid.setText((Float.parseFloat(pre)*Float.parseFloat(txtid.getText()))+"");
break;
case "/":
txtid.setText((Float.parseFloat(pre)/Float.parseFloat(txtid.getText()))+"");
break;
case "=":
break;
}
op="=";
}
});
String buttonCommand = e.getActionCommand();
AlgebraAccessories mainWindow = new AlgebraAccessories();
ListAccessories listAccessories = new ListAccessories();
Scaling sclTool = new Scaling();
MetricConverter mtrConv = new MetricConverter();
if (buttonCommand.equals("Welcome Screen"))
{
calFrame.dispose();
mainWindow.MainWelcomeWindow();
}
else if (buttonCommand.equals("List Accessories"))
{
calFrame.dispose();
listAccessories.selectionWindow();
}
else if (buttonCommand.equals("Scaling Tool"))
{
calFrame.dispose();
sclTool.Scale();
}
else if (buttonCommand.equals("Metric Converter"))
{
calFrame.dispose();
mtrConv.MetricConverter();
}
else if (buttonCommand.equals("Help"))
{
calFrame.dispose();
mainWindow.MainWelcomeWindow();
}
else if (buttonCommand.equals("Exit to Welcome Screen"))
{
calFrame.dispose();
mainWindow.MainWelcomeWindow();
}
else if (buttonCommand.equals("Exit Program"))
{
System.exit(0);
}
}
public static void main(String[] args)
{
Operation mainGui = new Operation();
mainGui.Calculator();
}
}