gpt4 book ai didi

java - JTabpan 拆分按钮

转载 作者:行者123 更新时间:2023-12-01 11:02:22 27 4
gpt4 key购买 nike

根据https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html我可以将自定义组件添加到 jtabpane,我尝试向选项卡添加一个组合框,这样选项卡中就会有一个向下的箭头,可以从选择中进行选择,就像带有拆分按钮的 Jtabpane 一样。

在我的示例中,我尝试使用关闭按钮修改现有的选项卡 Pane ,但它不起作用。谁能指出我正确的方向?

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);
}

}

最佳答案

From my example i have tried to modify the existing tab pane with a close button , however it does not work. anyone can point me in the right direction?

在教程的 ButtonTabComponent 类中,我进行了以下更改:

//JButton button = new TabButton();
//add(button);
String[] calcStrings = { "BMI", "BMR", "BFP", "Weight Loss"};
add( new JComboBox(calcStrings) );

并且组合框按预期显示在选项卡上。

所以,我建议你:

  1. 从教程中的工作示例开始
  2. 进行与上述相同的更改以证明您可以使用组合框
  3. 从教程中删除一些不必要的代码并重新测试。
  4. 然后,当它再次停止工作时,您就知道您所做的更改导致了问题。

关于java - JTabpan 拆分按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33235529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com