程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java內部窗體設計與窗體定位

Java內部窗體設計與窗體定位

編輯:關於JAVA

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.UIManager;
class Mywindow extends JFrame
{
JButton button1;
JButton button2;
JInternalFrame interframe1,interframe2;
JPanel jpanel;
Mywindow()
{
setVisible(true);
//setSize(400,400)見下面的說明!
jpanel=(JPanel)this.getContentPane();
jpanel.setLayout(new GridLayout(1,2));
button1=new JButton("我");
button2=new JButton("你");
//------------------------------------------------------
interframe1=new JInternalFrame("內窗體A",true,true,true,true);
interframe1.setSize(150,250);
interframe1.setVisible(true); // 必須加上此句 !否則內部窗體無法 !
interframe1.getContentPane().add(button1);
JDesktopPane desk1=new JDesktopPane();
desk1.add(interframe1);
jpanel.add(desk1);
//----------------------------------------------------
interframe2=new JInternalFrame("內窗體B",true,true,true,true);
interframe2.setSize(150,250);
interframe2.setVisible(true);
interframe2.getContentPane().add(button2,BorderLayout.CENTER);
interframe2.getContentPane().add( new JLabel("你好啊"),BorderLayout.NORTH);
JDesktopPane desk2=new JDesktopPane();
desk2.add(interframe2);
jpanel.add(desk2);
}
//--------------------------------------------------------
public Dimension getPreferredSize()
{
return new Dimension(500,500);
/*注意:這種方式設置窗體大小顯示的效果更好!
*如果直接這樣設置setSize(400,400)大小 ,效果不好
* 建議初學者測試!
*/
}
}
public class interframe
{
public static void main(String args[])
{ Mywindow win=new Mywindow();
win.pack();
// win.setVisible(true); 如果有此句,內部窗體不顯示!
// win.setSize(new Dimension(400,400));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = win.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
win.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
win.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved