java做窗口的話,需要用swing技術(shù),之后創(chuàng)建JFrame 等組件,即可完成窗口創(chuàng)建工作。
創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括遂川網(wǎng)站建設(shè)、遂川網(wǎng)站制作、遂川網(wǎng)頁(yè)制作以及遂川網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,遂川網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到遂川省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
package inter.frame;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;public class MenuTest { /**
* @param args
*/
JFrame frame; //定義一個(gè)窗口架構(gòu)
JMenuBar mb;//定義窗口的菜單工具欄
JMenu m; //定義菜單
JMenuItem mi1;//定義菜單的內(nèi)容
JMenuItem mi2; //定義菜單的內(nèi)容
public MenuTest() {
initFrame();
initAction();
}
public void initFrame() {
frame = new JFrame();
mb = new JMenuBar();
m = new JMenu("學(xué)生查詢");
mi1 = new JMenuItem("確認(rèn)");
mi2 = new JMenuItem("取消"); m.add(mi1);
m.add(mi2);
mb.add(m);
frame.add(mb, BorderLayout.NORTH);
frame.setSize(300, 300); //設(shè)置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置退出時(shí)關(guān)閉窗口
frame.setVisible(true);//設(shè)置窗口可見(jiàn)
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實(shí)現(xiàn)代碼根據(jù)實(shí)際要求填寫
System.out.println("click");
JOptionPane.showMessageDialog(null, "你點(diǎn)擊了確定按鈕");
}
});
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實(shí)現(xiàn)代碼根據(jù)實(shí)際要求填寫
JOptionPane.showMessageDialog(null, "你點(diǎn)擊了取消按鈕");
}
});
} public static void main(String[] args) {
new MenuTest();//執(zhí)行菜單創(chuàng)建
}}
我這有個(gè)聯(lián)系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 {
/**
* 主模塊,初始化所有子模塊,并設(shè)置主框架的相關(guān)屬性
*/
public SwingTest() {
// 初始化所有模塊
MenuTest menuTest = new MenuTest();
LeftPanel leftPanel = new LeftPanel();
RightPanel rightPanel = new RightPanel();
BottomPanel bottomPanel = new BottomPanel();
CenterPanel centerPanel = new CenterPanel();
// 設(shè)置主框架的布局
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);
// 利用無(wú)名內(nèi)隱類,增加窗口事件
this.addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
// 釋放資源,退出程序
dispose();
System.exit(0);
}
});
setSize(800, 600);
setTitle("* All Groupware Of Swing *");
// 隱藏frame的標(biāo)題欄,此功暫時(shí)關(guān)閉,以方便使用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) {
// "關(guān)于"對(duì)話框的處理
aboutDialog.setVisible(true);
}
});
}
/**
* 返回關(guān)于對(duì)話框
*/
public JDialog getAboutDialog() {
return aboutDialog;
}
/**
* 設(shè)置"關(guān)于"對(duì)話框的外觀及響應(yīng)事件,操作和JFrame一樣都是在內(nèi)容 框架上進(jìn)行的
*/
public void initAboutDialog() {
aboutDialog.setTitle("About");
Container con = aboutDialog.getContentPane();
// Swing 中使用html語(yǔ)句
Icon icon = new ImageIcon("smile.gif");
JLabel aboutLabel = new JLabel("htmlbfont size=5"
+ "centerAll Groupware Of Swing !" + "brKasam.Huang",
icon, JLabel.CENTER);
// JLabel aboutLabel = new JLabel("Swing
// 組件大全簡(jiǎn)體版!",icon,JLabel.CENTER);
con.add(aboutLabel, BorderLayout.CENTER);
aboutDialog.setSize(450, 225);
aboutDialog.setLocation(300, 300);
aboutDialog.addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
dispose();
}
});
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最左邊模塊,繼承JPanel,初始化內(nèi)容為JTree JPanel--+ --JTree
*/
class LeftPanel extends JPanel {
private int i = 0;
public LeftPanel() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultMutableTreeNode select = new DefaultMutableTreeNode("select");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("" + i);
root.add(child);
root.add(select);
child.add(child1);
JTree tree = new JTree(root);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
// 每個(gè)節(jié)點(diǎn)的行高
tree.setRowHeight(20);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
// 內(nèi)隱類不能直接引用外部類tree,1.外部變量可申明為final 2.新建外部類的對(duì)象
JTree tree = (JTree) e.getSource();
DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
i++;
selectNode.add(new DefaultMutableTreeNode("" + i));
// Object s = tree.getLastSelectedPathComponent();//
// getSelectedValue();
// JOptionPane.showMessageDialog(null, s + " is selected",
// "Message !", JOptionPane.YES_OPTION);
}
});
tree.setPreferredSize(new Dimension(100, 300));
// tree.setEnabled(true);
JScrollPane scrollPane = new JScrollPane(tree);
// scrollPane.setSize(100,350);
this.add(scrollPane);
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最下面層模塊,繼承JPanel,初始化內(nèi)容為進(jìn)度條,并由定時(shí)器控制 JPanel--+ --JProcessBar --Timer
*/
class BottomPanel extends JPanel {
private JProgressBar pb;
public BottomPanel() {
pb = new JProgressBar();
pb.setPreferredSize(new Dimension(550, 20));
// 設(shè)置定時(shí)器,用來(lái)控制進(jìn)度條的處理
Timer time = new Timer(1, new ActionListener() {
int counter = 0;
public void actionPerformed(ActionEvent e) {
counter++;
pb.setValue(counter);
Timer t = (Timer) e.getSource();
// 如果進(jìn)度條達(dá)到最大值重新開(kāi)發(fā)計(jì)數(shù)
if (counter == pb.getMaximum()) {
t.stop();
counter = 0;
t.start();
}
}
});
time.start();
pb.setStringPainted(true);
pb.setMinimum(0);
pb.setMaximum(1000);
pb.setBackground(Color.white);
pb.setForeground(Color.red);
this.add(pb);
}
/**
* 設(shè)置進(jìn)度條的數(shù)據(jù)模型
*/
public void setProcessBar(BoundedRangeModel rangeModel) {
pb.setModel(rangeModel);
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最右邊模塊,繼承JPanel,初始化各種按鈕 JPanel--+ --JButton --JToggleButton -- JList --
* JCombox --JCheckBox ....
*/
class RightPanel extends JPanel {
public RightPanel() {
this.setLayout(new GridLayout(8, 1));
// 初始化各種按鈕
JCheckBox checkBox = new JCheckBox("CheckButton");
JButton button = new JButton("OpenFile");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser file = new JFileChooser();
int result = file.showOpenDialog(new JPanel());
if (result == file.APPROVE_OPTION) {
String fileName = file.getSelectedFile().getName();
String dir = file.getCurrentDirectory().toString();
JOptionPane.showConfirmDialog(null,
dir + "" + fileName, "SelectFile",
JOptionPane.YES_OPTION);
}
}
});
JToggleButton toggleButton = new JToggleButton("RadioButton");
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton radioButton1 = new JRadioButton("RadioButton 1", false);
JRadioButton radioButton2 = new JRadioButton("RadioButton 2", false);
// 組合框的處理
JComboBox comboBox = new JComboBox();
comboBox.setToolTipText("ComboBox");
comboBox.addItem("程序員");
comboBox.addItem("分析員");
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox comboBox1 = (JComboBox) e.getSource();
comboBox1.addItem("程序員");
comboBox1.addItem("分析員");
}
});
// 列表框的處理
DefaultListModel litem = new DefaultListModel();
litem.addElement("SAT");
litem.addElement("SUN");
JList list = new JList(litem);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
JList l = (JList) e.getSource();
Object s = l.getSelectedValue();
JOptionPane.showMessageDialog(null, s, "Message !",
JOptionPane.YES_OPTION);
}
});
// 增加按鈕組
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
// 增加各種按鈕到JPanel中顯示
add(button);
add(toggleButton);
add(checkBox);
add(radioButton1);
add(radioButton2);
add(comboBox);
add(list);
this.setBorder(new EtchedBorder(EtchedBorder.LOWERED,
Color.LIGHT_GRAY, Color.blue));
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 中間層模塊,繼承JPanel,初始化頁(yè)簽,并在頁(yè)簽中設(shè)置文本區(qū),表格, 文本區(qū)上下用分隔條分隔 JPanel--+ -JTabbedPane--+
* --Draw --JTable -JTextAreas -JText --JPopupMenu
*/
class CenterPanel extends JPanel {
public CenterPanel() {
JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP);
JTextField textField = new JTextField(
"Text Area,Click Text Button Will select file !");
textField.setActionCommand("textField");
JTextPane textPane = new JTextPane();
textPane.setCursor(new Cursor(Cursor.TEXT_CURSOR));
textPane.setText("Editor,Click the TextArea .");
// textPane.setForeground(new ImageIcon(""));
textPane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
JTextPane textPane = (JTextPane) e.getSource();
textPane.setText("Editor click command success!");
// textField.setText(""+textPane.getText());
}
});
/*
* UpperCaseDocument doc = new Document();
* textField.setDocumentsetDocument(doc);
* doc.addDocumentListener(new DocumentListener() { public void
* changedUpdate(DocumentEvent e){} public void
* removeUpdate(DocumentEvent e){} public void
* insertUpdate(DocumentEvent e) { Document text =
* (Document)e.getDocument(); text.setText("Copy Success !"); } });
*/
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
textField, textPane);
JTable table = new JTable(10, 10);
// UIManager.get("Table.focusCellForeground");
// table.showHorizontalLines(true);
// table.showVerticalLines(true);
// table.setGridColor(Color.blue);
JPanel pane = new JPanel();
pane.add(table.getTableHeader(), BorderLayout.NORTH);
pane.add(table);
tab.addTab("Text Action", splitPane);
// tab.addTab(table.getTableHeader());
tab.addTab("Table Action", pane);
tab.setPreferredSize(new Dimension(500, 600));
this.add(tab);
this.setEnabled(true);
}
}
public static void main(String args[]){
new SwingTest();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Login extends JFrame{
nbsp;public Login() {
nbsp;nbsp;setLayout(new GridLayout(1,2));//網(wǎng)格布局,1行2列,放置左面板和右面板
nbsp;nbsp;setTitle("發(fā)表iPhone說(shuō)說(shuō)");//設(shè)置窗口標(biāo)題
nbsp;nbsp;setSize(550,300);//設(shè)置大小
nbsp;nbsp;setLocationRelativeTo(null);//設(shè)置窗口位置
nbsp;nbsp;setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置窗口關(guān)閉按鈕動(dòng)作
nbsp;nbsp;//左面板
nbsp;nbsp;JPanel leftPanel = new JPanel();
nbsp;nbsp;leftPanel.setBorder(BorderFactory.createTitledBorder("登錄手機(jī)騰訊網(wǎng)"));//給左面板加上邊框,并添加文字
nbsp;nbsp;leftPanel.setLayout(new BorderLayout());
nbsp;nbsp;JPanel leftPanelTop = new JPanel();
nbsp;nbsp;JPanel leftPanelBottom = new JPanel();
nbsp;nbsp;leftPanel.add(leftPanelTop, BorderLayout.CENTER);
nbsp;nbsp;leftPanel.add(leftPanelBottom, BorderLayout.SOUTH);
nbsp;nbsp;leftPanelTop.setLayout(new GridLayout(3,2));//網(wǎng)格布局,3行2列
nbsp;nbsp;nbsp;nbsp;nbsp;
nbsp;nbsp;//========================請(qǐng)?jiān)谙旅鎸懮夏愕拇a
nbsp;nbsp;JLabel qq = new JLabel("QQ號(hào)碼");
nbsp;nbsp;JTextField qqNum = new JTextField();
nbsp;nbsp;JLabel pwd = new JLabel("QQ密碼");
nbsp;nbsp;JPasswordFieldnbsp; qqPwd = new JPasswordField();
nbsp;nbsp;JLabel verify = new JLabel("驗(yàn)證碼");
nbsp;nbsp;JTextField qqVer = new JTextField();
nbsp;nbsp;leftPanelTop.add(qq);
nbsp;nbsp;leftPanelTop.add(qqNum);
nbsp;nbsp;leftPanelTop.add(pwd);
nbsp;nbsp;leftPanelTop.add(qqPwd);
nbsp;nbsp;leftPanelTop.add(verify);
nbsp;nbsp;leftPanelTop.add(qqVer);
nbsp;nbsp;leftPanelBottom.setLayout(new GridLayout(1,2));
nbsp;nbsp;JLabel i_vc = new JLabel(new ImageIcon("images/VerificationCode.jpg"));//這是顯示驗(yàn)證碼的標(biāo)簽,幫你寫好了,后面直接調(diào)用即可。
nbsp;nbsp;JButton bt1 = new JButton("帳號(hào)密碼登陸");
nbsp;nbsp;JButton bt2 = new JButton("帶驗(yàn)證碼登陸");
nbsp;nbsp;JPanel btPanel = new JPanel();
nbsp;nbsp;btPanel.setLayout(new GridLayout(2,1));
nbsp;nbsp;btPanel.add(bt1);
nbsp;nbsp;btPanel.add(bt2);
nbsp;nbsp;leftPanelBottom.add(i_vc);
nbsp;nbsp;leftPanelBottom.add(btPanel);
nbsp;nbsp;//============================================
nbsp;nbsp;add(leftPanel);//將左面板放入窗體中
nbsp;nbsp;//右面板
nbsp;nbsp;JPanel rightPanel = new JPanel();
nbsp;nbsp;rightPanel.setBorder(BorderFactory.createTitledBorder("發(fā)表iPhone說(shuō)說(shuō)"));//給右面板加上邊框,并添加文字
nbsp;nbsp;//========================請(qǐng)?jiān)谙旅鎸懮夏愕拇a
nbsp;nbsp;JTextArea msg = new JTextArea();
nbsp;nbsp;JButton submit = new JButton("馬上發(fā)表說(shuō)說(shuō)");
nbsp;nbsp;rightPanel.setLayout(new BorderLayout());
nbsp;nbsp;rightPanel.add(msg, BorderLayout.CENTER);
nbsp;nbsp;rightPanel.add(submit, BorderLayout.SOUTH);
nbsp;nbsp;//============================================
nbsp;nbsp;add(rightPanel);//將右面板放入窗體中
nbsp;nbsp;setVisible(true);//使窗體可見(jiàn)
nbsp;}
nbsp;public static void main(String[] args){
nbsp;nbsp;new Login();
nbsp;}
}