Java: 如TabbedPane一样显示多个面板: 设置时用得多
2009-02-09 14:14
429 查看
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MultiPanel extends JPanel { private static final long serialVersionUID = 1L; private JButton changeButton1; private JButton changeButton2; private JButton changeButton3; private JPanel panel1; private JPanel panel2; private JPanel panel3; private JPanel currentPanel; public MultiPanel() { this.setLayout(new BorderLayout()); changeButton1 = new JButton( "<html><font color=red>Red Panel</font></html>"); changeButton2 = new JButton( "<html><font color=green>Green Panel</font></html>"); changeButton3 = new JButton( "<html><font color=blue>Blue Panel</font></html>"); panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); panel1.setBackground(Color.RED); panel2.setBackground(Color.GREEN); panel3.setBackground(Color.BLUE); currentPanel = panel1; Box box = Box.createHorizontalBox(); box.add(changeButton1); box.add(changeButton2); box.add(changeButton3); this.add(box, BorderLayout.SOUTH); this.add(currentPanel, BorderLayout.CENTER); addButtonActionListener(); } private void addButtonActionListener() { changeButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { currentPanel.setVisible(false); currentPanel = panel1; currentPanel.setVisible(true); add(currentPanel, BorderLayout.CENTER); validate(); } }); changeButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { currentPanel.setVisible(false); currentPanel = panel2; currentPanel.setVisible(true); add(currentPanel, BorderLayout.CENTER); validate(); } }); changeButton3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { currentPanel.setVisible(false); currentPanel = panel3; currentPanel.setVisible(true); add(currentPanel, BorderLayout.CENTER); validate(); } }); } private static void createAndShowGUI() { JFrame frame = new JFrame("Multi Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 600); MultiPanel panel = new MultiPanel(); frame.getContentPane().add(panel, BorderLayout.CENTER); frame.setVisible(true); } /** * @param args */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { MultiPanel.createAndShowGUI(); } }); } }
相关文章推荐
- [JAVA]①重绘JPanel面板,设置窗体背景图片
- 【教程】JAVA设置窗体透明显示
- Java 编程下 Eclipse 如何设置单行代码显示的最大宽度
- java ToolTipText() 设置换行 字体 和 显示时间
- table里面怎么设置td的样式和Excel里面的表格一样&&当光标浮动td上到元素上时如何显示全部文本。
- 设置java的JTextPane字体加粗
- 用FOR,和数组显示多个精灵并设置位置
- 我的VS2005采用的是web开发设置。不知道动到了那里,现在每次按F5启动调试的时候总是不能成功启动调试。效果和按CTRL+F5一样。就是把项目显示在了浏览器之后,那个启动调试的绿色按钮还可以使用,不是暗色的。
- 在eclipse中将javadoc设置成中文显示
- SystemUI下的快速设置面板显示异常
- 总结下JavaWeb应用里正确显示中文需要的设置
- java设置JTable中作为序号显示的第一列居中显示
- 第一个Java程序,简单的打开图片并显示在面板上
- Java中文件选择器JFileChooser使用(如何设置默认文件名,并在框中显示)小结
- Filezilla虚拟目录的配置(解决设置多个目录只显示主目录的问题)
- Java String.Split(String regex)方法之设置多个分隔符
- 解决Revit可停靠面板(Dockablepane)显示不全等UI问题
- [Java] Eclipse 设置相同变量背景色高亮显示
- 用三个Java类设置文本显示
- intellij idea13.1和myeclipse一样同时显示多个项目