JavaSE Swing知识漏洞

1. Frame  的默认布局方式是 BorderLayout,所以如果要使用 setBounds 方法给组件设置具体位置时最好是 将默认的布局方式去掉,设置为null

例如:

//        this.setLayout(null);
        
        gamePanel.setVisible(true);
        pokerPanel.setVisible(true);
        gameInfoPanel.setVisible(true);

        
        pokerPanel.setBounds(10, 10, 580, 200);
        pokerPanel.setBackground(Color.RED);
        
        gamePanel.setBounds(10, 220, 580, 200);
        gamePanel.setBackground(Color.GREEN);
        
        gameInfoPanel.setBounds(10, 430, 580, 140);
        gameInfoPanel.setBackground(Color.BLUE);
        
        add(pokerPanel);
        add(gamePanel);
        add(gameInfoPanel);

效果:无论是哪个panel最后加上去都会铺满整个frame

2011-06-30_215432

 

去掉注释后的正确效果:

 

2011-06-30_215531

 

2. 显示图片

第一种方法是给label放一个 图片,

实例:

package com;  
import java.awt.BorderLayout;  
import java.net.URL;  
import javax.swing.*;  
  
public class ShowImage extends JFrame{  
    /** 
     * @param args 
     */  
    public ShowImage() {  
        JPanel panel=new JPanel(new BorderLayout());  
        JPanel panel2=new JPanel(new BorderLayout());  
        JPanel panel3=new JPanel(new BorderLayout());  
          
        String urlString="D:\\MyEclipse 6.0\\新建文件夹\\Exam\\bin\\images\\winter.jpg";  
        JLabel label=new JLabel(new ImageIcon(urlString));  
          
        URL url=getClass().getResource("/images/orz2.jpg");  
        JLabel label2=new JLabel(new ImageIcon(url));  
          
        URL url2=getClass().getResource("/images/orz.jpg");  
        JLabel label3=new JLabel(new ImageIcon(url2));  
      
        panel.add(label,BorderLayout.CENTER);  
        panel2.add(label2,BorderLayout.CENTER);  
        panel3.add(label3,BorderLayout.CENTER);  
          
        this.getContentPane().setLayout(new BorderLayout());  
        this.getContentPane().add(panel,BorderLayout.CENTER);  
        this.getContentPane().add(panel2,BorderLayout.SOUTH);  
        this.getContentPane().add(panel3,BorderLayout.EAST);  
          
        this.setSize(400, 300);  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        this.setTitle("显示图像");  
        this.setVisible(true);  
    }  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
      ShowImage showImage=new ShowImage();  
    }  
  
} 

label1是使用绝对路径,label2和label3使用的是相对路径,相对于类文件(.class文件)的路径

 

另一种更好的画图方法:使用 g.drawImage

实例:

        Color color=g.getColor();//首先要重画面板,清除前面的所有内容
        g.setColor(this.getBackground());
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(color);
        
        URL url1 = getClass().getResource("images/"+a+".jpg");
        Image image1=getToolkit().getImage(url1);
        g.drawImage(image1, 20, 10, null, this);

 

我的实例:

        URL url1 = getClass().getResource("images/"+a+".jpg");
        Image image1=getToolkit().getImage(url1);
        g.drawImage(image1, 30, 10, null, this);
        
        URL url2 = getClass().getResource("images/"+b+".jpg");
        Image image2=getToolkit().getImage(url2);
        g.drawImage(image2, 164, 10, null, this);
        
        URL url3 = getClass().getResource("images/"+c+".jpg");
        Image image3=getToolkit().getImage(url3);
        g.drawImage(image3, 298, 10, null, this);
        
        URL url4 = getClass().getResource("images/"+d+".jpg");
        Image image4=getToolkit().getImage(url4);
        g.drawImage(image4, 432, 10, null, this);
        
        //之前使用的是label,不是很好
//        URL url1 = getClass().getResource("images/"+a+".jpg");
//        JLabel label1 = new JLabel(new ImageIcon(url1));
//        this.add(label1);
//        
//        URL url2 = getClass().getResource("images/"+b+".jpg");
//        JLabel label2 = new JLabel(new ImageIcon(url2));
//        this.add(label2);
//
//        URL url3 = getClass().getResource("images/"+c+".jpg");
//        JLabel label3 = new JLabel(new ImageIcon(url3));
//        this.add(label3);
//
//        URL url4 = getClass().getResource("images/"+d+".jpg");
//        JLabel label4 = new JLabel(new ImageIcon(url4));
//        this.add(label4);

效果图:

2011-06-30_234757

 

3.setBounds和setLocation

实例:

        JButton newButton = new JButton("New game");
        newButton.setLocation(300, 20);        
//        newButton.setBounds(400, 20,100,30);   
        gamePanel.add(newButton);   

效果是:【左图:setLocation 】没有显示  【右图:setBounds 】 显示

no   yes

 

所以说:如果要让它显示出来,要么使用setBounds或者setLocation加上setSize(两个都要写,不然就不会显示出来了)

转载于:https://www.cnblogs.com/yinger/archive/2011/07/01/2095138.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在《Java学习笔记》中,可以找到有关JavaSE基本知识的内容。其中可能包括Java的基本语法、数据类型、控制流程、面向对象编程等方面的知识。如果您想要打印JavaSE知识的内容,可以使用Java的IO流来实现。您可以使用System.out.println或者其他打印方法来在控制台输出您想要的JavaSE知识。根据您提供的代码片段,在MapText类的main方法中,使用了System.out.println来打印存储在Map中的值,实现了对键值对的打印输出。另外,您还提供了一个匿名内部类的示例,其中重写了MyInterface接口的add方法,通过System.out.println打印了"-------add------"的内容。根据您的需求,您可以根据具体的知识点和代码实现,在适当的位置使用System.out.println或其他打印方法来打印出您想要的JavaSE知识内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [[Java学习笔记doc]-javase基本知识](https://download.csdn.net/download/arthas777/88206261)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [JavaSE知识点整理](https://blog.csdn.net/Kxxindy/article/details/123136578)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值