Java Swing教程 - Java Swing字体...
我们可以使用标题,图标或两者来显示标签。
我们可以使用标题,图标或两者来显示标签。...
getTabCount()方法返回JTabbedPane中的选项卡数。
getTabCount()方法返回JTabbedPane中的选项卡数。...
我们可以使用它的索引来获取表示选项卡的组件。
JPanel contactsPanel = tabbedPane.getTabComponentAt(1);
我们可以使用它的索引来获取表示选项卡的组件。...
public void addTab(String title, Component component) public void addTab(String title, Icon icon, Component component) public void addTab(String title, Icon icon, Component component, String tip) public void insertTab(String title, Icon icon, Component component, String tip, int index)
import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.KeyEvent; //from w w w . j a v a2 s. c o m import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTabbedPane; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Main { static Color colors[] = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA }; static void add(JTabbedPane tabbedPane, String label) { int count = tabbedPane.getTabCount(); JButton button = new JButton(label); button.setBackground(colors[count]); tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label); } public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); String titles[] = { "A", "B", "C", "D", "E", "F" }; for (int i = 0, n = titles.length; i < n; i++) { add(tabbedPane, titles[i]); } frame.add(tabbedPane, BorderLayout.CENTER); frame.setSize(400, 150); frame.setVisible(true); } }
import java.awt.BorderLayout; import java.awt.event.KeyEvent; //from w ww .ja va 2 s . c o m import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTabbedPane; public class TabLocation { static void add(JTabbedPane tabbedPane, String label, int mnemonic) { int count = tabbedPane.getTabCount(); JButton button = new JButton(label); tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label); tabbedPane.setMnemonicAt(count, mnemonic); } public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tabbedPane = new JTabbedPane(); String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" }; int mnemonic[] = { KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P, KeyEvent.VK_A }; for (int i = 0, n = titles.length; i < n; i++) { add(tabbedPane, titles[i], mnemonic[i]); } tabbedPane.setTabPlacement(JTabbedPane.BOTTOM); frame.add(tabbedPane, BorderLayout.CENTER); frame.setSize(400, 150); frame.setVisible(true); } }
我们可以使用以下方法更改背景颜色,启用禁用选项卡。
public void setBackgroundAt(int index, Color background) public void setForegroundAt(int index, Color foreground) public void setEnabledAt(int index, boolean enabled) public void setDisabledIconAt(int index, Icon disabledIcon)
import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTabbedPane; public class Main { static Color colors[] = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA }; static void add(JTabbedPane tabbedPane, String label, int mnemonic) { int count = tabbedPane.getTabCount(); JButton button = new JButton(label); tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label); tabbedPane.setMnemonicAt(count, mnemonic); } public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tabbedPane = new JTabbedPane(); String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" }; int mnemonic[] = { KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P, KeyEvent.VK_A }; for (int i = 0, n = titles.length; i < n; i++) { add(tabbedPane, titles[i], mnemonic[i]); tabbedPane.setBackgroundAt(i,colors[i]); } frame.add(tabbedPane, BorderLayout.CENTER); frame.setSize(400, 150); frame.setVisible(true); } }
要删除选项卡,您可以使用删除特定选项卡removeTabAt(int index),remove(int index)或remove(Component component)
使用removeAll()删除所有选项卡
import java.awt.BorderLayout; import java.awt.event.KeyEvent; //w w w. j av a 2s .c o m import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTabbedPane; public class Main { static void add(JTabbedPane tabbedPane, String label, int mnemonic) { int count = tabbedPane.getTabCount(); JButton button = new JButton(label); tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label); tabbedPane.setMnemonicAt(count, mnemonic); } public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" }; int mnemonic[] = { KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P, KeyEvent.VK_A }; for (int i = 0, n = titles.length; i < n; i++) { add(tabbedPane, titles[i], mnemonic[i]); } tabbedPane.removeAll(); frame.add(tabbedPane, BorderLayout.CENTER); frame.setSize(400, 150); frame.setVisible(true); } }
import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTabbedPane; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class TabSample { static void add(JTabbedPane tabbedPane, String label) { JButton button = new JButton(label); tabbedPane.addTab(label, button); } public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tabbedPane = new JTabbedPane(); String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" }; for (int i = 0, n = titles.length; i < n; i++) { add(tabbedPane, titles[i]); } ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changeEvent) { JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource(); int index = sourceTabbedPane.getSelectedIndex(); System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index)); } }; tabbedPane.addChangeListener(changeListener); frame.add(tabbedPane, BorderLayout.CENTER); frame.setSize(400, 150); frame.setVisible(true); } }
import java.awt.BorderLayout; //w w w. ja va 2 s . c om import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTabbedPane; public class TabSampleTabLayoutPolicy { static void add(JTabbedPane tabbedPane, String label) { JButton button = new JButton(label); tabbedPane.addTab(label, button); } public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" }; for (int i = 0, n = titles.length; i < n; i++) { add(tabbedPane, titles[i]); } frame.add(tabbedPane, BorderLayout.CENTER); frame.setSize(400, 150); frame.setVisible(true); } }
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; public class JDK6TabbedPaneExample { private JFrame frame = new JFrame(); private JTabbedPane tabbedPane = new JTabbedPane(); private JButton addTabButton = new JButton("Add Tab"); private ImageIcon closeXIcon = new ImageIcon("C:/CloseX.gif"); private Dimension closeButtonSize; private int tabCounter = 0; public JDK6TabbedPaneExample() { addTabButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ add(); } }); closeButtonSize = new Dimension(closeXIcon.getIconWidth() + 2, closeXIcon.getIconHeight() + 2); frame.add(tabbedPane, BorderLayout.CENTER); frame.add(addTabButton, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setMinimumSize(new Dimension(300, 300)); frame.setVisible(true); } public void add() { final JPanel content = new JPanel(); JPanel tab = new JPanel(); tab.setOpaque(false); JLabel tabLabel = new JLabel("Tab " + (++tabCounter)); JButton tabCloseButton = new JButton(closeXIcon); tabCloseButton.setPreferredSize(closeButtonSize); tabCloseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int closeTabNumber = tabbedPane.indexOfComponent(content); tabbedPane.removeTabAt(closeTabNumber); } }); tab.add(tabLabel, BorderLayout.WEST); tab.add(tabCloseButton, BorderLayout.EAST); tabbedPane.addTab(null, content); tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tab); } public static void main(String[] args) { JDK6TabbedPaneExample main = new JDK6TabbedPaneExample(); } }
fn:containsIgnoreCase()函数 fn:containsIgnoreCase()函数用于确定一个字符串是否包含指定的子串,忽略大小写。 语法fn:contain...
点击计数器Web 页面的点击计数器很多时候,你可能有兴趣知道你网站的某个特定页面上的总点击量。使用 servlet 来计算这些点击量...
Hello, Cargo!Cargo 是 Rustaceans 用来帮助管理他们的 Rust 项目的一个工具。Cargo 目前处在 pre-1.0 状态,所以它仍然是一项正...
MATLAB下载安装安装MATLAB的过程很简单,最重要的是要找到可靠的下载资源。对此,我们需要到 MathWorks 的官方网站进行下载,Mat...
WebSecurity - Logout()WebSecurity 对象定义 Logout() 方法注销当前用户。C# 和 VB 语法Logout()实例实例 C#// Log out the cur...