怎么上jtabbedpane的标签栏上设置按钮? 像eclipse里面的那种
1个回答
展开全部
我觉得是你理解错了,按钮不是在jtabbedpane,只不过是通过布局,让控件显示在那个位置。
下边是一个例子。
----------------------------------------------------------------------------------------------------------
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class FontApp extends JFrame implements ChangeListener {
JButton btnNewButton = null;
public FontApp() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(12, 81, 370, 181);
getContentPane().add(tabbedPane);
JPanel panel = new JPanel();
tabbedPane.addTab("tab1", null, panel, null);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("tab2", null, panel_1, null);
JPanel panel_2 = new JPanel();
tabbedPane.addTab("tab3", null, panel_2, null);
tabbedPane.addChangeListener(this);
JPanel panel_3 = new JPanel();
panel_3.setBounds(225, 74, 157, 26);
getContentPane().add(panel_3);
btnNewButton = new JButton("New button");
panel_3.add(btnNewButton);
setVisible(true);
}
public static void main(String[] args) {
new FontApp();
}
public void stateChanged(ChangeEvent e) {
JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
int index = tabbedPane.getSelectedIndex();
btnNewButton.setVisible(true);
if (index == 0) {
btnNewButton.setText("New button");
}
if (index == 1) {
btnNewButton.setText(" button");
}
if (index == 2) {
btnNewButton.setVisible(false);
}
}
}
下边是一个例子。
----------------------------------------------------------------------------------------------------------
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class FontApp extends JFrame implements ChangeListener {
JButton btnNewButton = null;
public FontApp() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(12, 81, 370, 181);
getContentPane().add(tabbedPane);
JPanel panel = new JPanel();
tabbedPane.addTab("tab1", null, panel, null);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("tab2", null, panel_1, null);
JPanel panel_2 = new JPanel();
tabbedPane.addTab("tab3", null, panel_2, null);
tabbedPane.addChangeListener(this);
JPanel panel_3 = new JPanel();
panel_3.setBounds(225, 74, 157, 26);
getContentPane().add(panel_3);
btnNewButton = new JButton("New button");
panel_3.add(btnNewButton);
setVisible(true);
}
public static void main(String[] args) {
new FontApp();
}
public void stateChanged(ChangeEvent e) {
JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
int index = tabbedPane.getSelectedIndex();
btnNewButton.setVisible(true);
if (index == 0) {
btnNewButton.setText("New button");
}
if (index == 1) {
btnNewButton.setText(" button");
}
if (index == 2) {
btnNewButton.setVisible(false);
}
}
}
追问
添加多个按钮也OK?
追答
当然,布局是你设计的,你想怎么添加都行啊。
我放按钮那块的panel很小,你可以扩大。
只是演示一下效果,我知道样子挺丑的。
你在eclipse里多开一些这样的view,然后看一下debug,那个按钮多,
就会出现在下边的容器里,与上面不是在一行里。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询