布局
- 边框布局:
- 流式布局:
- 菜单
-化零为整,封装方法(一个界面会设置多个按钮,可以将他们用数组的方法写出来,可以少写一些代码)
String[]string={"矩形","三角形","圆形","三等分点"};
-容器分为顶级容器JFrame和一般容器JPanel
-JFrame是默认的边框布局,JPanel是默认的流式布局
边框布局:
-五个方位,且每个方位只能加一个组件,并不是每个方位都需要加组件
流式布局:
-南北方向:宽度与父类组件一致,其高度可以自设置
-东西方向:高度在没有南北方向的情况下与父类组件一致,有南北方向时需要去掉南北的高度,可以设置宽度
-中间方向:也就是剩下没有设置的范围
-使用绘制的是一块单独的面板JPanel:
-将鼠标监听器加到面板上
-将绘图工具–画笔Graphics从窗体上获取改到面板上获取
菜单
-容器:菜单栏
JMenuBar jMenuBar = new JMenuBar();
-菜单:
JMenu jMenu = new JMenu("文件");
-菜单项
JMenuItem jMenuItem = new JMenuItem ("打开");
-给菜单项加上动作监听器
-文件选择器:打开图片绘制图片
JFileChooser chooser = new JFileChooser();
点击JFileChooser直接复制那一窜代码
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
//返回值的位置在窗体中
int returnVal = chooser.showOpenDialog(jf);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
-创建一个方法,设置窗体的大小、标题、流式布局,可以在属性栏写一个全局变量的尺寸、颜色、边框布局。
-设置面板的五个方位对象,设置其尺寸、背景颜色、边框布局的方位加进窗体中
-设置一个按钮对象的for循环,创建按钮对象,设置其背景颜色,将按钮对象添加到动作监听器对象,把按钮设置在北方向
-设置一个颜色按钮对象的for循环,创建按钮对象,当i=0时,输入文本“MB”,按钮对象添加到动作监听器中,且设置在南方向
-不等于0时,给颜色按钮对象设置其背景颜色,让背景颜色不透明,设置按钮尺寸,将按钮对象添加到动作监听器对象,把按钮设置在南方向
-创建一个菜单方法,设置菜单栏、菜单、菜单项,把菜单加入到菜单栏,菜单项加入到菜单,给菜单项添加动作监听器对象中将菜单栏对象直接加入到JFrame中的菜单栏方法
Dimension dimension=new Dimension(30,30);
String[]string={"矩形","三角形","圆形","三等分点"};
Color[] color={Color.RED,Color.green,Color.BLUE,Color.yellow,Color.ORANGE,Color.cyan,Color.gray,Color.gray};
DimesionLisenter dimesionLisenter=new DimesionLisenter();
public void addMenu(JFrame jFrame){
JMenuBar jMenuBar=new JMenuBar();
JMenu jMenu=new JMenu("文件");
jMenuBar.add(jMenu);
JMenuItem jMenuItem=new JMenuItem("打开");
jMenuItem.addActionListener(dimesionLisenter);
jMenu.add(jMenuItem);
jFrame.setJMenuBar(jMenuBar);
}
public void name(){
JFrame jf=new JFrame();
jf.setSize(800,800);
jf.setTitle("登录界面" );
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel nor=new JPanel();
JPanel sou=new JPanel();
JPanel wes=new JPanel();
JPanel eas=new JPanel();
JPanel cen=new JPanel();
nor.setPreferredSize(dimension);
sou.setPreferredSize(dimension);
wes.setPreferredSize(dimension);
eas.setPreferredSize(dimension);
nor.setBackground(Color.WHITE);
sou.setBackground(Color.WHITE);
wes.setBackground(Color.WHITE);
eas.setBackground(Color.WHITE);
cen.setBackground(Color.pink);
jf.add(nor,BorderLayout.NORTH);
jf.add(sou,BorderLayout.SOUTH);
jf.add(eas,BorderLayout.EAST);
jf.add(wes,BorderLayout.WEST);
jf.add(cen,BorderLayout.CENTER);
for(int i=0;i<string.length;i++){
JButton jButton=new JButton(string[i]);
jButton.setBackground(Color.WHITE);
nor.add(jButton);
jButton.addActionListener(dimesionLisenter);
}
for(int i=0;i<color.length;i++){
JButton jButton=new JButton();
if(i==0){
jButton.setText("MB");
sou.add(jButton);
jButton.addActionListener(dimesionLisenter);
}else {
jButton.setBackground(color[i]);
jButton.setOpaque(true);
jButton.setBorderPainted(false);
jButton.setPreferredSize(dimension);
sou.add(jButton);
jButton.addActionListener(dimesionLisenter);
}
}
addMenu(jf);
jf.setVisible(true);
dimesionLisenter.graphics= jf.getGraphics();
}
public static void main(String[] args) {
Domition domition=new Domition();
domition.name();
}
}
-将菜单栏添加到窗体中,实现,动作监听器对象调用画笔对象等于窗体对象调用得到的画笔
-监听器对象继承动作监听器,在属性栏写上 Graphics 对象,字符串对象,窗体对象
-直接调用动作监听器中的方法,申明一个对象等于动作事件对象调用得到的动作指令
-如果申明的对象不为空值时,if如果申明对象等于“MB”时,申明一个对象等于JColorChooser.showDialog
-申明的按钮对象等于(按钮)动作事件e调用得到的资源,设置被颜色,画笔颜色
-if如果申明对象等于“打开”时,输入JFileChooser chooser = new JFileChooser();
点击JFileChooser直接复制那一窜代码,可能会出现报错,需要导一下包,更改一下返回值的位置在窗体上```
int returnVal = chooser.showOpenDialog(jf);
-if如果都不等于上述的值,则string=str
-当为空值时,申明按钮对象等于(按钮)动作事件e调用得到的资源,申明的颜色按钮对象等于按钮对象调用得到的背景色,画笔对象调用设置的颜色
-最后在主方法中调用,即可
public class DimesionLisenter implements ActionListener {
Graphics graphics = null;
String string;
JFrame jf;
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (!str.equals("")) {
if (str.equals(“MB”)) {
System.out.println(str + “”);
Color color = JColorChooser.showDialog(null, “颜色”, Color.RED);
JButton jButton = (JButton) e.getSource();
jButton.setBackground(color);
graphics.setColor(color);
} else if (str.equals(“打开”)) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
“JPG & GIF Images”, “jpg”, “gif”);
chooser.setFileFilter(filter);
//返回值的位置在窗体中
int returnVal = chooser.showOpenDialog(jf);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
} else {
System.out.println(str + "");
string = str;
}
} else {
JButton jButton = (JButton) e.getSource();
Color color = jButton.getBackground();
graphics.setColor(color);
}
}
}