根据https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html我可以将自定义组件添加到jtabpane,我已经尝试将一个组合框添加到选项卡,以便从选项卡中将有一个向下的箭头,可以选择从选择中选择就像带有分割按钮的Jtabpane一样。
从我的示例中,我尝试使用关闭按钮修改现有选项卡窗格,但它不起作用。谁能指出我正确的方向?
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.plaf.ComboBoxUI;
public class DropdownPaneComponent extends JPanel {
String[] calcStrings = { "BMI", "BMR", "BFP", "Weight Loss"};
private JTabbedPane pane;
public DropdownPaneComponent(JTabbedPane pane) {
//unset default FlowLayout' gaps
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
if (pane == null) {
throw new NullPointerException("TabbedPane is null");
}
this.pane = pane;
setOpaque(false);
//make JLabel read titles from JTabbedPane
JLabel label = new JLabel() {
public String getText() {
int i = pane.indexOfTabComponent(DropdownPaneComponent.this);
if (i != -1) {
return pane.getTitleAt(i);
}
return null;
}
};
add(label);
//add more space between the label and the button
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
//tab button
JComboBox jcbCalcs = new TabButton();
add(jcbCalcs);
//add more space to the top of the component
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
}
private class TabButton extends JComboBox {
public TabButton() {
super(calcStrings);
setToolTipText("close this tab");
//Make the button looks the same for all Laf's
//Make it transparent
//No need to be focusable
setFocusable(false);
setBorder(BorderFactory.createEtchedBorder());
}
}
}
jframe
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JComboBox;
import javax.swing.JButton;
public class FatBlaster {
private JFrame frmFatBlasterPrototype;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FatBlaster window = new FatBlaster();
window.frmFatBlasterPrototype.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public FatBlaster() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmFatBlasterPrototype = new JFrame();
frmFatBlasterPrototype.setTitle("Fat Blaster Prototype");
frmFatBlasterPrototype.setBounds(100, 100, 450, 300);
frmFatBlasterPrototype.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frmFatBlasterPrototype.getContentPane().add(tabbedPane, BorderLayout.CENTER);
JPanel pnlCalculation = new JPanel();
tabbedPane.addTab("Calculations", null, pnlCalculation, null);
tabbedPane.setComponentAt(0, new DropdownPaneComponent(tabbedPane));
JPanel pnlFoodDiary = new JPanel();
tabbedPane.addTab("Food Diary", null, pnlFoodDiary, null);
JMenuBar menuBar = new JMenuBar();
frmFatBlasterPrototype.setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("File");
menuBar.add(mnNewMenu);
JMenu mnNewMenu_1 = new JMenu("View");
menuBar.add(mnNewMenu_1);
}
}
答案 0 :(得分:1)
从我的示例中,我尝试使用关闭按钮修改现有选项卡窗格,但它不起作用。谁能指出我正确的方向?
在本教程的ButtonTabComponent
课程中,我进行了以下更改:
//JButton button = new TabButton();
//add(button);
String[] calcStrings = { "BMI", "BMR", "BFP", "Weight Loss"};
add( new JComboBox(calcStrings) );
,组合框按预期显示在选项卡上。
所以,我建议你: