这几句代码看不出问题
把窗体放到屏幕中央
this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2);
把窗体放到屏幕中央
this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2);
import java.awt.*;
import java.util.*;
import javax.swing.*;////////////////////////////////////////////////////SerbiApp Class
public class SerbinApp extends JFrame
implements ActionListener
{
JPanel contentPanel=new JPanel(); JMenuBar menuBar=new JMenuBar();
JMenu fileMenu=new JMenu("文件");
JMenuItem startMenuItem=new JMenuItem("开始");
Canvas cans; public static void main(String[] args)
{
new SerbinApp();
}
public SerbinApp()
{
super("SerbinApp --W.J.W--");
addWindowListener(new WindowAdapter()
{public void windowCloseing(WindowEvent e){System.exit(0);}}
);
//////////////////////////////////////////////////
setJMenuBar(menuBar);
setContentPane(contentPanel);
contentPanel.setLayout(new BorderLayout());
menuBar.add(fileMenu);
fileMenu.add(startMenuItem);
startMenuItem.addActionListener(this);
////////////////////////////////////////////////
cans=new Canvas();
cans.setSize(400,300);
contentPanel.add(cans,BorderLayout.SOUTH);
pack();
setResizable(false);
setLocation(400,600);
show();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==startMenuItem)
{
cans.repaint();
System.out.println("startMenuItem clicked.") ;
}
}
}
cans.setSize(400,300);和pack();
这两句
把它们去掉,然后加上this.setSize(400,300);
效果就出来了
很明显的
那两句使得JFrame被Canvas覆盖了
但若是那样的话,canvas大小就变了,我想让它固定在(400,300)上,因为要在上面精确作图啊