我要求将最小化和关闭按钮放在我的JTabbedPane
结束的右上角。为清楚起见,环绕区域是我需要的两个按钮(我有一个未修饰的框架)
任何人都可以指导我如何实现这一目标吗?我的问题是(更多)关于按钮的定位(在JTabbedPane上)而不是实现最小化/关闭的功能
谢谢!
答案 0 :(得分:1)
作为子项添加到JTabbedPane的所有组件都将包含在选项卡中。这意味着无法将按钮直接添加到JTabbedPane。如果您希望将关闭和最小化按钮作为选项卡式窗格的一部分直接(而不是侧面),我建议在选项卡式窗格中添加两个选项卡,其中包含自定义选项卡组件(JTabbedPane.setTabComponentAt()
)是按钮。由于按钮是选项卡的唯一组件,因此无法选择实际的“选项卡”本身,只会发生按钮事件。使用JTabbedPane时,无法修复选项卡的位置,这将导致这两个“按钮选项卡”在您选择的布局中浮动。在它们左侧插入更多选项卡将具有使用关闭和最小化按钮始终结束选项卡列表的效果。
答案 1 :(得分:0)
我找到了解决此问题的方法:只需在选项卡式窗格旁边放一个工具栏
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JTabbedPane;
import java.awt.FlowLayout;
import java.awt.Insets;
import javax.swing.JToolBar;
import java.awt.ComponentOrientation;
import javax.swing.JButton;
import java.awt.Cursor;
public class PanelTab extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PanelTab frame = new PanelTab();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PanelTab() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 424, 0 };
gbl_contentPane.rowHeights = new int[] { 51, 0 };
gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
contentPane.setLayout(gbl_contentPane);
JPanel panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 0;
gbc_panel.gridy = 0;
contentPane.add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] { 184, 0 };
gbl_panel.rowHeights = new int[] { 54, 0 };
gbl_panel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
gbl_panel.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
panel.setLayout(gbl_panel);
JToolBar toolBar = new JToolBar();
toolBar.setOpaque(false);
toolBar.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
toolBar.setFloatable(false); // The toolbar must not be float
GridBagConstraints gbc_toolBar = new GridBagConstraints();
gbc_toolBar.anchor = GridBagConstraints.NORTHEAST;
gbc_toolBar.gridx = 0;
gbc_toolBar.gridy = 0;
panel.add(toolBar, gbc_toolBar);
JButton btnHola = new JButton("You must put your exit icon here");
btnHola.setOpaque(false);
toolBar.add(btnHola);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
gbc_tabbedPane.fill = GridBagConstraints.BOTH;
gbc_tabbedPane.gridx = 0;
gbc_tabbedPane.gridy = 0;
panel.add(tabbedPane, gbc_tabbedPane);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("New tab", null, panel_1, null);
}
}
我想这可能对你有帮助......