JTabbedPane
在面板上设置多个选项卡
import java.awt.Container; import java.io.File; 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 JTabbedPaneDemo { public static void main(String[] args) { JFrame frame = new JFrame("Welcome to leaf's blog"); Container cont = frame.getContentPane(); JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP); JPanel pan1 = new JPanel(); JPanel pan2 = new JPanel(); JButton but = new JButton("按钮"); JLabel lab = new JLabel("标签"); pan1.add(but); pan2.add(lab); String picPath = "E:" + File.separator + "1.jpg"; tab.addTab("图片选项", new ImageIcon(picPath), pan1,"图像"); tab.addTab("文字选项", pan2); cont.add(tab); frame.setSize(200, 100); frame.setLocation(300, 200); frame.setVisible(true); } }
JScrollPane
为图片设置滚动条
import java.awt.Container; import java.io.File; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Icon; import javax.swing.JScrollPane; public class JScrollPaneDemo { public static void main(String[] args) { JFrame frame = new JFrame("Welcome to leaf's blog"); Container cont = frame.getContentPane(); String picPath = "E:" + File.separator + "1.jpg"; Icon icon = new ImageIcon(picPath); JPanel pan = new JPanel(); JLabel lab = new JLabel(icon); pan.add(lab); JScrollPane scr = new JScrollPane(pan,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); cont.add(scr); frame.setSize(200, 100); frame.setLocation(300, 200); frame.setVisible(true); } }
单行文件输入组件JTextField
import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class JTextFieldDemo { public static void main(String[] args) { JFrame frame = new JFrame("Welcome to leaf's blog"); JTextField name = new JTextField(30); JTextField noed = new JTextField("leaf",10); JLabel namelab = new JLabel("输入用户姓名:"); JLabel noedlab = new JLabel("不可编辑文本:"); noed.setEnabled(false); name.setColumns(30); noed.setColumns(10); frame.setLayout(new GridLayout(2,2)); frame.add(namelab); frame.add(name); frame.add(noedlab); frame.add(noed); frame.setSize(200, 100); frame.setLocation(300, 200); frame.setVisible(true); } }
密文输入组件JPasswordField
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; public class JPasswordFieldDemo { public static void main(String[] args) { JFrame frame = new JFrame("Welcome to leaf's blog"); JPasswordField jpf1 = new JPasswordField(); JPasswordField jpf2 = new JPasswordField(); jpf2.setEchoChar('#'); JLabel lab1 = new JLabel("默认的回显:"); JLabel lab2 = new JLabel("回显设置'#':"); lab1.setBounds(10,10,100,20); lab2.setBounds(10,40,100,20); jpf1.setBounds(110,10,80,20); jpf2.setBounds(110,40,50,20); frame.setLayout(null); frame.add(lab1); frame.add(lab2); frame.add(jpf1); frame.add(jpf2); frame.setSize(200, 100); frame.setLocation(300, 200); frame.setVisible(true); } }
多行文本输入组件JTextAera
import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JScrollPane; public class JTextAreaDemo { public static void main(String[] args) { JFrame frame = new JFrame("Welcome to leaf's blog"); JTextArea jta = new JTextArea(20,100); jta.setLineWrap(true); JScrollPane scr = new JScrollPane(jta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); JLabel lab = new JLabel("多行文本域:"); lab.setBounds(10, 10, 120, 20); jta.setBounds(130, 10, 150, 100); frame.setLayout(new GridLayout(2,1)); frame.add(lab); frame.add(scr); frame.setSize(300, 150); frame.setLocation(300,200); frame.setVisible(true); } }