`
dongzhumao86
  • 浏览: 16419 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

《Java 2 图形设计卷Ⅱ- SWING》第12章 轻量容器

阅读更多

12章 轻量容器

  本章讨论如下Swing轻量容器:
  ·JPanel
  ·JRootPane
  ·JLayeredPane
  ·JTabbedPane
  ·JSplitPane
  JPanelAWTCanvasPanel类的继承类。文本和图形都可以绘制到JPanel实例中,并且JPanel实例可以用作一个通用容器。
  JRootPane是一个包含在窗体、对话框、窗口、内部窗体和Swing小应用程序等Swing顶层容器中的容器。
  JLayeredPane允许把它所包含的组件放置在不同的层中。层控制显示组件的深度。
  JTabbedPane是一个能够包含多个组件的容器。JTabbedPane包含的多个组件一次只能显示一个。JTabbedPane的实例包含能够用于选取当前显示的组件的选项卡。
  JSplitPane包含两个组件,这两个组件由一个分隔体所分隔。可以拖动分隔体以改变每个组件所占据的空间大小。

12.1 JPanel

  JPanel是最简单的Swing组件之一;但它也是使用最多的组件之一。Swing在很多其他组件中使用了JPanel实例;例如,缺省时,JRootPane容器为它的内容窗格和玻璃窗格创建了JPanel实例,正如表12-1所反映的那样。
  JPanel类具有简单容器显示图形的画布的双重功能。图12-1示出的小应用程序创建了三个JPanel实例:一个包含“Name选项卡和文本域的控制面板、一个专门用作显示文本和图形的画布的窗格,以及包含上述控制面板和画面的第三个面板。
  例12-1中列出了图12-1中示出的小应用程序的代码。

12-1 一个使用了三个JPanel实例的小应用程序


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class Test extends JApplet {

        public Test() {

               Container contentPane = getContentPane();

               JPanel panel = new JPanel(new BorderLayout());

               JPanel controlPanel = new JPanel();

               JPanel canvas = new Canvas();

 

               canvas.setBorder(

                       BorderFactory.createLineBorder(Color.black));

 

               controlPanel.add(new JLabel("Name:"));

               controlPanel.add(new JTextField(20));

 

               panel.add(controlPanel, BorderLayout.NORTH);

               panel.add(canvas, BorderLayout.CENTER);

 

               contentPane.add(panel);

        }

}

class Canvas extends JPanel {

        public void paintComponent(Graphics g) {

               super.paintComponent(g);

 

               Dimension size = getSize();

               g.setColor(Color.black);

               g.drawLine(50,50,size.width,size.height);

               g.drawArc(20,40,25,25,0,290);

               g.drawString("A JPanel Used as a Canvas", 20, 20);

        }

}


12.1.1 JPanel的属性

 

12.1.2 JPanel的事件

 

12.1.3 JPanel类总结

 

12.1.4 AWT兼容

 

12.2 JRootPane

 

12.2.1 RootPaneContainer接口

 

12.2.2玻璃空格

12-2 玻璃窗格测试小应用程序


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JApplet {
private Component glassPane = new CustomGlassPane();

public void init() {
JRadioButton rb = new JRadioButton();
JButton button = new JButton("show glass pane");
Container contentPane = getContentPane();

contentPane.setLayout(new FlowLayout());
contentPane.add(button);

rb.setIcon(new ImageIcon("duke_standing.gif"));
rb.setRolloverIcon(new ImageIcon("duke_waving.gif"));

setGlassPane(glassPane);
contentPane.add(rb);

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
glassPane.setVisible(true);
}
});
}
}
class CustomGlassPane extends JPanel {
private JButton button;
private String displayString = "glass pane ... ";

public CustomGlassPane() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
setVisible(false);
}
});
}
public void paintComponent(Graphics g) {
Dimension size = getSize();
FontMetrics fm = g.getFontMetrics();
int sw = fm.stringWidth(displayString);
int fh = fm.getHeight();

g.setColor(Color.blue);

for(int row=fh; row < size.height; row += fh)
for(int col=0; col < size.width; col += sw)
g.drawString(displayString, col, row);
}
}


12.2.3内容窗格

 

12-3 一个定制的内容窗格


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ContentPaneTest extends JApplet {
private JButton button = new JButton("show glass pane");

public void init() {
setGlassPane(new CustomGlassPane(button));
setContentPane(new CustomContentPane(button));

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getGlassPane().setVisible(true);
}
});
}
}
class CustomContentPane extends JPanel {
private ImageIcon rain = new ImageIcon("rain.gif");
private ImageIcon punch = new ImageIcon("punch.gif");
private int rainw = rain.getIconWidth();
private int rainh = rain.getIconHeight();

public CustomContentPane(JButton button) {
add(button);
add(new JLabel("I'm a JLabel in the Content Pane",
punch, SwingConstants.RIGHT));
}
public void paintComponent(Graphics g) {
Dimension size = getSize();

for(int row=0; row < size.height; row += rainh)
for(int col=0; col < size.width; col += rainw)
rain.paintIcon(this,g,col,row);
}
}
class CustomGlassPane extends JPanel {
private JButton button;
private Point ulhc = new Point(20,20), last;
private String displayString =
"I'm on the glass pane - drag me around!";

public CustomGlassPane(JButton b) {
button = b;

setOpaque(false);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
last = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
setVisible(false);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point drag = e.getPoint();
ulhc.x += drag.x - last.x;
ulhc.y += drag.y - last.y;

repaint();

last.x = drag.x;
last.y = drag.y;
}
});
}
public void paintComponent(Graphics g) {
FontMetrics fm = g.getFontMetrics();
int sw = fm.stringWidth(displayString);
int sh = fm.getHeight();
int ascent = fm.getAscent();

g.drawRect(ulhc.x, ulhc.y, sw + 10, sh + 10);
g.drawString(displayString,
ulhc.x + 5, ulhc.y + ascent + 5);
}
}


12.2.4 JRootPane属性

 

12.2.5 JRootPane事件

 

12-4 一个传递事件的玻璃窗格


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;

public class Test extends JApplet {
Component glassPane = new AnnotationPane();
JCheckBox annotations = new JCheckBox("annotations");

public void init() {
createContainerHierarchy();
setupGlassPane();
}
private void createContainerHierarchy() {
Container contentPane = getContentPane();

JPanel controlPanel = new JPanel();
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);

readFile(textArea.getDocument());

controlPanel.add(annotations);

contentPane.add(scrollPane, "Center"); // scroll pane
contentPane.add(controlPanel, "South"); // panel

textArea.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
System.out.println("enter");
}
});
}
private void setupGlassPane() {
setGlassPane(glassPane);

annotations.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED)
glassPane.setVisible(true);
else
glassPane.setVisible(false);
}
});
}
private void readFile(Document doc) {
try {
Reader in = new FileReader("Test.java");
char[] buff = new char[4096];
int next;

while ((next = in.read(buff, 0, buff.length)) != -1)
doc.insertString(
doc.getLength(), new String(buff, 0, next), null);
}
catch(Exception e) {
System.out.println("interruption");
}
}
}
class AnnotationPane extends JPanel {
private Icon annotations[] = {
new ImageIcon("annotation.gif"),
new ImageIcon("annotation_1.gif"),
new ImageIcon("annotation_2.gif")
};
public void paintComponent(Graphics g) {
annotations[0].paintIcon(this, g, 400, 50);
annotations[1].paintIcon(this, g, 10, 150);
annotations[2].paintIcon(this, g, 10, 265);
}
}


12.2.6 JRootPane类总结

 

12.2.7 AWT兼容

 

12.3 JLayoutPane

 

12.3.1回顾轻量组件的层序

 

12-5 添加到一个内容窗格中的按钮的程序


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JApplet {
Container cp = getContentPane();

private final Component[] comps = {
new JButton(), new JButton(),
new JButton(), new JButton(),
new JButton(), new JButton(),
};
public void init() {
cp.setLayout(null);

for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];
cp.add(button);

String t = "Button #";

t += i + " Index: " + getIndexOf(button);

button.setText(t);
button.setBounds(i*50, i*50, 350, 75);
System.out.println("Adding: " + button.getText());
}
}
private int getIndexOf(Component button) {
int ncomponents = cp.getComponentCount();

for(int i=0; i < ncomponents; ++i) {
Component c = cp.getComponent(i);
if(button == c)
return i;
}
return -1;
}
}


 

12.3.2为组件分配器

 

12-6 所包含在一个分层窗格中的组件分配到层中


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JApplet {
private JLayeredPane lp = new JLayeredPane();

private Integer[] layers = {
JLayeredPane.FRAME_CONTENT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.PALETTE_LAYER,
JLayeredPane.MODAL_LAYER,
JLayeredPane.POPUP_LAYER,
JLayeredPane.DRAG_LAYER,
};
private final Component[] comps = {
new JButton("Frame Content"), new JButton("Default"),
new JButton("Palette"), new JButton("Modal"),
new JButton("Popup"), new JButton("Drag"),
};
public void init() {
setContentPane(lp);
lp.setLayout(null);

for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];

System.out.println("Adding: " + button.getText());

lp.setLayer(button, layers[i].intValue());
lp.add(button);
}
for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];
String t = button.getText();
String replacement = new String("Layer: ");

replacement += t + "(" + lp.getLayer(button) + "),";
replacement += " Index: " + lp.getIndexOf(button);

button.setText(replacement);
button.setBounds(i*50, i*50, 350, 75);
}
}
}


 

12.3.3指定同一层中组件的位置

 

12-7 同一层中放置组件的缺省方式


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JApplet {
private JLayeredPane lp = new JLayeredPane();

private Integer[] layers = {
JLayeredPane.FRAME_CONTENT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.PALETTE_LAYER,
JLayeredPane.MODAL_LAYER,
JLayeredPane.POPUP_LAYER,
JLayeredPane.DRAG_LAYER,
};
private final Component[] comps = {
new JButton("Frame Content"), new JButton("Default"),
new JButton("default 1"), new JButton("default 2"),
new JButton("Palette"), new JButton("Modal"),
new JButton("Popup"), new JButton("Drag"),
};
public void init() {
setContentPane(lp);
lp.setLayout(null);

for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];

lp.setLayer(button, layers[i].intValue());
lp.add(button);
}
for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];
String t = button.getText();
String replacement = new String("Layer: ");

replacement += t + "(" + lp.getLayer(button) + "),";
replacement += " Index: " + lp.getIndexOf(button);
replacement += " Position: " +
lp.getPosition(button);

button.setText(replacement);
button.setBounds(i*50, i*50, 350, 75);
}
}
}


12-8 显式地设置同层中组件的位置


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JApplet {
private JLayeredPane lp = new JLayeredPane();

private Integer[] layers = {
JLayeredPane.FRAME_CONTENT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.DEFAULT_LAYER,
JLayeredPane.PALETTE_LAYER,
JLayeredPane.MODAL_LAYER,
JLayeredPane.POPUP_LAYER,
JLayeredPane.DRAG_LAYER,
};
private final Component[] comps = {
new JButton("Frame Content"), new JButton("Default"),
new JButton("default 1"), new JButton("default 2"),
new JButton("Palette"), new JButton("Modal"),
new JButton("Popup"), new JButton("Drag"),
};
public void init() {
setContentPane(lp);
lp.setLayout(null);

for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];
String t = button.getText();

lp.setLayer(button, layers[i].intValue());
lp.add(button);
}
for(int i=0; i < comps.length; ++i) {
AbstractButton button = (AbstractButton)comps[i];
String t = button.getText();
String replacement = new String("Layer: ");

if(t.equals("Default"))
lp.setPosition(button, 2);
else if(t.equals("default 2"))
lp.setPosition(button, 0);

replacement += t + "(" + lp.getLayer(button) + "),";
replacement += " Index: " + lp.getIndexOf(button);
replacement += " Position: " +
lp.getPosition(button);

button.setText(replacement);
button.setBounds(i*50, i*50, 350, 75);
}
}
}


 

 

12.3.4使用拖动层

 

12-9 使用拖动层


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JApplet {
public void init() {
setContentPane(new CustomContentPane());
}
}
class CustomContentPane extends JLayeredPane {
private ImageIcon rain = new ImageIcon("rain.gif");
private ImageIcon punch = new ImageIcon("punch.gif");
private ImageIcon skelly = new ImageIcon("skelly.gif");
private int rainw = rain.getIconWidth();
private int rainh = rain.getIconHeight();

private JLabel[] labels = {
new JLabel("I stay put", punch, SwingConstants.RIGHT),
new JLabel("Drag me around!",
skelly, SwingConstants.RIGHT),
};

public CustomContentPane() {
Dragger listener = new Dragger();
JCheckBox onDragLayer = new JCheckBox("Drag Layer");

// JLayeredPane has a null layout by default
setLayout(new FlowLayout());

onDragLayer.setOpaque(false);

add(onDragLayer);
add(labels[0]);
add(labels[1]);

labels[1].addMouseMotionListener(listener);
labels[1].addMouseListener(listener);

setLabelText();

onDragLayer.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
setLayer(labels[1],
JLayeredPane.DRAG_LAYER.intValue());
}
else {
setLayer(labels[1],
JLayeredPane.DEFAULT_LAYER.intValue());

}
setLabelText();
validate();
}
});
}
public void paintComponent(Graphics g) {
Dimension size = getSize();

for(int row=0; row < size.height; row += rainh)
for(int col=0; col < size.width; col += rainw)
rain.paintIcon(this,g,col,row);
}
private void setLabelText() {
for(int i=0; i < labels.length; ++i) {
JLabel label = labels[i];
String t = new String("Layer: ");

t += "(" + getLayer(label) + "),";
t += " Index: " + getIndexOf(label);

label.setText(t);
}
}
}
class Dragger extends MouseAdapter
implements MouseMotionListener {
Point press = new Point();
boolean dragging = false;

public void mousePressed(MouseEvent event) {
press.x = event.getX();
press.y = event.getY();
dragging = true;
}
public boolean isDragging() {
return dragging;
}
public void mouseReleased(MouseEvent event) {
dragging = false;
}
public void mouseClicked(MouseEvent event) {
dragging = false;
}
public void mouseMoved(MouseEvent event) {
// don't care
}
public void mouseDragged(MouseEvent event) {
Component c = (Component)event.getSource();

if(dragging) {
Point loc = c.getLocation();
Point pt = new Point();
pt.x = event.getX() + loc.x - press.x;
pt.y = event.getY() + loc.y - press.y;
c.setLocation(pt.x, pt.y);
c.getParent().repaint();
}
}
}


12.3.5 JLayeredPane属性

 

12.3.6 JLayeredPane类总结

 

12.3.7 AWT兼容

 

12.4 JTablePane

 

12-10 JTabbedPane的一个简单实例


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
JTabbedPane tp = new JTabbedPane();
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();

panelOne.add(new JButton("button in panel 1"));
panelTwo.add(new JButton("button in panel 2"));

tp.add(panelOne, "Panel One");
tp.addTab("Panel Two",
new ImageIcon("document.gif"),
panelTwo,
"tooltip text");

contentPane.setLayout(new BorderLayout());
contentPane.add(tp);
}
}


12.4.1选项卡的位置

 

12.4.2 JTabbedPane的属性

 

12-11 JTabbedPane的实例设置选项卡放置位置


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JApplet {
private JTabbedPane tp = new JTabbedPane(SwingConstants.BOTTOM);
private JComboBox combo = new JComboBox();

public Test() {
Container contentPane = getContentPane();
JPanel comboPanel = new JPanel();
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();

tp.add(panelOne, "Panel One");
tp.addTab("Panel Two",
new ImageIcon("document.gif"),
panelTwo,
"tooltip text");

combo.addItem("TOP");
combo.addItem("LEFT");
combo.addItem("RIGHT");
combo.addItem("BOTTOM");

setComboValue();

comboPanel.add(new JLabel("Tab Placement:"));
comboPanel.add(combo);

contentPane.setLayout(new BorderLayout());
contentPane.add(comboPanel, BorderLayout.NORTH);
contentPane.add(tp, BorderLayout.CENTER);

combo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JComboBox cb = (JComboBox)e.getSource();
int state = e.getStateChange();

if(state == ItemEvent.SELECTED) {
String s = (String)cb.getSelectedItem();

if(s.equals("TOP"))
tp.setTabPlacement(JTabbedPane.TOP);
else if(s.equals("LEFT"))
tp.setTabPlacement(JTabbedPane.LEFT);
else if(s.equals("RIGHT"))
tp.setTabPlacement(JTabbedPane.RIGHT);
else if(s.equals("BOTTOM"))
tp.setTabPlacement(JTabbedPane.BOTTOM);

tp.validate();
}
}
});
}
private void setComboValue() {
int placement = tp.getTabPlacement();
String selectedItem = null;

switch(placement) {
case JTabbedPane.TOP:
selectedItem = "TOP";
break;
case JTabbedPane.LEFT:
selectedItem = "LEFT";
break;
case JTabbedPane.RIGHT:
selectedItem = "RIGHT";
break;
case JTabbedPane.BOTTOM:
selectedItem = "BOTTOM";
break;
}
combo.setSelectedItem(selectedItem);
}
}


12.4.3 JTabbedPane的事件

 

12-12 响应JTabbedPane的变化事件


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Test extends JApplet {

public Test() {
Container contentPane = getContentPane();
JTabbedPane tp = new JTabbedPane();
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();

tp.add(panelOne, "Panel One");
tp.addTab("Panel Two",
new ImageIcon("document.gif"),
panelTwo,
"tooltip text");

contentPane.setLayout(new BorderLayout());
contentPane.add(tp);

tp.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JTabbedPane tabbedPane =
(JTabbedPane)e.getSource();

int index = tabbedPane.getSelectedIndex();
String s = tabbedPane.getTitleAt(index);

showStatus(s + " selected");
}
});
}
}


12.4.4 JTabbedPane类总结

 

12.5 JSplitPane

 

12-13 运行中的JSplitPane


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
JButton left, right;
JSplitPane sp = new JSplitPane();
ControlPanel cp = new ControlPanel(sp);

contentPane.setLayout(new BorderLayout());
contentPane.add(sp, BorderLayout.CENTER);
contentPane.add(cp, BorderLayout.NORTH);

left = (JButton)sp.getTopComponent();
right = (JButton)sp.getBottomComponent();

System.out.println("left button minimum size: " +
left.getMinimumSize());
System.out.println("right button minimum size: " +
right.getMinimumSize());

}
}

class ControlPanel extends JPanel {
private JSplitPane sp;

public ControlPanel(JSplitPane splitPane) {
sp = splitPane;

JComboBox dividerSize = new JComboBox();
JComboBox orientation = new JComboBox();

JCheckBox continuous = new JCheckBox(
"Continuous Layout");
JCheckBox oneTouch = new JCheckBox(
"One Touch Expandable");

Integer initialSize = new Integer(sp.getDividerSize());
dividerSize.addItem(initialSize.toString());
dividerSize.addItem("10");
dividerSize.addItem("20");
dividerSize.addItem("30");
dividerSize.addItem("40");

orientation.addItem("horizontal");
orientation.addItem("vertical");

int initialOrientation = sp.getOrientation();
if(initialOrientation == JSplitPane.HORIZONTAL_SPLIT)
orientation.setSelectedItem("horizontal");
else
orientation.setSelectedItem("vertical");

boolean initialContinuousLayout = sp.isContinuousLayout();
if(initialContinuousLayout)
continuous.setSelected(true);

add(oneTouch);
add(continuous);
add(new JLabel("Divider Size:"));
add(dividerSize);
add(new JLabel("Orientation:"));
add(orientation);

oneTouch.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED)
sp.setOneTouchExpandable(true);
else
sp.setOneTouchExpandable(false);
}
});
continuous.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED)
sp.setContinuousLayout(true);
else
sp.setContinuousLayout(false);
}
});
dividerSize.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JComboBox combo = (JComboBox)e.getSource();
String s = (String)combo.getSelectedItem();

sp.setDividerSize(Integer.parseInt(s));
}
});
orientation.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JComboBox combo = (JComboBox)e.getSource();
String s = (String)combo.getSelectedItem();

if(s.equals("horizontal"))
sp.setOrientation(
JSplitPane.HORIZONTAL_SPLIT);
else
sp.setOrientation(JSplitPane.VERTICAL_SPLIT);
}
});
}
}


12.5.1 JSplitPane属性

 

12.5.2 JSplitPane事件

 

12-14 监视一个拆分窗格的分隔体的位置


import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;

public class Test extends JApplet {
public Test() {
Container contentPane = getContentPane();
JSplitPane sp = new JSplitPane();

contentPane.add(sp, BorderLayout.CENTER);
sp.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
if(e.getPropertyName().equals(
JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
JSplitPane jsp = (JSplitPane)e.getSource();

int dl = jsp.getDividerLocation();

JButton lb = (JButton)jsp.getLeftComponent();
JButton rb = (JButton)jsp.getRightComponent();

showStatus("Divider Location: " + dl + " / " +
lb.getText() + ": " + lb.getSize() + " / " +
rb.getText() + ": " + rb.getSize());
}
}
});
}
}


12.5.3 JSplitPane类总结

 略

12.5.4 AWT兼容

 略

 

 

 

 

分享到:
评论

相关推荐

    Java语言程序设计-基础篇-中文ppt-第十二章.ppt

    Java语言程序设计中的第十二章主要探讨了图形用户界面(GUI)的基础知识,特别是使用Java进行GUI编程。GUI是用户与计算机系统交互的一种直观方式,Java提供了强大的API来支持这一功能,包括AWT(Abstract Window ...

    JAVA语言程序设计-第十二章 高级用户界面GUI设计

    在本章节"JAVA语言程序设计-第十二章 高级用户界面GUI设计"中,我们将深入探讨Java如何创建美观且功能丰富的图形用户界面(GUI)。GUI是计算机软件中与用户进行交互的一种方式,使得非程序员也能轻松操作应用程序。...

    Java语言程序设计基础篇课后题答案-Chapter12GUIBasics.pdf

    AWT组件是重量级的,因为它们依赖于本地操作系统的界面元素,而Swing组件是轻量级的,因为它们完全由Java编写,不依赖于操作系统的界面元素。文档中提到***ponent是所有Java GUI组件类的根类,而JComponent是Swing ...

    java swing [英文第2版]

    Swing库为开发者提供了丰富的组件,如按钮、文本框、滚动面板、菜单等,这些组件都是基于Java的轻量级(也称为“无状态”或“抽象窗口工具包”,AWT)框架构建的。与AWT不同,Swing组件不是直接在操作系统窗口系统上...

    Java程序设计第十一章图形用户界面.ppt

    Swing组件是轻量级的,它们不直接依赖于操作系统,因此具有更好的可移植性和更丰富的功能。Swing还提供了更现代的外观和感觉,以及更多的定制选项。 在GUI设计中,组件(Component)是构成界面的基本单元,它们可以...

    JBuider第十二章:Swing3.rar

    在JBuider第十二章中,我们深入探讨了Swing库的使用,这是一个针对Java程序员的集成开发环境(IDE),特别强化了Swing组件的创建和管理。 Swing是Java Foundation Classes (JFC)的一部分,它提供了大量的组件,如...

    第十二章图形用户界面设计.ppt

    Swing组件更加轻量级,且支持模态对话框、JTable、JTree等复杂控件,同时提供了更好的可定制性和跨平台兼容性。Swing中的组件通常继承自AWT组件,但它们通常不直接与操作系统交互,而是通过Java的抽象层实现,这使得...

    计算机 swing

    Swing 组件基于轻量级组件,这意味着它们并不依赖于特定的操作系统平台,因此可以跨多个操作系统运行。 ### 2. 代码结构分析 #### 2.1 基本框架 ```java package com.sheng.www; import java.awt.*; import java....

    MyEclipse.6.Java.开发中文教程(11-18章).zip

    第十二章“开发Struts 2 Spring应用”深入讲解了Struts 2与Spring的结合,这是现代Web应用的常见架构。开发者将学习到如何利用MyEclipse的工具支持,创建Struts 2 Action,配置Spring依赖注入,以及实现事务管理。 ...

    banqueinteractivewithlist_javaswing_plan42k_

    Swing是轻量级的,意味着它不依赖于操作系统提供的图形服务,而是使用Java本身来绘制组件,这使得Swing应用程序在各种操作系统上有一致的外观和行为。 在这个"banqueinteractivewithlist"项目中,我们很可能会遇到...

    Java基础知识学习--自己整理.zip

    Java Enterprise Edition(Java EE)是一套用于开发企业级应用的标准和API,Spring框架则是其中常用的一个轻量级容器,提供依赖注入和AOP等功能。 以上只是Java基础知识的一部分,实际的Java学习还包括网络编程、...

    java开源包10

    JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor ...

    java开源包1

    JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor ...

    MyEclipse 6 Java 开发中文教程

    第五,第十四章和第十二章分别讲述了JSF(JavaServer Faces)和Struts 2+Spring的应用开发。JSF是Java EE的用户界面框架,提供了丰富的组件库和生命周期管理。Struts 2结合Spring,形成了一种强大的MVC架构,适合...

    java开源包6

    JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor ...

    java开源包2

    JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor ...

    java开源包9

    JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor ...

    java开源包4

    JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor ...

    java开源包101

    JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor ...

Global site tag (gtag.js) - Google Analytics