我这有个联系swing的界面大全,里面包含了常用的主件,可以直接用
package demo.synth;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.synth.SynthLookAndFeel;
import javax.swing.table.*;
public class SwingTest extends JFrame {
/**
* 主模块,初始化所有子模块,并设置主框架的相关属性
*/
public SwingTest() {
// 初始化所有模块
MenuTest menuTest = new MenuTest();
LeftPanel leftPanel = new LeftPanel();
RightPanel rightPanel = new RightPanel();
BottomPanel bottomPanel = new BottomPanel();
CenterPanel centerPanel = new CenterPanel();
// 设置主框架的布局
Container c = this.getContentPane();
// c.setLayout(new BorderLayout());
this.setJMenuBar(menuTest);
c.add(leftPanel, BorderLayout.WEST);
c.add(rightPanel, BorderLayout.EAST);
c.add(centerPanel, BorderLayout.CENTER);
c.add(bottomPanel, BorderLayout.SOUTH);
// 利用无名内隐类,增加窗口事件
this.addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
// 释放资源,退出程序
dispose();
System.exit(0);
}
});
setSize(800, 600);
setTitle("* All Groupware Of Swing *");
// 隐藏frame的标题栏,此功暂时关闭,以方便使用window事件
// setUndecorated(true);
setLocation(200, 150);
this.setVisible(true);
}
// //////////////////////////////////////////////////////////////////////////
/**
* 菜单栏处理模块 JMenuBar --+ --JMenu--+ --JMenuItem --ActionListener
*
*/
class MenuTest extends JMenuBar {
private JDialog aboutDialog;
/**
* 菜单初始化操作
*/
public MenuTest() {
JMenu fileMenu = new JMenu("File");
JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_E);
JMenuItem aboutMenuItem = new JMenuItem("About...", KeyEvent.VK_A);
fileMenu.add(exitMenuItem);
fileMenu.add(aboutMenuItem);
this.add(fileMenu);
aboutDialog = new JDialog();
initAboutDialog();
// 菜单事件
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(0);
}
});
aboutMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// "关于"对话框的处理
aboutDialog.setVisible(true);
}
});
}
/**
* 返回关于对话框
*/
public JDialog getAboutDialog() {
return aboutDialog;
}
/**
* 设置"关于"对话框的外观及响应事件,操作和JFrame一样都是在内容 框架上进行的
*/
public void initAboutDialog() {
aboutDialog.setTitle("About");
Container con = aboutDialog.getContentPane();
// Swing 中使用html语句
Icon icon = new ImageIcon("smile.gif");
JLabel aboutLabel = new JLabel(""
+ "
java编程思想里现成的例子。
// Demonstration of File dialog boxes.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FileChooserTest extends JFrame {
private JTextField
fileName = new JTextField(),
dir = new JTextField();
private JButton
open = new JButton("Open"),
save = new JButton("Save");
public FileChooserTest() {
JPanel p = new JPanel();
open.addActionListener(new OpenL());
p.add(open);
save.addActionListener(new SaveL());
p.add(save);
add(p, BorderLayout.SOUTH);
dir.setEditable(false);
fileName.setEditable(false);
p = new JPanel();
p.setLayout(new GridLayout(2,1));
p.add(fileName);
p.add(dir);
add(p, BorderLayout.NORTH);
}
class OpenL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
// Demonstrate "Open" dialog:
int rVal = c.showOpenDialog(FileChooserTest.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
fileName.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
}
if(rVal == JFileChooser.CANCEL_OPTION) {
fileName.setText("You pressed cancel");
dir.setText("");
}
}
}
class SaveL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
// Demonstrate "Save" dialog:
int rVal = c.showSaveDialog(FileChooserTest.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
fileName.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
}
if(rVal == JFileChooser.CANCEL_OPTION) {
fileName.setText("You pressed cancel");
dir.setText("");
}
}
}
public static void main(String[] args) {
FileChooserTest f = new FileChooserTest();
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 200);
f.setVisible(true);
}
} ///:~
授人以鱼
不如授人以渔
看看这个吧!!——其实就是对话框嘛!
http://blog.csdn.net/driftingice/archive/2004/08/19/79535.aspx