chb13062255378
chb13062255378
2017-05-24 17:38
采纳率: 0%
浏览 935

有关 .9.png 点九图的问题(是java程序不是安卓程序)

我在JAVA实践中遇到一个问题,我指的是电脑上的java程序不是安卓程序,在制作好 .9.png格式的文件后应用在程序中,它虽然可以正常显示,但是我添加JButton,JTxetArea等等组件没有按我之前制作图片时的意图显示(就是制作时右边和下边的黑线决定的显示区域),它就是默认一直在中间顶端显示,我尝试了很多图片,不管黑线怎么画,都不会按我的意图而是一直在默认的中间顶端出现。困扰了我好久,如果大家愿意指导,我将感激不尽。

以下是我的代码:
1:NPanel(继承JPanel的实现.9.png图片的)

public class NPanel extends JPanel{

private NinePatch np;
private int skin = 0;
int[] pad;
public NPanel(){     //先执行构造函数,再paintComponent(要add到JFrame且setsize setvisible)
    skin = 0;
    np = loadNinePatch(String.format("/np/skin_%d.9.png", skin));
}
public NPanel(int sk){   //先执行构造函数,再paintComponent(要add到JFrame且setsize setvisible)
    skin = sk;
    np = loadNinePatch(String.format("/np/skin_%d.9.png", skin));
}
public NPanel(int sk, JComponent jc){    
    skin = sk;
    np = loadNinePatch(String.format("/np/skin_%d.9.png", skin));
    this.add(jc);
}
public NPanel(int sk, JComponent jc, LayoutManager LM){  
    skin = sk;
    np = loadNinePatch(String.format("/np/skin_%d.9.png", skin));
    this.add(jc);
    this.setLayout(LM);
}
// 重写绘图方法

/** 
 * @param path 
 *            - the image path. 
 * @return an NinePatch object, or {@code null} if the given path is not 
 *         valid or an error occurs during loading. 
 */  
private NinePatch loadNinePatch(String path) {  
    InputStream stream = this.getClass().getResourceAsStream(path);  
    if (stream != null) {  
        try {  
            return NinePatch.load(stream, true, false);  
        } catch (IOException e) {  
            System.err.println("An error occurs during loading.");  
            e.printStackTrace();  
            return null;  
        }  
    } else {  
        System.err.println("Couldn't find the file: " + path);  
        return null;  
    }  
}  

/** 
 * To improve the repaint speed, the code block contained in 
 * paintComponent() must be able to be executed quickly. For example, we 
 * usually put the load image code out of the paintComponent() for rapid UI 
 * update. 
 *  
 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) 
 */  
@Override  
protected void paintComponent(Graphics g) {
        pad = new int[4];
        np.getPadding(pad);
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;  
        Rectangle clip = g2.getClipBounds();
        System.out.printf("%d,%d,%d,%d\n",clip.x, clip.y, clip.width, clip.height);
        System.out.printf("%d,%d,%d,%d\n",pad[0], pad[1], pad[2], pad[3]);
        if(np != null && clip.x == 0 && clip.y == 0){
            np.draw(g2, clip.x, clip.y, clip.width, clip.height);
       //     np = null;
        }


}  

}
2:这是JFrame的代码

public class GUI extends JFrame{
final private Toolkit toolkit = Toolkit.getDefaultToolkit();
final private Dimension screensize = toolkit.getScreenSize(); //获取屏幕分辨率
private final int Width = screensize.width/3;
private final int Height = screensize.height/2;
private String filePath = "Record.txt";
JTextArea content = new JTextArea();
public GUI(){
this.setBounds(screensize.width/2 - Width/2, screensize.height/2 - Height/2, Width, Height);
Loading();
content.setOpaque(false);
content.setEditable(false);
NPanel npl = new NPanel(3);
npl.add(new JButton("123"));
// npl.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
this.add(npl);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
private void init(){

}
private void Loading(){
    File file = new File(filePath);  
    BufferedReader br = null;  
    StringBuilder sb = new StringBuilder();   //使用StringBuilder 防止多次改变String导致资源 耗费
    while(true){
        try{  
            br = new BufferedReader (new FileReader(file));  
            String str = null;  
            while ((str = br.readLine()) != null){  
                    sb.append(str).append("\n");  
                }  
            content.setText(sb.toString()); 
            break;
        }  
        catch(FileNotFoundException e1){ 
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.exit(0);
            }
        }  
        catch(IOException e1){  
            e1.printStackTrace(); 
            System.exit(0);
        }  
        finally{  
            if(br != null){     //在finally关闭流,防止异常导致无法关闭流
                try{  
                    br.close();  
                }  
                catch(IOException e1){  
                    e1.printStackTrace();  
                    System.exit(0);
                }  
            }  
        }
    }
}

}

然后在主函数 new GUI();就运行了,结果如下

图片说明

对了还有我自己制作好的.9.png图片:

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • caozhy

    这个你最好求助身边的同事或者你的老师。你这么提问没法回答。

    点赞 评论
  • m0_37779956
    m0_37779956 2017-05-25 00:48

    问问题的时候也配个图或者代码说明一下问题所在,不然很难复现你的问题

    点赞 评论

相关推荐