JFrame、JPanel 、Layout开发的简单例子
写了Java这么久,居然发现想手写一个带网格袋布局的JFrame,还不记得怎么写,写了这么多代码真不敢说记得所有细节。
幸好,只要记清楚概念就能快速开发。首先,明确一下3种容器类的差别和用途:
| No. | 区别 |
|---|---|
| 1 | Panel
JPanel用于放置其他控件,也包含其他panels。 |
| 2 | Frame
JFrame 是包含 title and a border的第一层级的容器,其中通过布局设置JPanel或其他控件的位置。 |
| 3 | Window
JWindow是不包含 title and a border的第一层级的容器。 |
JFrame构造函数
| S.N. | Description |
|---|---|
| 1 | JFrame()
Constructs a new frame that is initially invisible. |
| 2 | JFrame(GraphicsConfiguration gc)
Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title. |
| 3 | JFrame(String title) 带标题
Creates a new, initially invisible Frame with the specified title. |
| 4 | JFrame(String title, GraphicsConfiguration gc)
Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device. |
Class methods
S.N. Method & Description
1 protected void addImpl(Component comp, Object constraints, int index)
Adds the specified child Component. 添加子控件 2 protected JRootPane createRootPane()
Called by the constructor methods to create the default rootPane. 3 protected void frameInit()
Called by the constructors to init the JFrame properly. 4 AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JFrame. 5 Container getContentPane() 返回容器Pane,经常使用
Returns the contentPane object for this frame. 6 int getDefaultCloseOperation()
Returns the operation that occurs when the user initiates a "close" on this frame. 7 Component getGlassPane()
Returns the glassPane object for this frame. 8 Graphics getGraphics()
Creates a graphics context for this component. 9 JMenuBar getJMenuBar() 返回菜单
Returns the menubar set on this frame. 10 JLayeredPane getLayeredPane()
Returns the layeredPane object for this frame. 11 JRootPane getRootPane() 返回根容器,经常使用
Returns the rootPane object for this frame. 12 TransferHandler getTransferHandler()
Gets the transferHandler property. 13 static boolean isDefaultLookAndFeelDecorated()
Returns true if newly created JFrames should have their Window decorations provided by the current look and feel. 14 protected boolean isRootPaneCheckingEnabled()
Returns whether calls to add and setLayout are forwarded to the contentPane. 15 protected String paramString()
Returns a string representation of this JFrame. 16 protected void processWindowEvent(WindowEvent e)
Processes window events occurring on this component. 17 void remove(Component comp)
Removes the specified component from the container. 18 void repaint(long time, int x, int y, int width, int height)
Repaints the specified rectangle of this component within time milliseconds. 19 void setContentPane(Container contentPane)
Sets the contentPane property. 20 void setDefaultCloseOperation(int operation)
Sets the operation that will happen by default when the user initiates a "close" on this frame. 21 static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
Provides a hint as to whether or not newly created JFrames should have their Window decorations (such as borders, widgets to close the window, title...) provided by the current look and feel. 22 void setGlassPane(Component glassPane)
Sets the glassPane property. 23 void setIconImage(Image image) 设置图标
Sets the image to be displayed as the icon for this window. 24 void setJMenuBar(JMenuBar menubar) 设置菜单
Sets the menubar for this frame. 25 void setLayeredPane(JLayeredPane layeredPane)
Sets the layeredPane property. 26 void setLayout(LayoutManager manager) 设置布局
Sets the LayoutManager. 27 protected void setRootPane(JRootPane root)
Sets the rootPane property. 28 protected void setRootPaneCheckingEnabled(boolean enabled)
Sets whether calls to add and setLayout are forwarded to the contentPane. 29 void setTransferHandler(TransferHandler newHandler)
Sets the transferHandler property, which is a mechanism to support transfer of data into this component. 30 void update(Graphics g)
Just calls paint(g).
代码1:第一个JFrame简单例子
import java.awt.*;
import javax.swing.*; public class JFrameExample implements Runnable
{
public static void main(String[] args)
{
JFrameExample example = new JFrameExample();
SwingUtilities.invokeLater(example);
} public void run()
{
JFrame frame = new JFrame("My JFrame Example");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 200));
frame.pack();
frame.setVisible(true);
}
}
代码2,另一个写法
public static void main(String[] args)
{
// schedule this for the event dispatch thread (edt)
SwingUtilities.invokeLater(new Runnable() {
public void run() {
displayJFrame();
}
});
} static void displayJFrame()
{
JFrame frame = new JFrame("My JFrame Example");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 200));
frame.pack();
frame.setVisible(true);
}
代码3:带有GridLayout的简单JFrame
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class SwingContainerDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel; public SwingContainerDemo(){
prepareGUI();
} public static void main(String[] args){
SwingContainerDemo swingContainerDemo = new SwingContainerDemo();
swingContainerDemo.showJPanelDemo();
} private void prepareGUI(){
mainFrame = new JFrame("Java Swing ");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("headerLabel ", JLabel.CENTER);
statusLabel = new JLabel("statusLabel ",JLabel.CENTER); statusLabel.setSize(350,100); msglabel = new JLabel("Welcome SWING ", JLabel.CENTER); controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
} private void showJPanelDemo(){
headerLabel.setText("Container in action: JPanel"); JPanel panel = new JPanel();
panel.setBackground(Color.magenta);
panel.setLayout(new FlowLayout());
panel.add(msglabel); controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
代码4:带有GridLayout和按钮事件的JFrame
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class SwingContainerDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel; public SwingContainerDemo(){
prepareGUI();
} public static void main(String[] args){
SwingContainerDemo swingContainerDemo = new SwingContainerDemo();
swingContainerDemo.showJFrameDemo();
} private void prepareGUI(){
mainFrame = new JFrame("Java Swing");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("Head", JLabel.CENTER);
statusLabel = new JLabel("Status",JLabel.CENTER); statusLabel.setSize(350,100); msglabel = new JLabel("Welcome SWING", JLabel.CENTER); controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
} private void showJFrameDemo(){
headerLabel.setText("Container in action: JFrame"); final JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setLayout(new FlowLayout());
frame.add(msglabel);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
frame.dispose();
}
});
JButton okButton = new JButton("Open a Frame");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusLabel.setText("A Frame shown to the user.");
frame.setVisible(true);
}
});
controlPanel.add(okButton);
mainFrame.setVisible(true);
}
}
代码5:带有菜单的JFrame
import java.awt.*;
import java.awt.event.*; public class SwingMenuDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel; public SwingMenuDemo(){
prepareGUI();
} public static void main(String[] args){
SwingMenuDemo swingMenuDemo = new SwingMenuDemo();
swingMenuDemo.showMenuDemo();
} private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
} private void showMenuDemo(){
//create a menu bar
final JMenuBar menuBar = new JMenuBar(); //create menus
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
final JMenu aboutMenu = new JMenu("About");
final JMenu linkMenu = new JMenu("Links"); //create menu items
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.setMnemonic(KeyEvent.VK_N);
newMenuItem.setActionCommand("New"); JMenuItem openMenuItem = new JMenuItem("Open");
openMenuItem.setActionCommand("Open"); JMenuItem saveMenuItem = new JMenuItem("Save");
saveMenuItem.setActionCommand("Save"); JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.setActionCommand("Exit"); JMenuItem cutMenuItem = new JMenuItem("Cut");
cutMenuItem.setActionCommand("Cut"); JMenuItem copyMenuItem = new JMenuItem("Copy");
copyMenuItem.setActionCommand("Copy"); JMenuItem pasteMenuItem = new JMenuItem("Paste");
pasteMenuItem.setActionCommand("Paste"); MenuItemListener menuItemListener = new MenuItemListener(); newMenuItem.addActionListener(menuItemListener);
openMenuItem.addActionListener(menuItemListener);
saveMenuItem.addActionListener(menuItemListener);
exitMenuItem.addActionListener(menuItemListener);
cutMenuItem.addActionListener(menuItemListener);
copyMenuItem.addActionListener(menuItemListener);
pasteMenuItem.addActionListener(menuItemListener); final JCheckBoxMenuItem showWindowMenu =
new JCheckBoxMenuItem("Show About", true);
showWindowMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(showWindowMenu.getState()){
menuBar.add(aboutMenu);
}else{
menuBar.remove(aboutMenu);
}
}
}); final JRadioButtonMenuItem showLinksMenu =
new JRadioButtonMenuItem("Show Links", true);
showLinksMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(menuBar.getMenu(3)!= null){
menuBar.remove(linkMenu);
mainFrame.repaint();
}else{
menuBar.add(linkMenu);
mainFrame.repaint();
}
}
}); //add menu items to menus
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(showWindowMenu);
fileMenu.addSeparator();
fileMenu.add(showLinksMenu);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem); //add menu to menubar
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);
menuBar.add(linkMenu); //add menubar to the frame
mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
} class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
statusLabel.setText(e.getActionCommand()
+ " JMenuItem clicked.");
}
}
}
代码6:带有BorderLayout的JFrame
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class SwingLayoutDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel; public SwingLayoutDemo(){
prepareGUI();
} public static void main(String[] args){
SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();
swingLayoutDemo.showBorderLayoutDemo();
} private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
} private void showBorderLayoutDemo(){
headerLabel.setText("Layout in action: BorderLayout"); JPanel panel = new JPanel();
panel.setBackground(Color.darkGray);
panel.setSize(300,300);
BorderLayout layout = new BorderLayout();
layout.setHgap(10);
layout.setVgap(10);
panel.setLayout(layout); panel.add(new JButton("Center"),BorderLayout.CENTER);
panel.add(new JButton("Line Start"),BorderLayout.LINE_START);
panel.add(new JButton("Line End"),BorderLayout.LINE_END);
panel.add(new JButton("East"),BorderLayout.EAST);
panel.add(new JButton("West"),BorderLayout.WEST);
panel.add(new JButton("North"),BorderLayout.NORTH);
panel.add(new JButton("South"),BorderLayout.SOUTH); controlPanel.add(panel); mainFrame.setVisible(true);
}
}
另一个复杂一些的例子:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities; import java.awt.*;
import java.awt.event.*; import javax.swing.*; public class MainFrame {
private JFrame mainFrame;
private JLabel statusLabel;
private JLabel msglabel;
private JTabbedPane tabbedPane;
private JTabbedPane bodyTabbedPane;
private JPanel favoritePanel;
private JPanel historyPanel;
private JPanel recyclePanel;
private JPanel configPanel;
private JPanel aboutPanel; public MainFrame() {
prepareGUI();
} private void prepareGUI() {
mainFrame = new JFrame("昕友软件");
mainFrame.setSize(1024, 768);
mainFrame.setLayout(new BorderLayout()); mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
statusLabel = new JLabel("昕友软件", JLabel.CENTER);
msglabel = new JLabel("昕友软件", JLabel.CENTER);
tabbedPane = new JTabbedPane(); mainFrame.add(tabbedPane, BorderLayout.CENTER);
mainFrame.add(msglabel, BorderLayout.NORTH);
mainFrame.add(statusLabel, BorderLayout.SOUTH); } private void buildHome() {
ImageIcon icon = null;
bodyTabbedPane = new JTabbedPane();
bodyTabbedPane.setTabPlacement(JTabbedPane.LEFT);
for (int i = 0; i < 9; i++) {
bodyTabbedPane.addTab("Tab"+i, icon, new JButton("计算" + i), "计算"
+ i);
} tabbedPane.addTab(" Home ", icon, bodyTabbedPane, "Home");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
} private void buildFavorite() {
ImageIcon icon = null;
favoritePanel = new JPanel();
tabbedPane.addTab(" Favorites ", icon, favoritePanel, "收藏夹");
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
} private void buildHistory() {
ImageIcon icon = null;
historyPanel = new JPanel();
tabbedPane.addTab(" History ", icon, historyPanel, "History");
tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
} private void buildRecycle() {
ImageIcon icon = null;
recyclePanel = new JPanel();
tabbedPane.addTab(" Recycle ", icon, recyclePanel, "Recycle");
tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
} private void buildConfig() {
ImageIcon icon = null;
configPanel = new JPanel();
tabbedPane.addTab(" Config ", icon, configPanel, "Config");
tabbedPane.setMnemonicAt(4, KeyEvent.VK_5);
} private void buildAbout() {
ImageIcon icon = null;
aboutPanel = new JPanel();
tabbedPane.addTab(" About ", icon, aboutPanel, "About");
tabbedPane.setMnemonicAt(5, KeyEvent.VK_6);
} public void showJFrame() { buildHome(); buildFavorite();
buildHistory();
buildRecycle();
buildConfig();
buildAbout();
mainFrame.setVisible(true);
} public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainFrame mainUI = new MainFrame();
mainUI.showJFrame();
}
}); }
}
代码7:带有CardLayout的JFrame
CardLayout是非常特殊的布局,其他的布局是控制相对位置,它的用法是把控件置于不同“卡片”,其目的是控制每次只显示一个卡片(绝不同时出现两个卡片)。
就像一个堆栈,每次放在最前的只有一个元素。比如需要制作Step by Step的桌面程序,使用CardLayout就非常方便。
其中用于控制展示卡片的关键代码:cardLayout.show(panel,name);name就是卡片的name
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class SwingLayoutDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel; public SwingLayoutDemo(){
prepareGUI();
} public static void main(String[] args){
SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();
swingLayoutDemo.showCardLayoutDemo();
} private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
} private void showCardLayoutDemo(){
headerLabel.setText("Layout in action: CardLayout"); final JPanel panel = new JPanel();
panel.setBackground(Color.CYAN);
panel.setSize(300,300); CardLayout layout = new CardLayout();
layout.setHgap(10);
layout.setVgap(10);
panel.setLayout(layout); JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(new JButton("确定"));
buttonPanel.add(new JButton("取消")); JPanel textBoxPanel = new JPanel(new FlowLayout()); textBoxPanel.add(new JLabel("姓名:"));
textBoxPanel.add(new JTextField(20)); panel.add("Button", buttonPanel);
panel.add("Text", textBoxPanel); final DefaultComboBoxModel panelName = new DefaultComboBoxModel(); panelName.addElement("Button");
panelName.addElement("Text"); final JComboBox listCombo = new JComboBox(panelName);
listCombo.setSelectedIndex(0); JScrollPane listComboScrollPane = new JScrollPane(listCombo); JButton showButton = new JButton("展示"); showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "";
if (listCombo.getSelectedIndex() != -1) {
CardLayout cardLayout = (CardLayout)(panel.getLayout());
cardLayout.show(panel,
(String)listCombo.getItemAt(listCombo.getSelectedIndex()));
}
statusLabel.setText(data);
}
}); controlPanel.add(listComboScrollPane);
controlPanel.add(showButton);
controlPanel.add(panel); mainFrame.setVisible(true);
}
}
其他不常用布局1:带有FlowLayout的JFrame
FlowLayout就是一种从左到右(left-to-right)排列的布局,代码略。
其他不常用布局2:带有GroupLayout的JFrame
GroupLayout在一些场景上可以替代布局之王网格袋布局,代码会更清晰.
有几篇不错的文章:
http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html
http://www.cnblogs.com/taoweiji/archive/2012/12/10/2812221.html
http://saupb.blog.163.com/blog/static/471241782010105105525362/
代码8:带有GridBagLayout的JFrame
真正的布局之王,复杂但值得学习
GridBagConstraints可以从11个方面来进行控制和操纵,也可以给你提供一些帮助。这些内容是:
- Gridx——组件的横向坐标
- Girdy——组件的纵向坐标
- Gridwidth——组件的横向宽度,也就是指组件占用的列数,这与HTML的colspan类似
- Gridheight——组件的纵向长度,也就是指组件占用的行数,这与HTML的rowspan类似
- Weightx——指行的权重,告诉布局管理器如何分配额外的水平空间
- Weighty——指列的权重,告诉布局管理器如何分配额外的垂直空间
- Anchor——告诉布局管理器组件在表格空间中的位置
- Fill——如果显示区域比组件的区域大的时候,可以用来控制组件的行为。控制组件是垂直填充,还是水平填充,或者两个方向一起填充
- Insets——指组件与表格空间四周边缘的空白区域的大小
- Ipadx—— 组件间的横向间距,组件的宽度就是这个组件的最小宽度加上ipadx值
- ipady—— 组件间的纵向间距,组件的高度就是这个组件的最小高度加上ipady值
最好的教程在这里:
http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame; public class SwingLayoutDemo {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false; public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
} JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
} button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c); button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c); button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c); button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c); button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);
} /**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane.
addComponentsToPane(frame.getContentPane()); //Display the window.
frame.pack();
frame.setVisible(true);
} public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
JFrame、JPanel 、Layout开发的简单例子的更多相关文章
- java socket编程开发简单例子 与 nio非阻塞通道
基本socket编程 1.以下只是简单例子,没有用多线程处理,只能一发一收(由于scan.nextLine()线程会进入等待状态),使用时可以根据具体项目功能进行优化处理 2.以下代码使用了1.8新特 ...
- 如何开发一个简单的HTML5 Canvas 小游戏
原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...
- Hibernate4.2.4入门(一)——环境搭建和简单例子
一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- 一个简单例子:贫血模型or领域模型
转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...
- NHibernate的简单例子
NHibernate的简单例子 @(编程) [TOC] 因为项目需求,搭了一个NHibernate的例子,中间遇到了一些问题,通过各种方法解决了,在这里记录一下最后的结果. 1. 需要的dll Com ...
- c# spring aop的简单例子
刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...
- Junit4的最简单例子
首先是JUnit的百度百科介绍: 接下来是JUnit4的最简单例子,使用的开发软件为Eclispe(里面集成了JUnit): 1.建立一个java project: 2.建立一个java class: ...
- (转)Java中使用正则表达式的一个简单例子及常用正则分享
转自:http://www.jb51.net/article/67724.htm 这篇文章主要介绍了Java中使用正则表达式的一个简单例子及常用正则分享,本文用一个验证Email的例子讲解JAVA中如 ...
随机推荐
- 【转】shell expect spawn、linux expect 用法小记 看着舒服点
使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写却不知其然.本文用一个最短的例子说明脚本的原理. 脚本代码如下: ######## ...
- yum安装nginx 加载image_filter 加载方式
通过yum安装的nginx 加载image_filter方式方法: yum install -y gd-devel libgd gcc libgdyum install -y nginx-module ...
- Hadoop3集群搭建之——hive添加自定义函数UDF
上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...
- 2018.11.06 NOIP训练 简单的计数问题(计数dp)
传送门 直接f[i][j]f[i][j]f[i][j]表示已经到第iii个位置已经找到jjj个的方案数. 简单转移一下就行了. 代码
- springboot同时使用thymeleaf和jsp模板
语言:javaEE 框架:springboot+thymeleaf.jsp模板引擎 背景:学习springboot过程中想同时使用thymeleaf和jsp访问(官方不建议) 步骤: 1) 在pom ...
- ACM-ICPC 2018 沈阳赛区网络预赛 B Call of Accepted(表达式求值)
题目链接:https://nanti.jisuanke.com/t/31443 相关前置链接 https://www.cnblogs.com/dolphin0520/p/3708602.html ht ...
- shell常见命令
awk awk是个很好用的东西,大量使用在linux系统分析的结果展示处理上.并且可以使用管道, input | awk '' | output 1.首先要知道形式 awk 'command' fi ...
- Oracle修改数据库的日期
---Oracle数据库更新时间字段数据时的sql语句---格式化时间插入 update t_invite_activityinfo set endtime=to_date('2019-10-30 1 ...
- IntelliJ IDEA 2017版 spring-boot 拦截器的操作三种方式
一.注解方式 @WebServlet(urlPatterns = "/myServlet") public class MyServlet extends HttpServlet ...
- IntelliJ IDEA 2017版 使用笔记(五) 模板 live template自定义设置(二) ;postfix使用;IDE快捷键使用
一.live template 活模板 就像这个单词的含义一样,live template就是一个高效的提高代码,书写速度的方式,(live template位置File-----settin ...