开始学java,使用netbeans,从开始学做桌面软件开始。
1.做一个MDI窗体
新建一个Frame,在上面在放一个JDesktopPane.再放一个菜单。
再建几个继承自JInternalFrame的类。点选菜单时执行以下代码.
private void jTableTestActionPerformed(java.awt.event.ActionEvent evt) {
NewJInternalFrame d = new NewJInternalFrame();
jDesktop1.add(d, javax.swing.JLayeredPane.DEFAULT_LAYER);
d.setVisible(true);
}
2.使MDI中子窗体不重复
点菜单时做执行如下代码.
private void jTableTestActionPerformed(java.awt.event.ActionEvent evt) {
JInternalFrame[] frames = this.jDesktop1.getAllFrames();
boolean isExisted = false;
for (int i = 0; i < frames.length; ++i) {
if (frames[i] instanceof NewJInternalFrame) {
(this.jDesktop1.getDesktopManager()).activateFrame((frames[i]));
isExisted = true;
break;
} }
if (!isExisted) {
NewJInternalFrame d = new NewJInternalFrame();
jDesktop1.add(d, javax.swing.JLayeredPane.DEFAULT_LAYER);
d.setVisible(true);
}
}
3.给MDI窗体加滚动条
首先继承JDesktopPane,做一个JDesktop.代码如下。
在Frame上放一个JScrollPane,在JScrollPane上放上刚做的JDesktop
public class JDesktop extends JDesktopPane
{
public @Override void paint(Graphics g)
{
super.paint(g);
Dimension d = preferredSizeOfAllFrames();
this.setPreferredSize(d);
this.revalidate();
}
/**
* @return 返回最佳desktop尺寸..
* 注:往左移时,尺寸没有变化,需改进。
*/
public Dimension preferredSizeOfAllFrames()
{
JInternalFrame [] array = getAllFrames();
int maxX = 0;
int maxY = 0;
for (int i = 0; i < array.length; i++)
{
if ( array[ i ].isVisible() )
{
int cx;
cx = array[i].getX();
int x = cx + array[i].getWidth();
if (x > maxX) maxX = x;
int cy;
cy = array[i].getY();
int y = cy + array[i].getHeight();
if (y > maxY) maxY = y;
}
}
return new Dimension(maxX, maxY);
}
}
5.使Frame屏幕居中
public MainFrame() {
initComponents();
//以下是netbeans生成的代码
setLocationRelativeTo(null); //只需加这一句
6.设置启动窗体
只需新增一个独立类,这个类只需有以下一个方法.
public class Main {
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);//这句设置第一个显示窗体
}
});
}
}
7.制作PictureBox
直接用label
8.设计登录窗体
将登录窗体设为启动窗体(参考6),点登录按钮时,执行如下代码.
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
if(!this.txtUserNo.getText().equals(""))
{
this.dispose();//这个函数好像已过时了。
new MainFrame().setVisible(true);
}
else
{
JOptionPane.showMessageDialog(this, "对不起!用户名不能为空.", "错误提示", JOptionPane.ERROR_MESSAGE);
}
}
9.添加事件
class TableSelectionFrame extends JFrame implements MouseListener {
private DefaultTableModel model;
private JTable table;
public TableSelectionFrame() {
setTitle("TableSelectionTest");
setSize(300, 200);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
model = new DefaultTableModel(10, 10);
for (int i = 0; i < model.getRowCount(); i++)
for (int j = 0; j < model.getColumnCount(); j++)
model.setValueAt(new Integer((i + 1) * (j + 1)), i, j);
table = new JTable(model);
table.addMouseListener(this);
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(table), "Center");
table.setCellSelectionEnabled(true);
}
public void mouseClicked(MouseEvent e) {
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
System.out.println(model.getValueAt(row, column));
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
10.反射生成子窗口
步骤1:
private void showInternalFrame(String[] args) {
JInternalFrame[] frames = this.jDesktop1.getAllFrames();
boolean isExisted = false;
for (int i = 0; i < frames.length; ++i) {
if (frames[i].getClass().getName().equals(args[0])) {
(this.jDesktop1.getDesktopManager()).activateFrame((frames[i]));
isExisted = true;
break;
}
}
if (!isExisted) {
try {
Class classDefinition = Class.forName(args[0]);
Object obj = classDefinition.newInstance();
jDesktop1.add((JInternalFrame) obj, javax.swing.JLayeredPane.DEFAULT_LAYER);
((JInternalFrame) obj).setVisible(true);
} catch (InstantiationException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
步骤2:调用
showInternalFrame(new String[]{"javaapplication1.DefineEditor"});