这样的界面怎么做呢

wangbylove 2013-07-02 05:57:31


如图的界面,要这样实现每一个按钮点下都显示对应的界面呢
...全文
160 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangbylove 2013-07-04
  • 打赏
  • 举报
回复
引用 3 楼 FirstHelloWorld 的回复:
利用JTabbedPane类中的addTab方法即可实现你的要求!! 源码
/**
     * Adds a <code>component</code> and <code>tip</code>
     * represented by a <code>title</code> and/or <code>icon</code>,
     * either of which can be <code>null</code>.
     * Cover method for <code>insertTab</code>.
     *
     * @param title the title to be displayed in this tab
     * @param icon the icon to be displayed in this tab
     * @param component the component to be displayed when this tab is clicked
     * @param tip the tooltip to be displayed for this tab
     * 
     * @see #insertTab
     * @see #removeTabAt  
     */
    public void addTab(String title, Icon icon, Component component, String tip) {
        insertTab(title, icon, component, tip, pages.size()); 
    }
正解,我就用他里
StevenLoveMaggie 2013-07-02
  • 打赏
  • 举报
回复
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.Color;
import java.awt.Font;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;

public class  FrameTest extends JFrame
{
	static final int SIZE = 6;
	private JButton[] buttons = new JButton[SIZE];
	private JPanel upPanel;  //上部窗体
	private JPanel centerPanel;   //中部窗体
	private JPanel[] insidePanles =  new JPanel[SIZE];  //内部窗体
	private CardLayout cardLayout = new CardLayout();  //布局方式
	private final String[] name={"按钮1","按钮2","按钮3","按钮4","按钮5","按钮6"};
	private final String names = "内部窗口";
	
	ActionListener listener = new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			JButton button = (JButton)e.getSource();
        	cardLayout.show(centerPanel,button.getLabel());
        }
	};
	
	public FrameTest(){
		super("Frame");
		
		//设置布局方式为BorderLayout
		this.setLayout(new BorderLayout());
		//创建upPanel
		upPanel = new JPanel();
		upPanel.setLayout(new FlowLayout());//设置布局方式为FlowLayout
		//创建中部面板
		centerPanel = new JPanel();
		centerPanel.setLayout(cardLayout);
		
		for(int i = 0;i<SIZE;i++){
			//实例化buttons,并设置属性
			buttons[i] = new JButton("按钮"+(i+1));
			buttons[i].setFont(new Font("宋体", 4, 15));
            buttons[i].setBackground(Color.yellow);
            buttons[i].addActionListener(listener);
			upPanel.add(buttons[i]);
			
			insidePanles[i] = new JPanel();
			insidePanles[i].add(new JLabel("内部窗口"+(i+1)));
			insidePanles[i].setBackground(Color.yellow);
			centerPanel.add(insidePanles[i],name[i]);
		}
		
		this.add(upPanel,BorderLayout.NORTH);
		this.add(centerPanel,BorderLayout.CENTER);
		this.setVisible(true);
		this.setSize(450,300);
	}

	public static void main(String[] args){
		FrameTest ft = new FrameTest();
	}
}
zhangxm2015 2013-07-02
  • 打赏
  • 举报
回复
利用JTabbedPane类中的addTab方法即可实现你的要求!! 源码
/**
     * Adds a <code>component</code> and <code>tip</code>
     * represented by a <code>title</code> and/or <code>icon</code>,
     * either of which can be <code>null</code>.
     * Cover method for <code>insertTab</code>.
     *
     * @param title the title to be displayed in this tab
     * @param icon the icon to be displayed in this tab
     * @param component the component to be displayed when this tab is clicked
     * @param tip the tooltip to be displayed for this tab
     * 
     * @see #insertTab
     * @see #removeTabAt  
     */
    public void addTab(String title, Icon icon, Component component, String tip) {
        insertTab(title, icon, component, tip, pages.size()); 
    }
Inhibitory 2013-07-02
  • 打赏
  • 举报
回复
JTabPane或者用CardLayout
fsh2008 2013-07-02
  • 打赏
  • 举报
回复
用frame,或直接放多个层,每个按钮对应显示某个层并隐藏当前层。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧