Java选项卡内的子面板怎么添加多个选项卡
importjavax.swing.event.*;importjava.awt.event.*;importjavax.swing.*;importjava.awt.*...
import javax.swing.event.*;import java.awt.event.*;import javax.swing.*;import java.awt.*;import java.beans.*; public class PPP extends JFrame { private JTabbedPane tabbedPane; private JTabbedPane tpp; public PPP() { getContentPane().setLayout(new BorderLayout()); JTabbedPane tabbedPane = new JTabbedPane(); getContentPane().add(BorderLayout.CENTER, tabbedPane); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); setTitle("JTabbedPane Nested Demonstration"); setSize(new Dimension(600, 500)); Container c = getContentPane(); createTab(tabbedPane, 0); createTab(tabbedPane, 1); } protected void createTab(JTabbedPane jtp, int ct) { if (ct > 2) return; JTabbedPane tp = new JTabbedPane(); tp.setTabPlacement(JTabbedPane.LEFT); jtp.add("TabLevel #" + ct,tp); createTab(tp, ct+1); } public static void main(String []args) { PPP main = new PPP(); main.show(); }}图一是想要的样子,图二是实际的样子
希望回答能提供编译结果如图一的代码 展开
希望回答能提供编译结果如图一的代码 展开
1个回答
展开全部
写了2个参考代码,第一个代码,在原基础上的修改.如下
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.beans.*;
public class PPP extends JFrame {
private JTabbedPane tabbedPane;
private JTabbedPane tpp;
public PPP() {
getContentPane().setLayout(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
getContentPane().add(BorderLayout.CENTER, tabbedPane);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
setTitle("JTabbedPane Nested Demonstration");
setSize(new Dimension(600, 500));
Container c = getContentPane();
createTab(tabbedPane, 0);
createTab(tabbedPane, 1);
}
protected void createTab(JTabbedPane jtp, int ct) {
JTabbedPane tp = new JTabbedPane();
tp.setTabPlacement(JTabbedPane.LEFT);
for (int i = ct; i < 2; i++) {
tp.add("TabLevel #" + (i+1), new JPanel());
}
jtp.add( tp,"TabLevel #"+ct );
}
public static void main(String[] args) {
PPP main = new PPP();
main.show();
}
}
参考代码2 .对过时的方法进行了替换, 简化了不必要的方法
import javax.swing.*;
import java.awt.*;
public class PPP2 extends JFrame {
private JTabbedPane tabbedPane;
private JTabbedPane tpp;
public PPP2() {
tabbedPane = new JTabbedPane();
//tabbedPane 的第一个页面tpp
tpp = new JTabbedPane(JTabbedPane.LEFT);
JPanel jp1 = new JPanel();jp1.setBackground(Color.YELLOW);
tpp.add("TabLevel # 1", jp1);
JPanel jp2 = new JPanel();jp2.setBackground(Color.RED);
tpp.add("TabLevel # 2", jp2);
//tabbedPane 的第二个页面jp3 (如果第二个页面还是一个JTabbedPane的话,参考第一个页面的写法)
JPanel jp3= new JPanel();
jp3.setBackground(Color.BLUE);
//把页面加入到tabbedPane
tabbedPane.add("TabLevel # 0", tpp);
tabbedPane.add("TabLevel # 1", jp3);
//把tabbedPane加入到窗口里
add(tabbedPane);
setTitle("JTabbedPane Nested Demonstration");
setSize(new Dimension(600, 500));
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//相当于addWindowListener..System.exit(0);这段代码
}
public static void main(String[] args) {
PPP2 main = new PPP2();
main.setVisible(true);//show已经是过时的方法了,使用setVisible
}
}
运行测试
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询