布局问题,修改如下:
import java.awt.*;
import javax.swing.*;
public class menu
{
public static void main(String args[])
{
JFrame frame=new JFrame("Windows");
Container c=frame.getContentPane();
c.setLayout(null); //-------------------->>>应用null布局才能设置大小
//菜单控件
JMenuBar cpm1=new JMenuBar();
frame.setJMenuBar(cpm1);
JMenu cpm2=new JMenu("File");
String str[]={"New","Open","Save","Exit","Print"};
for (int i=0;i<5;i++)
{
JMenuItem cpm3=new JMenuItem(str[i]);
cpm2.add(cpm3);
}
cpm1.add(cpm2);
//添加菜单控件2
JMenu cpm4=new JMenu("Edit");
String str1[]={"Redo","Undo","Copy","Paste","Delet"};
for (int i=0;i<5;i++)
{
JMenuItem cpm5=new JMenuItem(str1[i]);
cpm4.add(cpm5);
}
cpm1.add(cpm4);
JMenu cpm6=new JMenu("Help");
String str3[]={"About","Online","Registration"};
ButtonGroup cpm7=new ButtonGroup();
for(int i=0;i<3;i++)
{
JRadioButtonMenuItem cpm8=new JRadioButtonMenuItem(str3[i]);
cpm7.add(cpm8);//将按钮添加到按钮组中去了。
cpm6.add(cpm8);//将按钮添加到菜单中去了。
if(i==0) cpm8.setSelected(true);
}
cpm6.addSeparator();
String str4[]={"Relepace","Find","Null"};
for(int i=0;i<3;i++)
{
JCheckBoxMenuItem cpm9=new JCheckBoxMenuItem(str4[i]);
cpm6.add(cpm9);
}
cpm1.add(cpm6);
//添加文本框控件;
JTextArea cpm10=new JTextArea("hello!");
cpm10.setBackground(Color.white);
cpm10.setSize(200,300);
cpm10.setLocation(100,200);
c.add(cpm10);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
}如果没猜错的话你是想做一个累世文本编辑器的东西吧??:)
如果是这样的话,把我修改的那句,以及你对JTextArea定义坐标和大小的代码都去掉,更好。
import java.awt.*;
import javax.swing.*;
public class menu
{
public static void main(String args[])
{
JFrame frame=new JFrame("Windows");
Container c=frame.getContentPane();
c.setLayout(null); //-------------------->>>应用null布局才能设置大小
//菜单控件
JMenuBar cpm1=new JMenuBar();
frame.setJMenuBar(cpm1);
JMenu cpm2=new JMenu("File");
String str[]={"New","Open","Save","Exit","Print"};
for (int i=0;i<5;i++)
{
JMenuItem cpm3=new JMenuItem(str[i]);
cpm2.add(cpm3);
}
cpm1.add(cpm2);
//添加菜单控件2
JMenu cpm4=new JMenu("Edit");
String str1[]={"Redo","Undo","Copy","Paste","Delet"};
for (int i=0;i<5;i++)
{
JMenuItem cpm5=new JMenuItem(str1[i]);
cpm4.add(cpm5);
}
cpm1.add(cpm4);
JMenu cpm6=new JMenu("Help");
String str3[]={"About","Online","Registration"};
ButtonGroup cpm7=new ButtonGroup();
for(int i=0;i<3;i++)
{
JRadioButtonMenuItem cpm8=new JRadioButtonMenuItem(str3[i]);
cpm7.add(cpm8);//将按钮添加到按钮组中去了。
cpm6.add(cpm8);//将按钮添加到菜单中去了。
if(i==0) cpm8.setSelected(true);
}
cpm6.addSeparator();
String str4[]={"Relepace","Find","Null"};
for(int i=0;i<3;i++)
{
JCheckBoxMenuItem cpm9=new JCheckBoxMenuItem(str4[i]);
cpm6.add(cpm9);
}
cpm1.add(cpm6);
//添加文本框控件;
JTextArea cpm10=new JTextArea("hello!");
cpm10.setBackground(Color.white);
cpm10.setSize(200,300);
cpm10.setLocation(100,200);
c.add(cpm10);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
}如果没猜错的话你是想做一个累世文本编辑器的东西吧??:)
如果是这样的话,把我修改的那句,以及你对JTextArea定义坐标和大小的代码都去掉,更好。
我己经OK!!!!