/* * TabbedPanel.java * * Created on 2009-2-2, 16:16:06 */ package components; import java.awt.BorderLayout; import java.awt.event.KeyEvent; import java.io.FileNotFoundException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTabbedPane; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; /** * * @author xuan */ public class TabbedPanel extends javax.swing.JPanel { /** Creates new form TabbedPanel */ public TabbedPanel() { initComponents(); } protected JComponent makeTextPanel(String text,String buttons) { JPanel panel = new JPanel(false); JLabel filler = new JLabel(text); JButton button = new JButton(buttons); filler.setHorizontalAlignment(JLabel.CENTER); // panel.setLayout(new GridLayout(1, 1)); panel.add(filler); panel.add(button); return panel; } protected JComponent makePanel() { JPanel panel = new JPanel(false); JLabel username = new JLabel("用户名:"); JTextField text = new JTextField(10); username.setHorizontalAlignment(JLabel.LEFT); JLabel password = new JLabel("密码:"); JPasswordField pass = new JPasswordField(10); panel.add(username); panel.add(text); password.setHorizontalAlignment(JLabel.LEFT); panel.add(password); panel.add(pass); return panel; } private static void createAndShowGUI() throws FileNotFoundException { //Create and set up the window. JFrame frame = new JFrame("TabbedPane"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add content to the window. frame.add(new TabbedPanel(), BorderLayout.CENTER); //Display the window. frame.pack(); frame.setVisible(true); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { tabbed = new javax.swing.JTabbedPane(); setName("Form"); // NOI18N tabbed.setTabPlacement(javax.swing.JTabbedPane.RIGHT); tabbed.setName("tabbed"); // NOI18N JComponent panel1 = makeTextPanel("自定义标签1","按钮1"); tabbed.addTab("选项卡1",panel1); tabbed.setMnemonicAt(0,KeyEvent.VK_1); JComponent panel2 = makeTextPanel("标签2","按钮1"); tabbed.addTab("连接设置", makePanel()); tabbed.setMnemonicAt(1,KeyEvent.VK_2); JComponent panel3 = makeTextPanel("标签3","欢迎光临"); tabbed.addTab("选项卡3", panel3); add(tabbed); tabbed.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(25, 25, 25) .addComponent(tabbed, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(40, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(40, 40, 40) .addComponent(tabbed, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(57, Short.MAX_VALUE)) ); }// </editor-fold> public static void main(String[] args) { //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { //Turn off metal's use of bold fonts UIManager.put("swing.boldMetal", Boolean.FALSE); try { createAndShowGUI(); } catch (FileNotFoundException ex) { Logger.getLogger(TabbedPanel.class.getName()).log(Level.SEVERE, null, ex); } } }); } // Variables declaration - do not modify private javax.swing.JTabbedPane tabbed; // End of variables declaration } 截图如下所示:
使用netbeans6.5编译通过
截图如下所示: